Skip to content

Commit 54c35a8

Browse files
localheinzkeradus
authored andcommitted
Enhancement: Allow to specify minimum and maximum PHP versions for code samples
1 parent 782fac0 commit 54c35a8

11 files changed

+496
-13
lines changed

src/Console/Command/DescribeCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpCsFixer\Fixer\FixerInterface;
2020
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
2121
use PhpCsFixer\FixerDefinition\ShortFixerDefinition;
22+
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSampleInterface;
2223
use PhpCsFixer\FixerFactory;
2324
use PhpCsFixer\RuleSet;
2425
use PhpCsFixer\StdinFileInfo;
@@ -149,6 +150,10 @@ private function describeRule(OutputInterface $output, $name)
149150
));
150151

151152
foreach ($definition->getCodeSamples() as $index => $codeSample) {
153+
if ($codeSample instanceof VersionSpecificCodeSampleInterface && !$codeSample->isSuitableFor(PHP_VERSION_ID)) {
154+
continue;
155+
}
156+
152157
$old = $codeSample->getCode();
153158
$tokens = Tokens::fromCode($old);
154159
if ($fixer instanceof ConfigurableFixerInterface) {

src/Fixer/Alias/PowToExponentiationFixer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
namespace PhpCsFixer\Fixer\Alias;
1414

1515
use PhpCsFixer\AbstractFunctionReferenceFixer;
16-
use PhpCsFixer\FixerDefinition\CodeSample;
17-
use PhpCsFixer\FixerDefinition\ShortFixerDefinition;
16+
use PhpCsFixer\FixerDefinition\FixerDefinition;
17+
use PhpCsFixer\FixerDefinition\VersionSpecification;
18+
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
1819
use PhpCsFixer\Tokenizer\CT;
1920
use PhpCsFixer\Tokenizer\Token;
2021
use PhpCsFixer\Tokenizer\Tokens;
@@ -74,20 +75,19 @@ public function fix(\SplFileInfo $file, Tokens $tokens)
7475
*/
7576
public function getDefinition()
7677
{
77-
/* @TODO That code should be in use, but for now it will fail on lower PHP version...
7878
return new FixerDefinition(
7979
'Converts \'pow()\' to \'**\' operator. Requires PHP >= 5.6.',
80-
array(new CodeSample("<?php\n pow(\$a, 1);")),
80+
array(
81+
new VersionSpecificCodeSample(
82+
"<?php\n pow(\$a, 1);",
83+
new VersionSpecification(50600)
84+
),
85+
),
8186
null,
8287
null,
8388
null,
8489
'Risky when the function \'pow()\' function is overridden.'
8590
);
86-
*/
87-
88-
return new ShortFixerDefinition(
89-
'Converts \'pow()\' to \'**\' operator. Requires PHP >= 5.6.'
90-
);
9191
}
9292

9393
/**

src/Fixer/ArrayNotation/ArraySyntaxFixer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
1818
use PhpCsFixer\FixerDefinition\CodeSample;
1919
use PhpCsFixer\FixerDefinition\FixerDefinition;
20+
use PhpCsFixer\FixerDefinition\VersionSpecification;
21+
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
2022
use PhpCsFixer\Tokenizer\CT;
2123
use PhpCsFixer\Tokenizer\Token;
2224
use PhpCsFixer\Tokenizer\Tokens;
@@ -91,12 +93,11 @@ public function getDefinition()
9193
"<?php\n[1,2];",
9294
array('syntax' => 'long')
9395
),
94-
/* @TODO That code should be in use, but for now it will fail on lower PHP version...
95-
new CodeSample(
96+
new VersionSpecificCodeSample(
9697
"<?php\narray(1,2);",
98+
new VersionSpecification(50400),
9799
array('syntax' => 'short')
98100
),
99-
*/
100101
),
101102
null,
102103
'Configure to use "long" or "short" array declaration syntax.',
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
* Dariusz Rumiński <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\FixerDefinition;
14+
15+
/**
16+
* @author Andreas Möller <[email protected]>
17+
*/
18+
final class VersionSpecificCodeSample implements VersionSpecificCodeSampleInterface
19+
{
20+
/**
21+
* @var CodeSampleInterface
22+
*/
23+
private $codeSample;
24+
25+
/**
26+
* @var VersionSpecificationInterface
27+
*/
28+
private $versionSpecification;
29+
30+
/**
31+
* @param string $code
32+
* @param VersionSpecificationInterface $versionSpecification
33+
* @param null|array $configuration
34+
*/
35+
public function __construct(
36+
$code,
37+
VersionSpecificationInterface $versionSpecification,
38+
array $configuration = null
39+
) {
40+
$this->codeSample = new CodeSample($code, $configuration);
41+
$this->versionSpecification = $versionSpecification;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getCode()
48+
{
49+
return $this->codeSample->getCode();
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function getConfiguration()
56+
{
57+
return $this->codeSample->getConfiguration();
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function isSuitableFor($version)
64+
{
65+
return $this->versionSpecification->isSatisfiedBy($version);
66+
}
67+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
* Dariusz Rumiński <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\FixerDefinition;
14+
15+
/**
16+
* @author Andreas Moeller <[email protected]>
17+
*/
18+
interface VersionSpecificCodeSampleInterface extends CodeSampleInterface
19+
{
20+
/**
21+
* @param int $version
22+
*
23+
* @return bool
24+
*/
25+
public function isSuitableFor($version);
26+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
* Dariusz Rumiński <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\FixerDefinition;
14+
15+
/**
16+
* @author Andreas Möller <[email protected]>
17+
*/
18+
final class VersionSpecification implements VersionSpecificationInterface
19+
{
20+
/**
21+
* @var int|null
22+
*/
23+
private $minimum;
24+
25+
/**
26+
* @var int|null
27+
*/
28+
private $maximum;
29+
30+
/**
31+
* @param int|null $minimum
32+
* @param int|null $maximum
33+
*
34+
* @throws \InvalidArgumentException
35+
*/
36+
public function __construct($minimum = null, $maximum = null)
37+
{
38+
if (null === $minimum && null === $maximum) {
39+
throw new \InvalidArgumentException('Either minimum or maximum need to be specified');
40+
}
41+
42+
if (null !== $minimum && (!is_int($minimum) || 1 > $minimum)) {
43+
throw new \InvalidArgumentException('Minimum needs to be either null or an integer greater than 0');
44+
}
45+
46+
if (null !== $maximum && (!is_int($maximum) || 1 > $maximum)) {
47+
throw new \InvalidArgumentException('Minimum needs to be either null or an integer greater than 0');
48+
}
49+
50+
if (null !== $maximum && null !== $minimum && $maximum < $minimum) {
51+
throw new \InvalidArgumentException('Maximum should not be less than the minimum');
52+
}
53+
54+
$this->minimum = $minimum;
55+
$this->maximum = $maximum;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function isSatisfiedBy($version)
62+
{
63+
if (null !== $this->minimum && $version < $this->minimum) {
64+
return false;
65+
}
66+
if (null !== $this->maximum && $version > $this->maximum) {
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
* Dariusz Rumiński <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\FixerDefinition;
14+
15+
/**
16+
* @author Andreas Möller <[email protected]>
17+
*/
18+
interface VersionSpecificationInterface
19+
{
20+
/**
21+
* @param int $version
22+
*
23+
* @return bool
24+
*/
25+
public function isSatisfiedBy($version);
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
* Dariusz Rumiński <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\Tests\FixerDefinition;
14+
15+
use PhpCsFixer\FixerDefinition\CodeSample;
16+
17+
/**
18+
* @author Andreas Möller <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
final class CodeSampleTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testConstructorSetsValues()
25+
{
26+
$code = '<php echo $foo;';
27+
$configuration = array(
28+
'foo' => 'bar',
29+
);
30+
31+
$codeSample = new CodeSample(
32+
$code,
33+
$configuration
34+
);
35+
36+
$this->assertSame($code, $codeSample->getCode());
37+
$this->assertSame($configuration, $codeSample->getConfiguration());
38+
}
39+
40+
public function testConfigurationDefaultsToNull()
41+
{
42+
$codeSample = new CodeSample('<php echo $foo;');
43+
44+
$this->assertNull($codeSample->getConfiguration());
45+
}
46+
}

0 commit comments

Comments
 (0)