Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:
composer install
OUTPUT=$(../bashunit -a exit_code "1" "vendor/bin/phpstan analyze test.php --error-format=raw")
echo "$OUTPUT"
../bashunit -a contains 'test.php:12:Version requirement will always evaluate to false.' "$OUTPUT"
../bashunit -a contains 'test.php:32:Version requirement will always evaluate to false.' "$OUTPUT"
../bashunit -a contains 'test.php:12:Version requirement <=8.0.0 does not match 8.1.0...8.5.99.' "$OUTPUT"
../bashunit -a contains 'test.php:32:Version requirement ^11.0.0 does not match 12.5.0...12.5.99.' "$OUTPUT"
steps:
- name: Harden the runner (Audit all outbound calls)
Expand Down
20 changes: 15 additions & 5 deletions src/Rules/PHPUnit/AttributeVersionRequirementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Php\PhpMinorVersionIterator;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function count;
use function is_numeric;
use function preg_match;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array

if ($this->warnAboutIncompleteVersion($versionRequirement)) {
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement is incomplete.'),
sprintf('Version requirement %s is incomplete.', $versionRequirement),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
Expand Down Expand Up @@ -129,8 +130,17 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
}
}

if (count($pharIoVersions) < 2) {
throw new ShouldNotHappenException();
}

$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement will always evaluate to false.'),
sprintf(
'Version requirement %s does not match %s...%s.',
$versionRequirement,
$pharIoVersions[0]->getVersionString(),
$pharIoVersions[count($pharIoVersions) - 1]->getVersionString(),
),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
Expand All @@ -140,7 +150,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array

if ($this->PHPUnitVersion->requiresPhpversionAttributeWithOperator()->yes()) {
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement is missing operator.'),
sprintf('Version requirement %s is missing operator.', $versionRequirement),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
Expand All @@ -149,7 +159,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
&& $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes()
) {
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement without operator is deprecated.'),
sprintf('Version requirement %s without operator is deprecated.', $versionRequirement),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
Expand All @@ -159,7 +169,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
}

/**
* @return Version[]
* @return list<Version>
*/
private function getAnalyzedPhpVersions(): array
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Rules/PHPUnit/AttributeRequiresPhpVersionRangeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public function testPhpVersionMismatch(): void
{
$this->analyse([__DIR__ . '/data/requires-php-version-mismatch.php'], [
[
'Version requirement will always evaluate to false.',
'Version requirement < 7.0 does not match 8.2.0...8.4.0.',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using a version range notation which github.com is also using. so I think it should be familiar to devs

20,
],
[
'Version requirement will always evaluate to false.',
'Version requirement ^5.0 does not match 8.2.0...8.4.0.',
28,
],
[
'Version requirement will always evaluate to false.',
'Version requirement ~5.0 does not match 8.2.0...8.4.0.',
36,
],
[
'Version requirement will always evaluate to false.',
'Version requirement 5.* does not match 8.2.0...8.4.0.',
44,
],
[
'Version requirement will always evaluate to false.',
'Version requirement 8.5.* does not match 8.2.0...8.4.0.',
76,
],
]);
Expand Down
26 changes: 13 additions & 13 deletions tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testRuleOnPHPUnit124DeprecationsOn(): void

$this->analyse([__DIR__ . '/data/requires-php-version.php'], [
[
'Version requirement without operator is deprecated.',
'Version requirement 8.0 without operator is deprecated.',
12,
],
]);
Expand All @@ -75,7 +75,7 @@ public function testRuleOnPHPUnit13(): void

$this->analyse([__DIR__ . '/data/requires-php-version.php'], [
[
'Version requirement is missing operator.',
'Version requirement 8.0 is missing operator.',
12,
],
]);
Expand All @@ -91,35 +91,35 @@ public function testPhpVersionMismatch(): void
[
// errors because https://github.com/sebastianbergmann/phpunit/issues/6451
// the test assumes PHP_VERSION_ID 80500 and the constraint only has 2 digits
'Version requirement will always evaluate to false.',
'Version requirement <= 8.5 does not match 8.5.0...8.5.99.',
12,
],
[
'Version requirement will always evaluate to false.',
'Version requirement < 7.0 does not match 8.5.0...8.5.99.',
20,
],
[
'Version requirement will always evaluate to false.',
'Version requirement ^5.0 does not match 8.5.0...8.5.99.',
28,
],
[
'Version requirement will always evaluate to false.',
'Version requirement ~5.0 does not match 8.5.0...8.5.99.',
36,
],
[
'Version requirement will always evaluate to false.',
'Version requirement 5.* does not match 8.5.0...8.5.99.',
44,
],
[
'Version requirement will always evaluate to false.',
'Version requirement <= 8.4 does not match 8.5.0...8.5.99.',
52,
],
[
'Version requirement will always evaluate to false.',
'Version requirement <= 8.5 does not match 8.5.0...8.5.99.',
60,
],
[
'Version requirement will always evaluate to false.',
'Version requirement 8.3.* does not match 8.5.0...8.5.99.',
68,
],
]);
Expand Down Expand Up @@ -158,11 +158,11 @@ public function testWarnAboutIncompleteVersion(): void

$this->analyse([__DIR__ . '/data/requires-php-version.php'], [
[
'Version requirement is incomplete.',
'Version requirement 8.0 is incomplete.',
12,
],
[
'Version requirement is incomplete.',
'Version requirement >=8.0 is incomplete.',
20,
],
]);
Expand All @@ -177,7 +177,7 @@ public function testWarnAboutIncompletePhpunitVersion(): void

$this->analyse([__DIR__ . '/data/requires-phpunit-version.php'], [
[
'Version requirement is incomplete.',
'Version requirement 11.0 is incomplete.',
12,
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function testWarnAboutIncompleteVersion(): void

$this->analyse([__DIR__ . '/data/requires-php-version-on-class.php'], [
[
'Version requirement will always evaluate to false.',
'Version requirement < 7.0 does not match 8.5.0...8.5.99.',
10,
],
[
'Version requirement is incomplete.',
'Version requirement < 7.0 is incomplete.',
10,
],
]);
Expand All @@ -44,7 +44,7 @@ public function testWarnAboutIncompletePhpunitVersion(): void

$this->analyse([__DIR__ . '/data/requires-phpunit-version.php'], [
[
'Version requirement is incomplete.',
'Version requirement >=11.0 is incomplete.',
18,
],
]);
Expand Down
Loading