Repository navigation
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 inecs.php, for example inwithSkip(). - The moved checks now run with
phpstaninstead ofecs. They only check files within your PHPStan paths, so a file that ECS checked but PHPStan doesn't analyse (for example intests/orconfig/) is no longer checked. - Ignore comments for the moved checks change. Replace
// phpcs:ignorewith@phpstan-ignore <identifier>, using the identifiers in the table below. - The
env(),HasFactory, config filename and Blade checks now need Larastan. Laravel projects withoutlarastan/larastanno 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 inException, so it may report classes that passed before. Subclasses ofErrormay use anErrorsuffix 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.phpand dotted names such asmail.localSettings.phpare now reported. - The listener suffix check now matches by namespace.
EventListenerSuffixSniffis removed in favour of the existingNamespaceBasedSuffixRuleentry forApp\Listeners. Applications with a root namespace other thanAppneed their own entry to keep the check. - Rector's
CompactToVariablesRectoris replaced byCompactToArrayRector. The new rule leavescompact()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
-
Update the package:
composer require --dev worksome/coding-style:^7.0
-
Remove any references to
Worksome\CodingStyle\Sniffs\*classes fromecs.php:-use Worksome\CodingStyle\Sniffs\Laravel\DisallowEnvUsageSniff; ... ->withSkip([ - DisallowEnvUsageSniff::class => [ - __DIR__ . '/tests/Browser', - ], ]);
-
Move any exceptions you had for those sniffs to PHPStan. For example, to keep allowing
HasFactoryin models, redeclare thedisallowedClassesentry withallowInin yourphpstan.neon:parameters: disallowedClasses: - class: 'Illuminate\Database\Eloquent\Factories\HasFactory' allowIn: - app/Models/*
Other checks can be ignored per path with
ignoreErrorsand the identifiers above, or disabled entirely with theworksomeandworksomeLaravelparameters. -
Replace any
// phpcs:ignorecomments for the moved checks with@phpstan-ignore <identifier>. -
Laravel projects only: add
configto your PHPStan paths to keep the config filename check, if it isn't there already. -
Run
vendor/bin/phpstan analyse,vendor/bin/ecsandvendor/bin/rector process --dry-runto confirm everything still passes.
Things to be aware of
- Fewer ECS sniffs are loaded. These sniffs are removed from
WorksomeEcsConfig, as thepsr12set or our own PHP-CS-Fixer rules already apply the same rules:EndFileNewlineSniff,LowerCaseKeywordSniff,LowercaseDeclarationSniff,InlineControlStructureSniff,LanguageConstructSpacingSniff,ReturnTypeHintSpacingSniff,ClassConstantVisibilitySniff,UselessInheritDocCommentSniff,ClassMemberSpacingSniff,MethodSpacingSniff,PropertySpacingSniffandEmptyLinesAroundClassBracesSniff. Skips for these inecs.phpcan 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) andRequireTrailingCommaInDeclarationSniff(trailing_comma_in_multiline, now configured for parameters as well as arrays).NamespaceDeclarationSniffandPropertyDeclarationSniffare removed, as thepsr12set already covers them. Skips for these sniffs inecs.phpshould be moved to the replacement rule. - TODO comments are matched as a whole word. Only
TODOorTODOs(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
bladeResourcesDirectoryparameter (defaults to%currentWorkingDirectory%/resources), and relative paths are resolved against the working directory. - The PHPStan stub now includes
configin its paths for new projects. phpstan/phpdoc-parseris now a direct dependency. It was already installed throughslevomat/coding-standard.
Other changes
- The package now has the
devkeyword, so Composer suggests installing it with--dev.
Full Changelog: v6.0.1...v7.0.0