Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

[PHPStanRules] Add MaxFileLengthRule #2907

Merged
merged 11 commits into from
Feb 5, 2021
6 changes: 6 additions & 0 deletions packages/phpstan-rules/config/configurable-rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ services:
arguments:
forbiddenTypes:
- Symfony\Component\Console\Command\Command

-
class: Symplify\PHPStanRules\Rules\MaxFileLengthRule
tags: [phpstan.rules.rule]
arguments:
maxLength: 200
79 changes: 79 additions & 0 deletions packages/phpstan-rules/src/Rules/MaxFileLengthRule.php
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,
]
),
]);
}
}
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
{
}
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
{
}
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');
}
}
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
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

@TomasVotruba TomasVotruba Feb 5, 2021

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 :)