Skip to content
Open
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
21 changes: 21 additions & 0 deletions src/Analyser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,27 @@ public function getFixedErrorDiff(): ?FixedErrorDiff
return $this->fixedErrorDiff;
}

/**
* @internal Experimental
*/
public function withFixedErrorDiff(FixedErrorDiff $fixedErrorDiff): self
{
return new self(
$this->message,
$this->file,
$this->line,
$this->canBeIgnored,
$this->filePath,
$this->traitFilePath,
$this->tip,
$this->nodeLine,
$this->nodeType,
$this->identifier,
$this->metadata,
$fixedErrorDiff,
);
}

/**
* @return mixed
*/
Expand Down
50 changes: 38 additions & 12 deletions src/Analyser/FileAnalyserCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\Rules\Registry as RuleRegistry;
use function array_keys;
use function get_class;
use function spl_object_id;
use function sprintf;

/**
Expand Down Expand Up @@ -45,6 +46,9 @@ final class FileAnalyserCallback
/** @var list<Error> */
private array $temporaryFileErrors = [];

/** @var array<string, list<PendingFix>> */
private array $pendingFixesByFile = [];

/** @var LinesToIgnore */
private array $linesToIgnore;

Expand Down Expand Up @@ -77,8 +81,6 @@ public function __construct(

public function __invoke(Node $node, Scope $scope): void
{
$parserNodes = $this->parserNodes;

/** @var Scope&NodeCallbackInvoker $scope */
if ($node instanceof Node\Stmt\Trait_) {
foreach (array_keys($this->linesToIgnore[$this->file] ?? []) as $lineToIgnore) {
Expand All @@ -101,14 +103,6 @@ public function __invoke(Node $node, Scope $scope): void
}
}

if ($scope->isInTrait()) {
$traitReflection = $scope->getTraitReflection();
if ($traitReflection->getFileName() !== null) {
$traitFilePath = $traitReflection->getFileName();
$parserNodes = $this->parser->parseFile($traitFilePath);
}
}

if ($this->outerNodeCallback !== null) {
($this->outerNodeCallback)($node, $scope);
}
Expand Down Expand Up @@ -149,7 +143,7 @@ public function __invoke(Node $node, Scope $scope): void
}

foreach ($ruleErrors as $ruleError) {
$error = $this->ruleErrorTransformer->transform($ruleError, $scope, $parserNodes, $node);
[$error, $pendingFix] = $this->ruleErrorTransformer->transformPreserveFixable($ruleError, $scope, $node);

if ($error->canBeIgnored()) {
foreach ($this->ignoreErrorExtensions as $ignoreErrorExtension) {
Expand All @@ -160,6 +154,11 @@ public function __invoke(Node $node, Scope $scope): void
}

$this->temporaryFileErrors[] = $error;
if ($pendingFix === null) {
continue;
}

$this->pendingFixesByFile[$pendingFix->fixingFilePath][] = $pendingFix;
}
}

Expand Down Expand Up @@ -305,7 +304,34 @@ public function getUnmatchedLineIgnores(): array
*/
public function getTemporaryFileErrors(): array
{
return $this->temporaryFileErrors;
if ($this->pendingFixesByFile === []) {
return $this->temporaryFileErrors;
}

$parserNodesByFile = [$this->file => $this->parserNodes];
foreach (array_keys($this->pendingFixesByFile) as $fixingFilePath) {
if (isset($parserNodesByFile[$fixingFilePath])) {
continue;
}
$parserNodesByFile[$fixingFilePath] = $this->parser->parseFile($fixingFilePath);
}

$diffsByErrorId = $this->ruleErrorTransformer->finalizePendingFixes(
$this->pendingFixesByFile,
$parserNodesByFile,
);

if ($diffsByErrorId === []) {
return $this->temporaryFileErrors;
}

$finalErrors = [];
foreach ($this->temporaryFileErrors as $error) {
$diff = $diffsByErrorId[spl_object_id($error)] ?? null;
$finalErrors[] = $diff !== null ? $error->withFixedErrorDiff($diff) : $error;
}

return $finalErrors;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Analyser/PendingFix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use Closure;
use PhpParser\Node;

/**
* @internal
*/
final class PendingFix
{

/**
* @param Closure(Node): Node $newNodeCallable
*/
public function __construct(
public readonly Error $error,
public readonly Node $originalNode,
public readonly Closure $newNodeCallable,
public readonly string $fixingFilePath,
)
{
}

}
Loading
Loading