Skip to content

Commit

Permalink
Adding collectors - step 2 (#5042)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 18, 2023
1 parent 7e55eaa commit e084356
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 24 deletions.
10 changes: 10 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Config;

use Illuminate\Container\Container;
use PHPStan\Collectors\Collector;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
Expand Down Expand Up @@ -220,6 +221,15 @@ static function (
SimpleParameterProvider::addParameter(Option::REGISTERED_RECTOR_RULES, $rectorClass);
}

/**
* @param class-string<Collector> $collectorClass
*/
public function collector(string $collectorClass): void
{
$this->singleton($collectorClass);
$this->tag($collectorClass, Collector::class);
}

public function import(string $filePath): void
{
if (str_contains($filePath, '*')) {
Expand Down
16 changes: 13 additions & 3 deletions packages/Parallel/Application/ParallelFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Console\Command\ProcessCommand;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\ProcessResult;
use Rector\Core\ValueObject\Reporting\FileDiff;
Expand Down Expand Up @@ -65,7 +66,8 @@ public function process(
Schedule $schedule,
string $mainScript,
callable $postFileCallback,
InputInterface $input
InputInterface $input,
Configuration $configuration
): ProcessResult {
$jobs = array_reverse($schedule->getJobs());
$streamSelectLoop = new StreamSelectLoop();
Expand All @@ -87,11 +89,19 @@ public function process(
$tcpServer = new TcpServer('127.0.0.1:0', $streamSelectLoop);
$this->processPool = new ProcessPool($tcpServer);

$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (&$jobs): void {
$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (
&$jobs,
$configuration
): void {
$inDecoder = new Decoder($connection, true, 512, 0, 4 * 1024 * 1024);
$outEncoder = new Encoder($connection);

$inDecoder->on(ReactEvent::DATA, function (array $data) use (&$jobs, $inDecoder, $outEncoder): void {
$inDecoder->on(ReactEvent::DATA, function (array $data) use (
&$jobs,
$inDecoder,
$outEncoder,
$configuration
): void {
$action = $data[ReactCommand::ACTION];
if ($action !== Action::HELLO) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @api
* Helpers constant for passing constant names around
*/
final class Name
final class BridgeItem
{
/**
* @var string
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,10 @@ parameters:
-
message: '#Parameter \#1 \$useType of callable callable\(0\|1\|2\|3, PhpParser\\Node\\Stmt\\UseUse, string\): void expects 0\|1\|2\|3, int given.#'
path: rules/CodingStyle/ClassNameImport/UseImportsTraverser.php

# rector collectors
- '#Creating new PHPStan\\Collectors\\CollectedData is not covered by backward compatibility promise\. The class might change in a minor PHPStan version#'
-
message: '#Anonymous function has an unused use \$configuration#'
path: packages/Parallel/Application/ParallelFileProcessor.php

5 changes: 3 additions & 2 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function run(Configuration $configuration, InputInterface $input): Proces
}

if ($configuration->isParallel()) {
$processResult = $this->runParallel($filePaths, $input, $postFileCallback);
$processResult = $this->runParallel($filePaths, $configuration, $input, $postFileCallback);
} else {
$processResult = $this->processFiles($filePaths, $configuration, $preFileCallback, $postFileCallback);
}
Expand Down Expand Up @@ -224,6 +224,7 @@ private function restoreErrorHandler(): void
*/
private function runParallel(
array $filePaths,
Configuration $configuration,
InputInterface $input,
callable $postFileCallback
): ProcessResult {
Expand All @@ -240,7 +241,7 @@ private function runParallel(
}

// mimics see https://github.com/phpstan/phpstan-src/commit/9124c66dcc55a222e21b1717ba5f60771f7dda92#diff-387b8f04e0db7a06678eb52ce0c0d0aff73e0d7d8fc5df834d0a5fbec198e5daR139
return $this->parallelFileProcessor->process($schedule, $mainScript, $postFileCallback, $input);
return $this->parallelFileProcessor->process($schedule, $mainScript, $postFileCallback, $input, $configuration);
}

/**
Expand Down
73 changes: 73 additions & 0 deletions src/Application/Collector/CollectorNodeVisitor.php
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;
}
}
41 changes: 41 additions & 0 deletions src/Application/Collector/CollectorProcessor.php
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();
}
}
18 changes: 9 additions & 9 deletions src/ValueObject/Error/SystemError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\Core\ValueObject\Error;

use Rector\Parallel\ValueObject\Name;
use Rector\Parallel\ValueObject\BridgeItem;
use Symplify\EasyParallel\Contract\SerializableInterface;

final class SystemError implements SerializableInterface
Expand Down Expand Up @@ -43,10 +43,10 @@ public function getRelativeFilePath(): ?string
public function jsonSerialize(): array
{
return [
Name::MESSAGE => $this->message,
Name::RELATIVE_FILE_PATH => $this->relativeFilePath,
Name::LINE => $this->line,
Name::RECTOR_CLASS => $this->rectorClass,
BridgeItem::MESSAGE => $this->message,
BridgeItem::RELATIVE_FILE_PATH => $this->relativeFilePath,
BridgeItem::LINE => $this->line,
BridgeItem::RECTOR_CLASS => $this->rectorClass,
];
}

Expand All @@ -56,10 +56,10 @@ public function jsonSerialize(): array
public static function decode(array $json): self
{
return new self(
$json[Name::MESSAGE],
$json[Name::RELATIVE_FILE_PATH],
$json[Name::LINE],
$json[Name::RECTOR_CLASS]
$json[BridgeItem::MESSAGE],
$json[BridgeItem::RELATIVE_FILE_PATH],
$json[BridgeItem::LINE],
$json[BridgeItem::RECTOR_CLASS]
);
}

Expand Down
18 changes: 9 additions & 9 deletions src/ValueObject/Reporting/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Nette\Utils\Strings;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Parallel\ValueObject\Name;
use Rector\Parallel\ValueObject\BridgeItem;
use Symplify\EasyParallel\Contract\SerializableInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -91,10 +91,10 @@ public function getFirstLineNumber(): ?int
public function jsonSerialize(): array
{
return [
Name::RELATIVE_FILE_PATH => $this->relativeFilePath,
Name::DIFF => $this->diff,
Name::DIFF_CONSOLE_FORMATTED => $this->diffConsoleFormatted,
Name::RECTORS_WITH_LINE_CHANGES => $this->rectorsWithLineChanges,
BridgeItem::RELATIVE_FILE_PATH => $this->relativeFilePath,
BridgeItem::DIFF => $this->diff,
BridgeItem::DIFF_CONSOLE_FORMATTED => $this->diffConsoleFormatted,
BridgeItem::RECTORS_WITH_LINE_CHANGES => $this->rectorsWithLineChanges,
];
}

Expand All @@ -105,14 +105,14 @@ public static function decode(array $json): self
{
$rectorWithLineChanges = [];

foreach ($json[Name::RECTORS_WITH_LINE_CHANGES] as $rectorWithLineChangesJson) {
foreach ($json[BridgeItem::RECTORS_WITH_LINE_CHANGES] as $rectorWithLineChangesJson) {
$rectorWithLineChanges[] = RectorWithLineChange::decode($rectorWithLineChangesJson);
}

return new self(
$json[Name::RELATIVE_FILE_PATH],
$json[Name::DIFF],
$json[Name::DIFF_CONSOLE_FORMATTED],
$json[BridgeItem::RELATIVE_FILE_PATH],
$json[BridgeItem::DIFF],
$json[BridgeItem::DIFF_CONSOLE_FORMATTED],
$rectorWithLineChanges,
);
}
Expand Down

0 comments on commit e084356

Please sign in to comment.