Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"rules-tests",
"tests"
],
"Rector\\Utils\\Tests\\": "utils-tests",
"E2e\\Parallel\\Reflection\\Resolver\\": [
"e2e/parallel-reflection-resolver/src/",
"e2e/no-parallel-reflection-resolver/src"
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,5 @@ parameters:
-
identifier: offsetAccess.invalidOffset
path: src/CustomRules/SimpleNodeDumper.php

- '#Method Rector\\Utils\\Rector\\RemoveRefactorDuplicatedNodeInstanceCheckRector\:\:getInstanceofNodeClass\(\) should return class\-string<PhpParser\\Node>\|null but returns class\-string#'
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<testsuite name="main">
<directory>tests</directory>
<directory>rules-tests</directory>
<directory>utils-tests</directory>
</testsuite>
</testsuites>

Expand Down
3 changes: 2 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Utils\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector;

return RectorConfig::configure()
->withPreparedSets(
Expand All @@ -18,7 +19,6 @@
naming: true,
instanceOf: true,
earlyReturn: true,
strictBooleans: true,
rectorPreset: true,
phpunitCodeQuality: true
)
Expand All @@ -37,6 +37,7 @@
])
->withRootFiles()
->withImportNames(removeUnusedImports: true)
->withRules([RemoveRefactorDuplicatedNodeInstanceCheckRector::class])
->withSkip([
StringClassNameToClassConstantRector::class,
// tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $node instanceof BooleanNot) {
return null;
}

if ($this->valueResolver->isFalse($node->expr)) {
return new ConstFetch(new Name('true'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ public function getNodeTypes(): array
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?FuncCall
{
if (! $node instanceof FuncCall) {
return null;
}

if (! $node->name instanceof Name) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -115,7 +116,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod, Class_ $class):
}

// parameter is required for contract coupling
if ($this->isName($classMethod->name, '__invoke') && $this->phpAttributeAnalyzer->hasPhpAttribute(
if ($this->isName($classMethod->name, MethodName::INVOKE) && $this->phpAttributeAnalyzer->hasPhpAttribute(
$class,
'Symfony\Component\Messenger\Attribute\AsMessageHandler'
)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use Rector\FileSystem\FilePathHelper;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Reflection\ReflectionResolver;

Expand All @@ -15,7 +14,6 @@
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ReflectionResolver $reflectionResolver,
private FilePathHelper $filePathHelper
) {
}

Expand All @@ -38,11 +36,7 @@ public function isVendorLocked(ClassMethod $classMethod): bool
$methodName = $this->nodeNameResolver->getName($classMethod);

// has interface vendor lock? → better skip it, as PHPStan has access only to just analyzed classes
if ($this->hasParentInterfaceMethod($classReflection, $methodName)) {
return true;
}

return $this->hasClassMethodLockMatchingFileName($classReflection, $methodName, '/vendor/');
return $this->hasParentInterfaceMethod($classReflection, $methodName);
}

/**
Expand All @@ -60,37 +54,4 @@ private function hasParentInterfaceMethod(ClassReflection $classReflection, stri

return false;
}

private function hasClassMethodLockMatchingFileName(
ClassReflection $classReflection,
string $methodName,
string $filePathPartName
): bool {
$ancestorClassReflections = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];
foreach ($ancestorClassReflections as $ancestorClassReflection) {
// parent type
if (! $ancestorClassReflection->hasNativeMethod($methodName)) {
continue;
}

// is file in vendor?
$fileName = $ancestorClassReflection->getFileName();
// probably internal class
if ($fileName === null) {
continue;
}

// not conditions? its a match
if ($filePathPartName === '') {
return true;
}

$normalizedFileName = $this->filePathHelper->normalizePathAndSchema($fileName);
if (str_contains($normalizedFileName, $filePathPartName)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class CoverBareGetNodeTypes extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
}

public function getNodeTypes(): array
{
return [Node\Stmt\ClassMethod::class];
}

public function refactor(Node $node)
{
if (! $node instanceof Node\Stmt\ClassMethod) {
return null;
}
}
}

?>
-----
<?php

namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class CoverBareGetNodeTypes extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
}

public function getNodeTypes(): array
{
return [Node\Stmt\ClassMethod::class];
}

public function refactor(Node $node)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class SomeClass extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
}

public function getNodeTypes(): array
{
return [Node\Stmt\ClassMethod::class];
}

/**
* @param Node\Stmt\ClassMethod $node
*/
public function refactor(Node $node)
{
if (! $node instanceof Node\Stmt\ClassMethod) {
return null;
}
}
}

?>
-----
<?php

namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class SomeClass extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
}

public function getNodeTypes(): array
{
return [Node\Stmt\ClassMethod::class];
}

/**
* @param Node\Stmt\ClassMethod $node
*/
public function refactor(Node $node)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveRefactorDuplicatedNodeInstanceCheckRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Utils\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector;

return RectorConfig::configure()
->withRules([
RemoveRefactorDuplicatedNodeInstanceCheckRector::class,
]);
Loading