Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CHANGELOG for PHP CS Fixer: custom fixers

## v3.24.0
- Add PhpUnitRequiresExplicitConstraintFixer
- Add PhpUnitRequiresConstraintFixer

## v3.23.0
- Add ClassConstantUsageFixer
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![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)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3607-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3610-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![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)
Expand Down Expand Up @@ -463,14 +463,20 @@ PHPUnit `fail`, `markTestIncomplete` and `markTestSkipped` functions must not be
}
```

#### PhpUnitRequiresExplicitConstraintFixer
Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint.
#### PhpUnitRequiresConstraintFixer
Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint and space after comparison operator.
```diff
<?php
class FooTest extends TestCase {
class MyTest extends TestCase {
/**
- * @requires PHP 8.1
+ * @requires PHP >= 8.1
*/
public function testFoo() {}

/**
- * @requires PHP 8.4
+ * @requires PHP >= 8.4
- * @requires PHP <8.3
+ * @requires PHP < 8.3
*/
public function testBar() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class PhpUnitRequiresExplicitConstraintFixer extends AbstractFixer
final class PhpUnitRequiresConstraintFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint.',
'Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint and space after comparison operator.',
[new CodeSample(
<<<'PHP'
<?php
class FooTest extends TestCase {
class MyTest extends TestCase {
/**
* @requires PHP 8.4
* @requires PHP 8.1
*/
public function testFoo() {}

/**
* @requires PHP <8.3
*/
public function testBar() {}

Expand Down Expand Up @@ -115,11 +120,8 @@ static function (array $matches): string {
\assert(\is_string($matches[1]));
\assert(\is_string($matches[2]));
\assert(\is_string($matches[3]));
if (Preg::match('/^[\\d\\.-]+(dev|(RC|alpha|beta)[\\d\\.])?$/', $matches[2])) {
$matches[2] = '>= ' . $matches[2];
}

return $matches[1] . $matches[2] . $matches[3];
return $matches[1] . self::fixString($matches[2]) . $matches[3];
},
$tokens[$index]->getContent(),
),
Expand Down Expand Up @@ -161,9 +163,9 @@ private static function fixAttribute(Tokens $tokens, int $index): void
private static function fixString(string $string): string
{
if (Preg::match('/^[\\d\\.-]+(dev|(RC|alpha|beta)[\\d\\.])?$/', $string)) {
return '>= ' . $string;
$string = '>=' . $string;
}

return $string;
return Preg::replace('/^([<>=!]{0,2})\\s*([\\d\\.-]+(dev|(RC|alpha|beta)[\\d\\.])?)$/', '$1 $2', $string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
/**
* @internal
*
* @covers \PhpCsFixerCustomFixers\Fixer\PhpUnitRequiresExplicitConstraintFixer
* @covers \PhpCsFixerCustomFixers\Fixer\PhpUnitRequiresConstraintFixer
*/
final class PhpUnitRequiresExplicitConstraintFixerTest extends AbstractFixerTestCase
final class PhpUnitRequiresConstraintFixerTest extends AbstractFixerTestCase
{
public function testIsRisky(): void
{
Expand Down Expand Up @@ -224,6 +224,21 @@ public function testFoo(): void {}
public function testFoo(): void {}
}',
];

yield 'attributes with comparison constraints' => [
'<?php class FooTest extends TestCase {
#[\\LeaveMeAlone("1.2")]
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhp("!= 8.2")]
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhpunit("< 11")]
public function testFoo(): void {}
}',
'<?php class FooTest extends TestCase {
#[\\LeaveMeAlone("1.2")]
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhp("!=8.2")]
#[\\PHPUnit\\Framework\\Attributes\\RequiresPhpunit("<11")]
public function testFoo(): void {}
}',
];
}

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

yield 'single annotation' => [
yield 'single annotation without operator' => [
'/**
* @requires PHP >= 8.4
*/',
Expand All @@ -242,6 +257,15 @@ private static function getFixCases(): iterable
*/',
];

yield 'single annotation without space' => [
'/**
* @requires PHP >= 8.4
*/',
'/**
* @requires PHP >=8.4
*/',
];

yield 'annotation with trailing spaces' => [
'/**
* @requires PHP >= 8.4 ' . '
Expand All @@ -253,12 +277,12 @@ private static function getFixCases(): iterable

yield 'multiple annotations' => [
'/**
* @requires PHP >= 7
* @requires PHPUnit >= 11
* @requires PHP > 7
* @requires PHPUnit < 12
*/',
'/**
* @requires PHP 7
* @requires PHPUnit 11
* @requires PHP >7
* @requires PHPUnit < 12
*/',
];
}
Expand Down