-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Downgrade PHP 5.4] Add DowngradeBinaryNotationRector (#1202)
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ePhp54/Rector/LNumber/DowngradeBinaryNotationRector/DowngradeBinaryNotationRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeBinaryNotationRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ts/DowngradePhp54/Rector/LNumber/DowngradeBinaryNotationRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return 0b11111100101 + 0B01; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return 2021 + 1; | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...ts/DowngradePhp54/Rector/LNumber/DowngradeBinaryNotationRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeBinaryNotationRector::class); | ||
}; |
60 changes: 60 additions & 0 deletions
60
rules/DowngradePhp54/Rector/LNumber/DowngradeBinaryNotationRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp54\Rector\LNumber; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://wiki.php.net/rfc/binnotation4ints | ||
* | ||
* @see \Rector\Tests\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector\DowngradeBinaryNotationRectorTest | ||
*/ | ||
final class DowngradeBinaryNotationRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Downgrade binary notation for integers', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$a = 0b11111100101; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$a = 2021; | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [LNumber::class]; | ||
} | ||
|
||
/** | ||
* @param LNumber $node | ||
*/ | ||
public function refactor(Node $node): ?LNumber | ||
{ | ||
$kind = $node->getAttribute(AttributeKey::KIND); | ||
if ($kind !== LNumber::KIND_BIN) { | ||
return null; | ||
} | ||
|
||
$node->setAttribute(AttributeKey::KIND, LNumber::KIND_DEC); | ||
// force php-parser to re-print: https://github.com/rectorphp/rector/issues/6618#issuecomment-893226087 | ||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
|
||
return $node; | ||
} | ||
} |