mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 04:19:44 +01:00
This commit is contained in:
parent
8e7c521c41
commit
63615f4197
@ -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
|
||||
|
@ -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];
|
||||
}
|
||||
}
|
@ -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'
|
||||
);
|
||||
}
|
||||
}
|
@ -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');
|
@ -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');
|
Loading…
x
Reference in New Issue
Block a user