diff --git a/CHANGELOG.md b/CHANGELOG.md index 09b2bd4c..76a3bc2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # CHANGELOG for PHP CS Fixer: custom fixers ## v3.24.0 -- Add PhpUnitRequiresExplicitConstraintFixer +- Add PhpUnitRequiresConstraintFixer ## v3.23.0 - Add ClassConstantUsageFixer diff --git a/README.md b/README.md index 75fa6b19..ee79dbfd 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 = 8.1 + */ + public function testFoo() {} + /** -- * @requires PHP 8.4 -+ * @requires PHP >= 8.4 +- * @requires PHP <8.3 ++ * @requires PHP < 8.3 */ public function testBar() {} diff --git a/src/Fixer/PhpUnitRequiresExplicitConstraintFixer.php b/src/Fixer/PhpUnitRequiresConstraintFixer.php similarity index 89% rename from src/Fixer/PhpUnitRequiresExplicitConstraintFixer.php rename to src/Fixer/PhpUnitRequiresConstraintFixer.php index 723c56d5..3444aa1d 100644 --- a/src/Fixer/PhpUnitRequiresExplicitConstraintFixer.php +++ b/src/Fixer/PhpUnitRequiresConstraintFixer.php @@ -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' = ' . $matches[2]; - } - return $matches[1] . $matches[2] . $matches[3]; + return $matches[1] . self::fixString($matches[2]) . $matches[3]; }, $tokens[$index]->getContent(), ), @@ -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); } } diff --git a/tests/Fixer/PhpUnitRequiresExplicitConstraintFixerTest.php b/tests/Fixer/PhpUnitRequiresConstraintFixerTest.php similarity index 86% rename from tests/Fixer/PhpUnitRequiresExplicitConstraintFixerTest.php rename to tests/Fixer/PhpUnitRequiresConstraintFixerTest.php index bc081c5c..53b7bedc 100644 --- a/tests/Fixer/PhpUnitRequiresExplicitConstraintFixerTest.php +++ b/tests/Fixer/PhpUnitRequiresConstraintFixerTest.php @@ -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 { @@ -224,6 +224,21 @@ public function testFoo(): void {} public function testFoo(): void {} }', ]; + + yield 'attributes with comparison constraints' => [ + ' ['']; - yield 'single annotation' => [ + yield 'single annotation without operator' => [ '/** * @requires PHP >= 8.4 */', @@ -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 ' . ' @@ -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 */', ]; }