Skip to content

v7.0.0

Latest

Choose a tag to compare

@owenvoke owenvoke released this 18 Nov 12:29
e3398c8

The custom PHP_CodeSniffer sniffs are replaced with PHPStan rules, PHP-CS-Fixer fixers and a Rector rule. Checks that only report problems now run in PHPStan, using the AST and reflection instead of tokens, and checks that fix code run in ECS or Rector. ECS sniffs that duplicate already-loaded PHP-CS-Fixer fixers are also removed, and several others are replaced with equivalent PHP-CS-Fixer rules.

Breaking changes

  • The Worksome\CodingStyle\Sniffs\* classes are removed. Remove any references to them in ecs.php, for example in withSkip().
  • The moved checks now run with phpstan instead of ecs. They only check files within your PHPStan paths, so a file that ECS checked but PHPStan doesn't analyse (for example in tests/ or config/) is no longer checked.
  • Ignore comments for the moved checks change. Replace // phpcs:ignore with @phpstan-ignore <identifier>, using the identifiers in the table below.
  • The env(), HasFactory, config filename and Blade checks now need Larastan. Laravel projects without larastan/larastan no longer get these checks.
  • The exception suffix check is stricter. It now checks every named class implementing Throwable, not only classes whose parent's name ends in Exception, so it may report classes that passed before. Subclasses of Error may use an Error suffix instead. The suffix is no longer configurable.
  • The config filename check is stricter. It checks the whole filename against a strict kebab-case pattern, so snake_case.php and dotted names such as mail.localSettings.php are now reported.
  • The listener suffix check now matches by namespace. EventListenerSuffixSniff is removed in favour of the existing NamespaceBasedSuffixRule entry for App\Listeners. Applications with a root namespace other than App need their own entry to keep the check.
  • Rector's CompactToVariablesRector is replaced by CompactToArrayRector. The new rule leaves compact() calls unchanged when a name is dynamic, may be undefined, or is a superglobal or $this.

Where each sniff went

Removed sniff Replacement Identifier
Classes\ExceptionSuffixSniff ExceptionSuffixRule (PHPStan) worksome.exceptionSuffix
Comments\DisallowTodoCommentsSniff DisallowTodoCommentsRule (PHPStan) worksome.disallowTodoComments
Enums\PascalCasingEnumCasesSniff PascalCaseEnumCasesRule (PHPStan) worksome.pascalCaseEnumCases
Laravel\ConfigFilenameKebabCaseSniff ConfigFilenameKebabCaseRule (PHPStan, Larastan only) worksome.laravel.configFilenameKebabCase
Laravel\DisallowBladeOutsideOfResourcesDirectorySniff DisallowBladeOutsideOfResourcesDirectoryRule (PHPStan, Larastan only) worksome.laravel.disallowBladeOutsideOfResourcesDirectory
Laravel\DisallowEnvUsageSniff Larastan's noEnvCallsOutsideOfConfig rule larastan.noEnvCallsOutsideOfConfig
Laravel\DisallowHasFactorySniff disallowedClasses entry for HasFactory (Larastan only) disallowed.namespace, disallowed.trait
Laravel\EventListenerSuffixSniff NamespaceBasedSuffixRule (App\Listeners: Listener) worksome.namespaceBasedSuffix
Functions\DisallowCompactUsageSniff CompactToArrayRector (Rector) —
PhpDoc\PropertyDollarSignSniff PropertyDollarSignFixer (ECS) —
PhpDoc\DisallowParamNoTypeOrCommentSniff DisallowParamNoTypeOrCommentFixer (ECS) —

Each new PHPStan rule can be disabled in the worksome parameters (exceptionSuffix, disallowTodoComments, pascalCaseEnumCases) or the worksomeLaravel parameters (configFilenameKebabCase, disallowBladeOutsideOfResourcesDirectory).

Upgrade guide

  1. Update the package:

    composer require --dev worksome/coding-style:^7.0
  2. Remove any references to Worksome\CodingStyle\Sniffs\* classes from ecs.php:

    -use Worksome\CodingStyle\Sniffs\Laravel\DisallowEnvUsageSniff;
    ...
         ->withSkip([
    -        DisallowEnvUsageSniff::class => [
    -            __DIR__ . '/tests/Browser',
    -        ],
         ]);
  3. Move any exceptions you had for those sniffs to PHPStan. For example, to keep allowing HasFactory in models, redeclare the disallowedClasses entry with allowIn in your phpstan.neon:

    parameters:
        disallowedClasses:
            -
                class: 'Illuminate\Database\Eloquent\Factories\HasFactory'
                allowIn:
                    - app/Models/*

    Other checks can be ignored per path with ignoreErrors and the identifiers above, or disabled entirely with the worksome and worksomeLaravel parameters.

  4. Replace any // phpcs:ignore comments for the moved checks with @phpstan-ignore <identifier>.

  5. Laravel projects only: add config to your PHPStan paths to keep the config filename check, if it isn't there already.

  6. Run vendor/bin/phpstan analyse, vendor/bin/ecs and vendor/bin/rector process --dry-run to confirm everything still passes.

Things to be aware of

  • Fewer ECS sniffs are loaded. These sniffs are removed from WorksomeEcsConfig, as the psr12 set or our own PHP-CS-Fixer rules already apply the same rules: EndFileNewlineSniff, LowerCaseKeywordSniff, LowercaseDeclarationSniff, InlineControlStructureSniff, LanguageConstructSpacingSniff, ReturnTypeHintSpacingSniff, ClassConstantVisibilitySniff, UselessInheritDocCommentSniff, ClassMemberSpacingSniff, MethodSpacingSniff, PropertySpacingSniff and EmptyLinesAroundClassBracesSniff. Skips for these in ecs.php can be removed.
  • Some ECS sniffs are replaced with PHP-CS-Fixer rules. These make the same changes as the sniffs they replace: SpaceAfterCastSniff (cast_spaces), SpaceAfterNotSniff (not_operator_with_successor_space), ParameterTypeHintSpacingSniff (type_declaration_spaces), RequireShortTernaryOperatorSniff (ternary_to_elvis_operator) and RequireTrailingCommaInDeclarationSniff (trailing_comma_in_multiline, now configured for parameters as well as arrays). NamespaceDeclarationSniff and PropertyDeclarationSniff are removed, as the psr12 set already covers them. Skips for these sniffs in ecs.php should be moved to the replacement rule.
  • TODO comments are matched as a whole word. Only TODO or TODOs (in any case) are reported, so words such as "Mastodon" no longer are.
  • The Blade check only covers analysed files. The resources directory can be changed with the bladeResourcesDirectory parameter (defaults to %currentWorkingDirectory%/resources), and relative paths are resolved against the working directory.
  • The PHPStan stub now includes config in its paths for new projects.
  • phpstan/phpdoc-parser is now a direct dependency. It was already installed through slevomat/coding-standard.

Other changes

  • The package now has the dev keyword, so Composer suggests installing it with --dev.

Full Changelog: v6.0.1...v7.0.0