This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 188
[PHPStanRules] Add MaxFileLengthRule #2907
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
136ff54
[PHPStanRules] [WIP] Fixes #2889 Add MaxFileLengthRule
samsonasik dceebfb
different value
samsonasik d6ad339
different value
samsonasik 0d97eac
different value
samsonasik 0ae9109
use scope->getFile()
samsonasik 1895c6d
functional
samsonasik 234241f
register rule to configurable-rules config
samsonasik 6bf80ed
test class fix
samsonasik 0bad972
phpstan fix
samsonasik 8456ed2
dynamic check path length
samsonasik 08f3c70
ignore existing CheckRequiredAutowireAutoconfigurePublicUsedInConfigS…
samsonasik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Param; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\FileNode; | ||
use Symplify\RuleDocGenerator\Contract\ConfigurableRuleInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Symplify\PHPStanRules\Tests\Rules\MaxFileLengthRule\MaxFileLengthRuleTest | ||
*/ | ||
final class MaxFileLengthRule extends AbstractSymplifyRule implements ConfigurableRuleInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Paths for file "%s" has %d chars, but must be shorter than %d.'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $maxLength; | ||
|
||
public function __construct(int $maxLength) | ||
{ | ||
$this->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, | ||
] | ||
), | ||
]); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\MaxFileLengthRule\Fixture; | ||
|
||
class ItIsVeryLongFileThatPassedMaxLengthConfigItIsVeryLongFileThatPassedMaxLengthConfigss | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/phpstan-rules/tests/Rules/MaxFileLengthRule/Fixture/SkipNotLong.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\MaxFileLengthRule\Fixture; | ||
|
||
class SkipNotLong | ||
{ | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/phpstan-rules/tests/Rules/MaxFileLengthRule/MaxFileLengthRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\MaxFileLengthRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use Symplify\PHPStanExtensions\Testing\AbstractServiceAwareRuleTestCase; | ||
use Symplify\PHPStanRules\Rules\MaxFileLengthRule; | ||
|
||
final class MaxFileLengthRuleTest extends AbstractServiceAwareRuleTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->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'); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/phpstan-rules/tests/Rules/MaxFileLengthRule/config/configured_rule.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
includes: | ||
- ../../../../config/services/services.neon | ||
|
||
services: | ||
- | ||
class: Symplify\PHPStanRules\Rules\MaxFileLengthRule | ||
tags: [phpstan.rules.rule] | ||
arguments: | ||
maxLength: 178 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rule should be renamed to more readable one in next PR. It has way too many words :)