samlitowitz/phpstan-opinionated-nomenclature

Opinionated PHPStan rules for naming things

1.0.1 2024-11-29 14:48 UTC

This package is auto-updated.

Last update: 2024-11-29 16:28:06 UTC


README

Opinionated PHPStan rules for naming things.

Table of Contents

  1. Installation
    1. PHPStan Extension Installer
    2. Manual Installation
  2. Rules
    1. Namespace
    2. Class Like (Class, Interface, Trait)
    3. Class
    4. Interface

Installation

Install the extension via Composer.

composer require --dev samlitowitz/phpstan-opinionated-nomenclature

This extension requires PHPStan to use.

PHPStan Extension Installer

Use the PHPStan Extension Installer to automatically install PHPStan extensions.

Manual Installation

Add vendor/samlitowitz/phpstan-opinionated-nomenclature/extension.neon to the includes section of your phpstan.neon or phpstan.neon.dist file, i.e.

includes:
    - vendor/samlitowitz/phpstan-opinionated-nomenclature/extension.neon

Rules

Namespace

  1. Namespace names MUST NOT be equal to or end in DTO of any case (case-insensitive)

    <?php
    namespace DTO; // bad
    namespace Client\Dto; // bad
  2. Namespace names MUST NOT be equal to Helper of any case (case-insensitive)

    <?php
    namespace Helper; // bad
    namespace Client\helper; // bad

Class Like (Class, Interface, Trait)

  1. Class like names MUST NOT be equal to or end in DTO of any case (case-insensitive)

    <?php
    class DTO {} // bad
    class ClientDTO {} // bad
  2. Class like names MUST NOT be equal to Helper of any case (case-insensitive)

    <?php
    class Helper {} // bad
  3. Class like names MUST NOT be equal to or start with any case (case-insensitive) of the namespace name it resides in

    <?php
    namespace Client;
    class ClientRequest {} // bad
    
    namespace Client\Request;
    class Request {} // bad
  4. Class like names MUST NOT end with their type name of any case (case-insensitive)

    <?php
    class ClientClass {} // bad
    interface FileInterface {} // bad
    trait TransactionTrait {} // bad

Class

  1. No non-final classes without children

    <?php
    class NonFinalNoChildren {} // bad

Interface

  1. Interface names MUST NOT be prefixed with I

    <?php
    interface IWriter {} // bad