Skip to content

Commit 760d867

Browse files
committed
Don't auto-fix versions without operator, as this can change meaning when tests will run
1 parent 8c77529 commit 760d867

5 files changed

Lines changed: 21 additions & 68 deletions

‎src/Rules/PHPUnit/AttributeVersionRequirementHelper.php‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ final class AttributeVersionRequirementHelper
2929
{
3030

3131
/**
32-
* Matches requirements whose version can be completed and given an explicit `>=` operator
33-
* without changing their meaning, e.g. `8`, `8.1`, `>=8.1` or `^8.1`.
32+
* Matches requirements whose version can be completed to major.minor.patch
33+
* without changing their meaning, e.g. `>=8`, `>=8.1` or `^8.1`.
34+
* Versions without an operator are deliberately not matched.
3435
*/
35-
private const FIXABLE_VERSION_REQUIREMENT = '/^(?P<operator>>=|\^)?(?P<space>\s*)(?P<version>\d+(?:\.\d+){0,2})$/';
36+
private const FIXABLE_VERSION_REQUIREMENT = '/^(?P<operator>>=|\^)(?P<space>\s*)(?P<version>\d+(?:\.\d+){0,2})$/';
3637

3738
private const VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m";
3839

@@ -183,20 +184,20 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc
183184
}
184185

185186
if ($this->PHPUnitVersion->requiresPhpversionAttributeWithOperator()->yes()) {
186-
$errorBuilder = RuleErrorBuilder::message(
187+
$errors[] = RuleErrorBuilder::message(
187188
sprintf('Version requirement %s is missing operator.', $versionRequirement),
188189
)
189-
->identifier('phpunit.attributeRequiresPhpVersion');
190-
$errors[] = $this->buildError($errorBuilder, $versionNode);
190+
->identifier('phpunit.attributeRequiresPhpVersion')
191+
->build();
191192
} elseif (
192193
$this->deprecationRulesInstalled
193194
&& $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes()
194195
) {
195-
$errorBuilder = RuleErrorBuilder::message(
196+
$errors[] = RuleErrorBuilder::message(
196197
sprintf('Version requirement %s without operator is deprecated.', $versionRequirement),
197198
)
198-
->identifier('phpunit.attributeRequiresPhpVersion');
199-
$errors[] = $this->buildError($errorBuilder, $versionNode);
199+
->identifier('phpunit.attributeRequiresPhpVersion')
200+
->build();
200201
}
201202
}
202203
return $errors;
@@ -262,22 +263,22 @@ private function buildError(RuleErrorBuilder $errorBuilder, ?String_ $versionNod
262263
}
263264

264265
/**
265-
* Completes the version to major.minor.patch and adds the implicit `>=` operator.
266+
* Completes the version to major.minor.patch.
266267
* Only applied where the meaning of the requirement stays the same.
268+
* Versions without an operator are not fixed.
267269
*/
268270
private function fixVersionRequirement(string $versionRequirement): ?string
269271
{
270272
if (preg_match(self::FIXABLE_VERSION_REQUIREMENT, $versionRequirement, $matches) !== 1) {
271273
return null;
272274
}
273275

274-
$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';
275276
$versionParts = explode('.', $matches['version']);
276277
while (count($versionParts) < 3) {
277278
$versionParts[] = '0';
278279
}
279280

280-
return $operator . $matches['space'] . implode('.', $versionParts);
281+
return $matches['operator'] . $matches['space'] . implode('.', $versionParts);
281282
}
282283

283284
/**

‎tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ public function testFixIncompleteVersion(): void
202202
$this->fix(__DIR__ . '/data/requires-php-version-fixable.php', __DIR__ . '/data/requires-php-version-fixable.php.fixed');
203203
}
204204

205-
public function testFixVersionWithoutOperator(): void
205+
public function testDoNotFixVersionWithoutOperator(): void
206206
{
207207
$this->phpunitMajorVersion = 12;
208208
$this->phpunitMinorVersion = 4;
209209
$this->deprecationRulesInstalled = true;
210210

211-
$this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php.fixed');
211+
$this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php');
212212
}
213213

214214
public function testNamedArgument(): void
@@ -237,7 +237,7 @@ public function testFixNamedArgument(): void
237237
$this->deprecationRulesInstalled = false;
238238
$this->warnAboutIncompleteVersion = true;
239239

240-
$this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php.fixed');
240+
$this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php');
241241
}
242242

243243
protected function getRule(): Rule

‎tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use PHPUnit\Framework\Attributes\RequiresPhpunit;
88

99
class FixableTest extends TestCase
1010
{
11-
#[RequiresPhp('>=8.0.0')]
11+
#[RequiresPhp('8')]
1212
public function testMajorOnly(): void {
1313

1414
}
1515

16-
#[RequiresPhp('>=8.0.0')]
16+
#[RequiresPhp('8.0')]
1717
public function testMissingOperator(): void {
1818

1919
}
@@ -39,19 +39,19 @@ class FixableTest extends TestCase
3939

4040
}
4141

42-
#[RequiresPhp(">=8.0.0")]
42+
#[RequiresPhp("8.0")]
4343
public function testDoubleQuoted(): void {
4444

4545
}
4646

47-
#[RequiresPhp('>=8.0.0')]
47+
#[RequiresPhp('8.0')]
4848
#[RequiresPhpunit('>=11.0.0')]
4949
public function testMultipleAttributes(): void {
5050

5151
}
5252

5353
#[RequiresPhp('>= 8.0.0')]
54-
#[RequiresPhp('>=8.1.0')]
54+
#[RequiresPhp('8.1')]
5555
public function testRepeatedAttribute(): void {
5656

5757
}

‎tests/Rules/PHPUnit/data/requires-php-version-named-argument.php.fixed‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎tests/Rules/PHPUnit/data/requires-php-version.php.fixed‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)