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

Commit

Permalink
use scope->getFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 5, 2021
1 parent 0d97eac commit 0ae9109
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/phpstan-rules/src/Rules/MaxFileLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public function getNodeTypes(): array
*/
public function process(Node $node, Scope $scope): array
{
return [self::ERROR_MESSAGE];
$file = $scope->getFile();
$long = strlen($file);

if ($long < $this->maxLength) {
return [];
}

return [self::ERROR_MESSAGE, $file, $long, $this->maxLength];
}

public function getRuleDefinition(): RuleDefinition
Expand Down
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 ItIsVeryLongFileThatPassedMaxLengthConfig
{
}
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,37 @@
<?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/ItIsVeryLongFileThatPassedMaxLengthConfig.php', [
[sprintf(MaxFileLengthRule::ERROR_MESSAGE, realpath(getcwd() . '/Fixture/ItIsVeryLongFileThatPassedMaxLengthConfig.php'), 20, 17 ), 17]
]];
}

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

0 comments on commit 0ae9109

Please sign in to comment.