diff --git a/packages/phpstan-rules/config/configurable-rules.neon b/packages/phpstan-rules/config/configurable-rules.neon index 7f037eef89..271cf4c109 100644 --- a/packages/phpstan-rules/config/configurable-rules.neon +++ b/packages/phpstan-rules/config/configurable-rules.neon @@ -121,3 +121,9 @@ services: arguments: forbiddenTypes: - Symfony\Component\Console\Command\Command + + - + class: Symplify\PHPStanRules\Rules\MaxFileLengthRule + tags: [phpstan.rules.rule] + arguments: + maxLength: 200 diff --git a/packages/phpstan-rules/src/Rules/MaxFileLengthRule.php b/packages/phpstan-rules/src/Rules/MaxFileLengthRule.php new file mode 100644 index 0000000000..70c720e8bb --- /dev/null +++ b/packages/phpstan-rules/src/Rules/MaxFileLengthRule.php @@ -0,0 +1,79 @@ +maxLength = $maxLength; + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [FileNode::class]; + } + + /** + * @param FileNode $node + * @return string[] + */ + public function process(Node $node, Scope $scope): array + { + $file = $scope->getFile(); + $long = strlen($file); + + if ($long < $this->maxLength) { + return []; + } + + return [sprintf(self::ERROR_MESSAGE, $file, $long, $this->maxLength)]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Path file must be shorten then configured maxLength', [ + new ConfiguredCodeSample( + <<<'CODE_SAMPLE' +# file path +/app/foo/bar/baz.php +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +# file path +/app/foo/baz.php +CODE_SAMPLE + , + [ + 'maxLength' => 17, + ] + ), + ]); + } +} diff --git a/packages/phpstan-rules/tests/Rules/MaxFileLengthRule/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php b/packages/phpstan-rules/tests/Rules/MaxFileLengthRule/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php new file mode 100644 index 0000000000..0a4e295140 --- /dev/null +++ b/packages/phpstan-rules/tests/Rules/MaxFileLengthRule/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php @@ -0,0 +1,9 @@ +analyse([$filePath], $expectedErrorMessagesWithLines); + } + + public function provideData(): Iterator + { + yield [__DIR__ . '/Fixture/SkipNotLong.php', []]; + yield [ + __DIR__ . '/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php', + [ + [ + sprintf( + MaxFileLengthRule::ERROR_MESSAGE, + __DIR__ . '/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php', + strlen( + __DIR__ . '/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php' + ), + 178 + ), + 03, + ], + ], + ]; + } + + protected function getRule(): Rule + { + return $this->getRuleFromConfig(MaxFileLengthRule::class, __DIR__ . '/config/configured_rule.neon'); + } +} diff --git a/packages/phpstan-rules/tests/Rules/MaxFileLengthRule/config/configured_rule.neon b/packages/phpstan-rules/tests/Rules/MaxFileLengthRule/config/configured_rule.neon new file mode 100644 index 0000000000..843805a341 --- /dev/null +++ b/packages/phpstan-rules/tests/Rules/MaxFileLengthRule/config/configured_rule.neon @@ -0,0 +1,9 @@ +includes: + - ../../../../config/services/services.neon + +services: + - + class: Symplify\PHPStanRules\Rules\MaxFileLengthRule + tags: [phpstan.rules.rule] + arguments: + maxLength: 178 diff --git a/phpstan.neon b/phpstan.neon index de57364a31..7b20a6c86b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -432,3 +432,8 @@ parameters: paths: - packages/changelog-linker/src/DependencyInjection/CompilerPass/AddRepositoryUrlAndRepositoryNameParametersCompilerPass.php - packages/symfony-route-usage/tests/Helper/DatabaseLoaderHelper.php + + - + message: '#Paths for file ".*CheckRequiredAutowireAutoconfigurePublicUsedInConfigServiceRuleTest\.php" has \d+ chars, but must be shorter than 200#' + paths: + - packages/phpstan-rules/tests/Rules/CheckRequiredAutowireAutoconfigurePublicUsedInConfigServiceRule/CheckRequiredAutowireAutoconfigurePublicUsedInConfigServiceRuleTest.php