-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decopule helper template/mixin services from PHPStanNodeScopeResolver (…
…#1067) Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
ceccbad
commit e060d00
Showing
12 changed files
with
198 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/NodeTypeResolver/PHPStan/CollisionGuard/MixinGuard.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\PHPStan\CollisionGuard; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
|
||
final class MixinGuard | ||
{ | ||
public function __construct( | ||
private BetterNodeFinder $betterNodeFinder, | ||
private ReflectionProvider $reflectionProvider, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Stmt[] $stmts | ||
*/ | ||
public function containsMixinPhpDoc(array $stmts): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findFirst($stmts, function (Node $node): bool { | ||
if (! $node instanceof FullyQualified && ! $node instanceof Class_) { | ||
return false; | ||
} | ||
|
||
if ($node instanceof Class_ && $node->isAnonymous()) { | ||
return false; | ||
} | ||
|
||
$className = $node instanceof FullyQualified ? $node->toString() : $node->namespacedName->toString(); | ||
|
||
return $this->isCircularMixin($className); | ||
}); | ||
} | ||
|
||
private function isCircularMixin(string $className): bool | ||
{ | ||
// fix error in parallel test | ||
// use function_exists on purpose as using reflectionProvider broke the test in parallel | ||
if (function_exists($className)) { | ||
return false; | ||
} | ||
|
||
$hasClass = $this->reflectionProvider->hasClass($className); | ||
|
||
if (! $hasClass) { | ||
return false; | ||
} | ||
|
||
$classReflection = $this->reflectionProvider->getClass($className); | ||
if ($classReflection->isBuiltIn()) { | ||
return false; | ||
} | ||
|
||
foreach ($classReflection->getMixinTags() as $mixinTag) { | ||
$type = $mixinTag->getType(); | ||
if (! $type instanceof ObjectType) { | ||
return false; | ||
} | ||
|
||
if ($type->getClassName() === $className) { | ||
return true; | ||
} | ||
|
||
if ($this->isCircularMixin($type->getClassName())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
packages/NodeTypeResolver/PHPStan/CollisionGuard/TemplateExtendsGuard.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\PHPStan\CollisionGuard; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt; | ||
use Rector\Core\Application\FileProcessor; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use ReflectionClass; | ||
use Symplify\SmartFileSystem\SmartFileSystem; | ||
|
||
final class TemplateExtendsGuard | ||
{ | ||
public function __construct( | ||
private BetterNodeFinder $betterNodeFinder, | ||
private SmartFileSystem $smartFileSystem, | ||
) { | ||
} | ||
|
||
/** | ||
* Thist needs to be checked early before `@mixin` check as | ||
* ReflectionProvider already hang when check class with `@template-extends` | ||
* | ||
* @see https://github.com/phpstan/phpstan/issues/3865 in PHPStan | ||
* @param Stmt[] $nodes | ||
*/ | ||
public function containsTemplateExtendsPhpDoc(array $nodes, string $currentFileName): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $node) use ($currentFileName): bool { | ||
if (! $node instanceof FullyQualified) { | ||
return false; | ||
} | ||
|
||
$className = $node->toString(); | ||
|
||
// fix error in parallel test | ||
// use function_exists on purpose as using reflectionProvider broke the test in parallel | ||
if (function_exists($className)) { | ||
return false; | ||
} | ||
|
||
// use class_exists as PHPStan ReflectionProvider hang on check className with `@template-extends` | ||
if (! class_exists($className)) { | ||
return false; | ||
} | ||
|
||
// use native ReflectionClass as PHPStan ReflectionProvider hang on check className with `@template-extends` | ||
$reflectionClass = new ReflectionClass($className); | ||
if ($reflectionClass->isInternal()) { | ||
return false; | ||
} | ||
|
||
$fileName = (string) $reflectionClass->getFileName(); | ||
if (! $this->smartFileSystem->exists($fileName)) { | ||
return false; | ||
} | ||
|
||
// already checked in FileProcessor::parseFileInfoToLocalCache() | ||
if ($fileName === $currentFileName) { | ||
return false; | ||
} | ||
|
||
$fileContents = $this->smartFileSystem->readFile($fileName); | ||
return (bool) Strings::match($fileContents, FileProcessor::TEMPLATE_EXTENDS_REGEX); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.