Skip to content

Commit

Permalink
DX: do not allow version specific code sample with minimum PHP versio…
Browse files Browse the repository at this point in the history
…n lower than the lowest supported one (#7207)
  • Loading branch information
kubawerlos authored Aug 19, 2023
1 parent 71b76ba commit dc9d958
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -38,7 +36,7 @@ public function getDefinition(): FixerDefinitionInterface
'In array declaration, there MUST NOT be a whitespace before each comma.',
[
new CodeSample("<?php \$x = array(1 , \"2\");\n"),
new VersionSpecificCodeSample(
new CodeSample(
<<<'PHP'
<?php
$x = [<<<EOD
Expand All @@ -48,7 +46,6 @@ public function getDefinition(): FixerDefinitionInterface
];

PHP,
new VersionSpecification(7_03_00),
['after_heredoc' => true]
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
Expand Down Expand Up @@ -102,9 +100,8 @@ public function getDefinition(): FixerDefinitionInterface
new CodeSample(
"<?php\nfunction Foo(Iterable \$a): VOID\n{\n echo 'Hello world';\n}\n"
),
new VersionSpecificCodeSample(
new CodeSample(
"<?php\nfunction Foo(Object \$a)\n{\n return 'hi!';\n}\n",
new VersionSpecification(7_02_00)
),
]
);
Expand Down
6 changes: 2 additions & 4 deletions src/Fixer/CastNotation/LowercaseCastFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
namespace PhpCsFixer\Fixer\CastNotation;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -29,7 +28,7 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
'Cast should be written in lower case.',
[
new VersionSpecificCodeSample(
new CodeSample(
'<?php
$a = (BOOLEAN) $b;
$a = (BOOL) $b;
Expand All @@ -44,7 +43,6 @@ public function getDefinition(): FixerDefinitionInterface
$a = (UNset) $b;
$a = (Binary) $b;
',
new VersionSpecification(7_04_00)
),
]
);
Expand Down
6 changes: 2 additions & 4 deletions src/Fixer/CastNotation/ShortScalarCastFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
namespace PhpCsFixer\Fixer\CastNotation;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -29,9 +28,8 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
'Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`, `(binary)` as `(string)`.',
[
new VersionSpecificCodeSample(
new CodeSample(
"<?php\n\$a = (boolean) \$b;\n\$a = (integer) \$b;\n\$a = (double) \$b;\n\n\$a = (binary) \$b;\n",
new VersionSpecification(7_04_00)
),
]
);
Expand Down
5 changes: 2 additions & 3 deletions src/Fixer/ControlStructure/TrailingCommaInMultilineFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getDefinition(): FixerDefinitionInterface
'Multi-line arrays, arguments list, parameters list and `match` expressions must have a trailing comma.',
[
new CodeSample("<?php\narray(\n 1,\n 2\n);\n"),
new VersionSpecificCodeSample(
new CodeSample(
<<<'SAMPLE'
<?php
$x = [
Expand All @@ -74,10 +74,9 @@ public function getDefinition(): FixerDefinitionInterface

SAMPLE
,
new VersionSpecification(7_03_00),
['after_heredoc' => true]
),
new VersionSpecificCodeSample("<?php\nfoo(\n 1,\n 2\n);\n", new VersionSpecification(7_03_00), ['elements' => [self::ELEMENTS_ARGUMENTS]]),
new CodeSample("<?php\nfoo(\n 1,\n 2\n);\n", ['elements' => [self::ELEMENTS_ARGUMENTS]]),
new VersionSpecificCodeSample("<?php\nfunction foo(\n \$x,\n \$y\n)\n{\n}\n", new VersionSpecification(8_00_00), ['elements' => [self::ELEMENTS_PARAMETERS]]),
]
);
Expand Down
5 changes: 1 addition & 4 deletions src/Fixer/FunctionNotation/FunctionDeclarationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;
Expand Down Expand Up @@ -82,11 +80,10 @@ function foo ($bar, $baz)
',
['closure_function_spacing' => self::SPACING_NONE]
),
new VersionSpecificCodeSample(
new CodeSample(
'<?php
$f = fn () => null;
',
new VersionSpecification(7_04_00),
['closure_fn_spacing' => self::SPACING_NONE]
),
]
Expand Down
5 changes: 1 addition & 4 deletions src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
Expand Down Expand Up @@ -74,7 +72,7 @@ public function getDefinition(): FixerDefinitionInterface
'keep_multiple_spaces_after_comma' => false,
]
),
new VersionSpecificCodeSample(
new CodeSample(
<<<'SAMPLE'
<?php
sample(
Expand All @@ -87,7 +85,6 @@ public function getDefinition(): FixerDefinitionInterface

SAMPLE
,
new VersionSpecification(7_03_00),
['after_heredoc' => true]
),
],
Expand Down
9 changes: 3 additions & 6 deletions src/Fixer/FunctionNotation/PhpdocToPropertyTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

use PhpCsFixer\AbstractPhpdocToTypeDeclarationFixer;
use PhpCsFixer\DocBlock\Annotation;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -39,7 +38,7 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
'EXPERIMENTAL: Takes `@var` annotation of non-mixed types and adjusts accordingly the property signature. Requires PHP >= 7.4.',
[
new VersionSpecificCodeSample(
new CodeSample(
'<?php
class Foo {
/** @var int */
Expand All @@ -48,9 +47,8 @@ class Foo {
private $bar;
}
',
new VersionSpecification(7_04_00)
),
new VersionSpecificCodeSample(
new CodeSample(
'<?php
class Foo {
/** @var int */
Expand All @@ -59,7 +57,6 @@ class Foo {
private $bar;
}
',
new VersionSpecification(7_04_00),
['scalar_types' => false]
),
],
Expand Down
6 changes: 2 additions & 4 deletions src/Fixer/FunctionNotation/UseArrowFunctionsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
namespace PhpCsFixer\Fixer\FunctionNotation;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
Expand All @@ -34,7 +33,7 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
'Anonymous functions with one-liner return statement must use arrow functions.',
[
new VersionSpecificCodeSample(
new CodeSample(
<<<'SAMPLE'
<?php
foo(function ($a) use ($b) {
Expand All @@ -43,7 +42,6 @@ public function getDefinition(): FixerDefinitionInterface

SAMPLE
,
new VersionSpecification(7_04_00)
),
],
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
namespace PhpCsFixer\Fixer\Operator;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\Analyzer\RangeAnalyzer;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
Expand All @@ -31,9 +30,8 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
'Use the null coalescing assignment operator `??=` where possible.',
[
new VersionSpecificCodeSample(
new CodeSample(
"<?php\n\$foo = \$foo ?? 1;\n",
new VersionSpecification(7_04_00)
),
]
);
Expand Down
8 changes: 8 additions & 0 deletions tests/Test/AbstractFixerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ final public function testFixerDefinitions(): void
}
self::assertTrue($hasSuitableSupportedVersion, 'Version specific code sample must be suitable for at least 1 supported PHP version.');

$hasUnsuitableSupportedVersion = false;
foreach ($supportedPhpVersions as $version) {
if (!$sample->isSuitableFor($version)) {
$hasUnsuitableSupportedVersion = true;
}
}
self::assertTrue($hasUnsuitableSupportedVersion, 'Version specific code sample must be unsuitable for at least 1 supported PHP version.');

if (!$sample->isSuitableFor(\PHP_VERSION_ID)) {
continue;
}
Expand Down

0 comments on commit dc9d958

Please sign in to comment.