Skip to content

Some #[RequiresPhp] errors are autofixable - #362

Merged
staabm merged 7 commits into
phpstan:2.1.xfrom
staabm:auto-vers
Nov 27, 2026
Merged

staabm merged 7 commits into
phpstan:2.1.xfrom
staabm:auto-vers

Conversation

@staabm

@staabm staabm commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

closes #309

@staabm
staabm marked this pull request as ready for review November 25, 2026 15:28
@staabm

staabm commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

//cc @SanderMuller

@SanderMuller

Copy link
Copy Markdown
Contributor

The fixes for '>=8.0', '>= 8', '>=11' and '^8.0' are correct. The fix for a version without an operator changes which tests run.

The PR completes '8' and '8.0' to '>=8.0.0'. PHPUnit does not read them as >=. In 10.5, 11.5 and 12.5, Requirement::from() first uses phar-io's VersionConstraintParser, which reads 8 as 8.* and 8.0 as 8.0.*. PHPUnit uses the >= default only when phar-io rejects the string. PHPUnit 13.5 ignores a version without an operator and emits a warning.

PHPUnit 12.5.45 on PHP 8.5.13:

#[RequiresPhp('8.0')]      skipped
#[RequiresPhp('>=8.0.0')]  runs
#[RequiresPhp('7')]        skipped
#[RequiresPhp('>=7.0.0')]  runs

On 12.x, the fix that testFixVersionWithoutOperator expects (PHPUnit 12.4, '8.0' to '>=8.0.0') runs a test that PHPUnit skips today. On 13.x, a requirement that PHPUnit ignores becomes active. The user may want either result, but the meaning is not the same, and the docblock of fixVersionRequirement() says that it is.

These forms keep the 10.x to 12.x meaning: '8' to '^8.0.0', and '8.0' to '~8.0.0' (or '8.0.*'). phar-io accepts the same versions for each pair from 7.4.33 to 12.0.0. PHPUnit 12.5 runs '^8.0.0', skips '~8.0.0', and reports no deprecation or warning for either. The other option is to fix only the requirements that have an operator.

What I checked:

  • '>=8.0', '>= 8', '>=11' and '^8.0' accept the same versions as their fixed forms, from 7.4.33 to 12.0.0.
  • The three new fix tests fail without the src change and pass with it.
  • make tests, make phpstan, lint, and phpcs on the changed files pass locally.
  • Performance: I analysed the 39 files in tests/Rules/PHPUnit/data with the extension and its rules, with the result cache cleared before each run. 2.1.x takes 445 ± 12 ms and the PR takes 417 ± 5 ms (hyperfine, 10 runs). There is no slowdown.

I read the PHPUnit 13.5 behaviour in its AttributeParser but did not run it. I did not check PHPUnit 13.0 to 13.4.

@staabm

staabm commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@SanderMuller please have another look

@staabm

staabm commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

The PR completes '8' and '8.0' to '>=8.0.0'. PHPUnit does not read them as >=. In 10.5, 11.5 and 12.5, Requirement::from() first uses phar-io's VersionConstraintParser, which reads 8 as 8.* and 8.0 as 8.0.*. PHPUnit uses the >= default only when phar-io rejects the string. PHPUnit 13.5 ignores a version without an operator and emits a warning.

fixed

@SanderMuller

Copy link
Copy Markdown
Contributor

Thanks, that fixes it. The fix now applies only to '>=8.0', '>= 8', '^8.0' and '>=11', which keep their meaning. A version without an operator stays as it is.

One small thing: the new comment says that PHPUnit does not support named arguments, but it does. With PHPUnit 11.5.58 and 12.5.45, #[RequiresPhp(versionRequirement: '>=99.0.0')] skips the test and versionRequirement: '>=8.0.0' runs it, the same as the positional form. So the code is right to check named arguments. Only the comment in AttributeVersionRequirementHelper and in requires-php-version-named-argument.php is wrong.

The tests, PHPStan, lint and phpcs pass locally. The fix tests fail without the src change. testNamedArgument passes without it too, because 2.1.x reads named arguments since #365.

The machine was busy during the timing (load average about 25), so I compare user CPU, not wall time. Analysing the 42 fixture files in tests/Rules/PHPUnit/data takes 378.8 ms of user CPU on 2.1.x and 378.3 ms on the PR (hyperfine, 10 runs).

@staabm

staabm commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

One small thing: the new comment says that PHPUnit does not support named arguments, but it does. With PHPUnit 11.5.58 and 12.5.45, #[RequiresPhp(versionRequirement: '>=99.0.0')] skips the test and versionRequirement: '>=8.0.0' runs it, the same as the positional form. So the code is right to check named arguments. Only the comment in AttributeVersionRequirementHelper and in requires-php-version-named-argument.php is wrong.

PHPUnit declares its classes as no-named-arguments

@staabm
staabm merged commit 8c16263 into phpstan:2.1.x Nov 27, 2026
114 checks passed
@staabm
staabm deleted the auto-vers branch November 27, 2026 14:09
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.

autofix incomplete version constraints

3 participants