-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e55eaa
commit e084356
Showing
9 changed files
with
166 additions
and
24 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
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
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
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
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
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Application\Collector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\CollectedData; | ||
use PHPStan\Collectors\Registry; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Throwable; | ||
|
||
/** | ||
* @api | ||
* @see Mimics https://github.com/phpstan/phpstan-src/commit/bccd1cd58e0d117ac8755ab228e338d966cac25a | ||
*/ | ||
final class CollectorNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @var CollectedData[] | ||
*/ | ||
private array $collectedDatas = []; | ||
|
||
public function __construct( | ||
private readonly Registry $collectorRegistry | ||
) { | ||
} | ||
|
||
/** | ||
* @param Node[] $nodes | ||
*/ | ||
public function beforeTraverse(array $nodes): ?array | ||
{ | ||
$this->collectedDatas = []; | ||
return null; | ||
} | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
$collectors = $this->collectorRegistry->getCollectors($node::class); | ||
|
||
foreach ($collectors as $collector) { | ||
/** @var Scope $scope */ | ||
$scope = $node->getAttribute(AttributeKey::SCOPE); | ||
|
||
try { | ||
$collectedData = $collector->processNode($node, $scope); | ||
} catch (Throwable) { | ||
// nothing to collect | ||
continue; | ||
} | ||
|
||
// no data collected | ||
if ($collectedData === null) { | ||
continue; | ||
} | ||
|
||
$this->collectedDatas[] = new CollectedData($collectedData, $scope->getFile(), $collector::class); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @return CollectedData[] | ||
*/ | ||
public function getCollectedDatas(): array | ||
{ | ||
return $this->collectedDatas; | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Application\Collector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeTraverser; | ||
use PHPStan\Collectors\CollectedData; | ||
use PHPStan\Collectors\Registry; | ||
|
||
/** | ||
* @api | ||
*/ | ||
final class CollectorProcessor | ||
{ | ||
private readonly NodeTraverser $nodeTraverser; | ||
|
||
private readonly CollectorNodeVisitor $collectorNodeVisitor; | ||
|
||
public function __construct( | ||
Registry $collectorRegistry | ||
) { | ||
$nodeTraverser = new NodeTraverser(); | ||
|
||
$this->collectorNodeVisitor = new CollectorNodeVisitor($collectorRegistry); | ||
$nodeTraverser->addVisitor($this->collectorNodeVisitor); | ||
|
||
$this->nodeTraverser = $nodeTraverser; | ||
} | ||
|
||
/** | ||
* @param Node[] $stmts | ||
* @return CollectedData[] | ||
*/ | ||
public function process(array $stmts): array | ||
{ | ||
$this->nodeTraverser->traverse($stmts); | ||
return $this->collectorNodeVisitor->getCollectedDatas(); | ||
} | ||
} |
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
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