[PHPStan] Fixes #4433 Add CheckCodeSampleBeforeAfterAlwaysDifferentRule (#4439)

This commit is contained in:
Abdul Malik Ikhsan 2020-10-18 19:14:55 +07:00 committed by GitHub
parent 8e7c521c41
commit 63615f4197
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 0 deletions

View File

@ -23,6 +23,10 @@ services:
class: Rector\PHPStanExtensions\Rule\RectorRuleAndValueObjectHaveSameStartsRule
tags: [phpstan.rules.rule]
-
class: Rector\PHPStanExtensions\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule
tags: [phpstan.rules.rule]
- Rector\PHPStanExtensions\Utils\PHPStanValueResolver
# $node->getAttribute($1) => Type|null by $1

View File

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanExtensions\Rule;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use Rector\Core\RectorDefinition\CodeSample;
/**
* @see \Rector\PHPStanExtensions\Tests\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule\CheckCodeSampleBeforeAfterAlwaysDifferentRuleTest
*/
final class CheckCodeSampleBeforeAfterAlwaysDifferentRule implements Rule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Code sample before and after must be different';
public function getNodeType(): string
{
return New_::class;
}
/**
* @param New_ $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
$class = $node->class;
if (! $class instanceof FullyQualified) {
return [];
}
$className = $class->toString();
if ($className !== CodeSample::class) {
return [];
}
$args = $node->args;
/** @var String_ $firstParameter */
$firstParameter = $args[0]->value;
/** @var String_ $secondParameter */
$secondParameter = $args[1]->value;
if ($firstParameter->value !== $secondParameter->value) {
return [];
}
return [self::ERROR_MESSAGE];
}
}

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanExtensions\Tests\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule;
use Iterator;
use PHPStan\Rules\Rule;
use Rector\PHPStanExtensions\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule;
use Symplify\PHPStanExtensions\Testing\AbstractServiceAwareRuleTestCase;
final class CheckCodeSampleBeforeAfterAlwaysDifferentRuleTest extends AbstractServiceAwareRuleTestCase
{
/**
* @dataProvider provideData()
* @param array<string|string[]|int[]> $expectedErrorsWithLines
*/
public function testRule(string $filePath, array $expectedErrorsWithLines): void
{
$this->analyse([$filePath], $expectedErrorsWithLines);
}
public function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/BeforeAfterDifferent.php', []];
yield [
__DIR__ . '/Fixture/BeforeAfterSame.php',
[[CheckCodeSampleBeforeAfterAlwaysDifferentRule::ERROR_MESSAGE, 9]],
];
}
protected function getRule(): Rule
{
return $this->getRuleFromConfig(
CheckCodeSampleBeforeAfterAlwaysDifferentRule::class,
__DIR__ . '/../../../config/phpstan-extensions.neon'
);
}
}

View File

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanExtensions\Tests\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule\Fixture;
use Rector\Core\RectorDefinition\CodeSample;
new CodeSample('A', 'B');

View File

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanExtensions\Tests\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule\Fixture;
use Rector\Core\RectorDefinition\CodeSample;
new CodeSample('A', 'A');