Skip to content

Commit 9322210

Browse files
authored
Add PhpUnitRequiresConstraintFixer (#1009)
1 parent c75deee commit 9322210

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

33
## v3.24.0
4-
- Add PhpUnitRequiresExplicitConstraintFixer
4+
- Add PhpUnitRequiresConstraintFixer
55

66
## v3.23.0
77
- Add ClassConstantUsageFixer

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3607-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3610-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)
@@ -463,14 +463,20 @@ PHPUnit `fail`, `markTestIncomplete` and `markTestSkipped` functions must not be
463463
}
464464
```
465465

466-
#### PhpUnitRequiresExplicitConstraintFixer
467-
Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint.
466+
#### PhpUnitRequiresConstraintFixer
467+
Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint and space after comparison operator.
468468
```diff
469469
<?php
470-
class FooTest extends TestCase {
470+
class MyTest extends TestCase {
471+
/**
472+
- * @requires PHP 8.1
473+
+ * @requires PHP >= 8.1
474+
*/
475+
public function testFoo() {}
476+
471477
/**
472-
- * @requires PHP 8.4
473-
+ * @requires PHP >= 8.4
478+
- * @requires PHP <8.3
479+
+ * @requires PHP < 8.3
474480
*/
475481
public function testBar() {}
476482

src/Fixer/PhpUnitRequiresExplicitConstraintFixer.php renamed to src/Fixer/PhpUnitRequiresConstraintFixer.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@
2323
use PhpCsFixer\Tokenizer\Token;
2424
use PhpCsFixer\Tokenizer\Tokens;
2525

26-
final class PhpUnitRequiresExplicitConstraintFixer extends AbstractFixer
26+
final class PhpUnitRequiresConstraintFixer extends AbstractFixer
2727
{
2828
public function getDefinition(): FixerDefinitionInterface
2929
{
3030
return new FixerDefinition(
31-
'Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint.',
31+
'Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint and space after comparison operator.',
3232
[new CodeSample(
3333
<<<'PHP'
3434
<?php
35-
class FooTest extends TestCase {
35+
class MyTest extends TestCase {
3636
/**
37-
* @requires PHP 8.4
37+
* @requires PHP 8.1
38+
*/
39+
public function testFoo() {}
40+
41+
/**
42+
* @requires PHP <8.3
3843
*/
3944
public function testBar() {}
4045
@@ -115,11 +120,8 @@ static function (array $matches): string {
115120
\assert(\is_string($matches[1]));
116121
\assert(\is_string($matches[2]));
117122
\assert(\is_string($matches[3]));
118-
if (Preg::match('/^[\\d\\.-]+(dev|(RC|alpha|beta)[\\d\\.])?$/', $matches[2])) {
119-
$matches[2] = '>= ' . $matches[2];
120-
}
121123

122-
return $matches[1] . $matches[2] . $matches[3];
124+
return $matches[1] . self::fixString($matches[2]) . $matches[3];
123125
},
124126
$tokens[$index]->getContent(),
125127
),
@@ -161,9 +163,9 @@ private static function fixAttribute(Tokens $tokens, int $index): void
161163
private static function fixString(string $string): string
162164
{
163165
if (Preg::match('/^[\\d\\.-]+(dev|(RC|alpha|beta)[\\d\\.])?$/', $string)) {
164-
return '>= ' . $string;
166+
$string = '>=' . $string;
165167
}
166168

167-
return $string;
169+
return Preg::replace('/^([<>=!]{0,2})\\s*([\\d\\.-]+(dev|(RC|alpha|beta)[\\d\\.])?)$/', '$1 $2', $string);
168170
}
169171
}

tests/Fixer/PhpUnitRequiresExplicitConstraintFixerTest.php renamed to tests/Fixer/PhpUnitRequiresConstraintFixerTest.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
/**
1515
* @internal
1616
*
17-
* @covers \PhpCsFixerCustomFixers\Fixer\PhpUnitRequiresExplicitConstraintFixer
17+
* @covers \PhpCsFixerCustomFixers\Fixer\PhpUnitRequiresConstraintFixer
1818
*/
19-
final class PhpUnitRequiresExplicitConstraintFixerTest extends AbstractFixerTestCase
19+
final class PhpUnitRequiresConstraintFixerTest extends AbstractFixerTestCase
2020
{
2121
public function testIsRisky(): void
2222
{
@@ -224,6 +224,21 @@ public function testFoo(): void {}
224224
public function testFoo(): void {}
225225
}',
226226
];
227+
228+
yield 'attributes with comparison constraints' => [
229+
'<?php class FooTest extends TestCase {
230+
#[\\LeaveMeAlone("1.2")]
231+
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhp("!= 8.2")]
232+
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhpunit("< 11")]
233+
public function testFoo(): void {}
234+
}',
235+
'<?php class FooTest extends TestCase {
236+
#[\\LeaveMeAlone("1.2")]
237+
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhp("!=8.2")]
238+
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhpunit("<11")]
239+
public function testFoo(): void {}
240+
}',
241+
];
227242
}
228243

229244
/**
@@ -233,7 +248,7 @@ private static function getFixCases(): iterable
233248
{
234249
yield 'no PHPDoc' => [''];
235250

236-
yield 'single annotation' => [
251+
yield 'single annotation without operator' => [
237252
'/**
238253
* @requires PHP >= 8.4
239254
*/',
@@ -242,6 +257,15 @@ private static function getFixCases(): iterable
242257
*/',
243258
];
244259

260+
yield 'single annotation without space' => [
261+
'/**
262+
* @requires PHP >= 8.4
263+
*/',
264+
'/**
265+
* @requires PHP >=8.4
266+
*/',
267+
];
268+
245269
yield 'annotation with trailing spaces' => [
246270
'/**
247271
* @requires PHP >= 8.4 ' . '
@@ -253,12 +277,12 @@ private static function getFixCases(): iterable
253277

254278
yield 'multiple annotations' => [
255279
'/**
256-
* @requires PHP >= 7
257-
* @requires PHPUnit >= 11
280+
* @requires PHP > 7
281+
* @requires PHPUnit < 12
258282
*/',
259283
'/**
260-
* @requires PHP 7
261-
* @requires PHPUnit 11
284+
* @requires PHP >7
285+
* @requires PHPUnit < 12
262286
*/',
263287
];
264288
}

0 commit comments

Comments
 (0)