Skip to content
Open
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
32 changes: 30 additions & 2 deletions src/TestCaseForTypeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,47 @@ public function gatherAnalyserErrors(array $files): array
$ruleErrors = $rule->processNode($node, $scope);
foreach ($ruleErrors as $ruleError) {
if ($this->ignored($ruleError)) {
$this->ignoredErrors[] = $ruleErrorTransformer->transform($ruleError, $scope, [], $node);
$this->ignoredErrors[] = $this->callTransform($ruleErrorTransformer, $ruleError, $scope, $nodeType, $node);

continue;
}

$actualErrors[] = $ruleErrorTransformer->transform($ruleError, $scope, [], $node);
$actualErrors[] = $this->callTransform($ruleErrorTransformer, $ruleError, $scope, $nodeType, $node);
}
}
}

return $actualErrors;
}

/**
* Calls RuleErrorTransformer::transform() using the correct argument signature for the
* installed PHPStan version:
* - PHPStan 1.x: transform(RuleError, Scope, string $nodeType, int $nodeLine)
* - PHPStan 2.x: transform(RuleError, Scope, array $fileNodes, Node $node)
*/
private function callTransform(
RuleErrorTransformer $transformer,
RuleError $ruleError,
Scope $scope,
string $nodeType,
CollectedDataNode $node,
): Error {
static $isLegacySignature = null;
if ($isLegacySignature === null) {
$thirdParam = (new \ReflectionMethod($transformer, 'transform'))->getParameters()[2];
$isLegacySignature = (string) $thirdParam->getType() === 'string';
}

if ($isLegacySignature) {
// PHPStan 1.x: third arg is string $nodeType, fourth is int $nodeLine
return $transformer->transform($ruleError, $scope, $nodeType, $node->getLine()); // @phpstan-ignore-line
}

// PHPStan 2.x: third arg is array $fileNodes, fourth is Node $node
return $transformer->transform($ruleError, $scope, [], $node); // @phpstan-ignore-line
}

/**
* Check if ignored.
*/
Expand Down