Skip to content

fix: support both PHPStan 1.x and 2.x signatures in gatherAnalyserErrors()#60

Open
herpaderpaldent wants to merge 1 commit into
pestphp:4.xfrom
herpaderpaldent:fix/rule-error-transformer-args
Open

fix: support both PHPStan 1.x and 2.x signatures in gatherAnalyserErrors()#60
herpaderpaldent wants to merge 1 commit into
pestphp:4.xfrom
herpaderpaldent:fix/rule-error-transformer-args

Conversation

@herpaderpaldent
Copy link
Copy Markdown

Problem

gatherAnalyserErrors() in TestCaseForTypeCoverage calls
RuleErrorTransformer::transform() with arguments that only match the PHPStan 2.x signature, causing a fatal TypeError for PHPStan 1.x users:

TypeError: PHPStan\Analyser\RuleErrorTransformer::transform(): Argument #3 ($nodeType)
must be of type string, array given, called in
.../pest-plugin-type-coverage/src/TestCaseForTypeCoverage.php on line 121

The two signatures differ between major versions:

Version Arg 3 Arg 4
PHPStan 1.x string $nodeType int $nodeLine
PHPStan 2.x array $fileNodes Node $node

The previous code passed ([], $node) which satisfies PHPStan 2.x but throws a fatal TypeError on PHPStan 1.x because $nodeType must be string, not array.

Fix

Add a private callTransform() helper that uses reflection (result cached via a static variable, so the check is done only once per process) to detect the installed PHPStan version's signature and dispatch to the correct call.

  • PHPStan 1.x path: transform($ruleError, $scope, $nodeType, $node->getLine())
  • PHPStan 2.x path: transform($ruleError, $scope, [], $node)

This fixes --type-coverage for PHPStan 1.x users without breaking PHPStan 2.x.

…ors()

PHPStan 1.x: RuleErrorTransformer::transform() expects (string $nodeType, int $nodeLine)
PHPStan 2.x: RuleErrorTransformer::transform() expects (array $fileNodes, Node $node)

The previous code passed ([], $node) which works on PHPStan 2.x but throws a
fatal TypeError on PHPStan 1.x because $nodeType must be string, not array.

Add a private callTransform() helper that uses reflection (cached via static
variable) to detect the installed PHPStan version's signature and dispatch to
the correct call, fixing --type-coverage for PHPStan 1.x users without
breaking PHPStan 2.x.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant