From a08d26bee5f6d1f07abb9dba4ee6f27eff1d364d Mon Sep 17 00:00:00 2001 From: Krystian Marcisz Date: Mon, 14 Dec 2020 00:45:55 +0100 Subject: [PATCH] [Symfony 5.2] Add PropertyAccessorCreationBooleanToFlagsRector (#4874) --- config/set/symfony52.php | 4 + ...tyAccessorCreationBooleanToFlagsRector.php | 104 ++++++++++++++++++ .../Fixture/argument_false.php.inc | 31 ++++++ .../Fixture/argument_flag.php.inc | 15 +++ .../Fixture/argument_true.php.inc | 31 ++++++ ...cessorCreationBooleanToFlagsRectorTest.php | 31 ++++++ 6 files changed, 216 insertions(+) create mode 100644 rules/symfony5/src/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector.php create mode 100644 rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_false.php.inc create mode 100644 rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_flag.php.inc create mode 100644 rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_true.php.inc create mode 100644 rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/PropertyAccessorCreationBooleanToFlagsRectorTest.php diff --git a/config/set/symfony52.php b/config/set/symfony52.php index cfd5dc569a0c..d02c6d2360dd 100644 --- a/config/set/symfony52.php +++ b/config/set/symfony52.php @@ -6,6 +6,7 @@ use Rector\Renaming\Rector\MethodCall\RenameMethodRector; use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Renaming\ValueObject\RenameClassConstant; +use Rector\Symfony5\Rector\New_\PropertyAccessorCreationBooleanToFlagsRector; use Rector\Symfony5\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symplify\SymfonyPhpConfig\ValueObjectInliner; @@ -28,6 +29,9 @@ ]), ]]); + # https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#propertyaccess + $services->set(PropertyAccessorCreationBooleanToFlagsRector::class); + # https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#security $services->set(RenameClassConstantRector::class) ->call('configure', [[ diff --git a/rules/symfony5/src/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector.php b/rules/symfony5/src/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector.php new file mode 100644 index 000000000000..4531eb96da77 --- /dev/null +++ b/rules/symfony5/src/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector.php @@ -0,0 +1,104 @@ +shouldSkip($node)) { + return null; + } + + $isTrue = $this->isTrue($node->args[0]->value); + $flags = $this->prepareFlags($isTrue); + $node->args[0] = $this->createArg($flags); + + return $node; + } + + private function prepareFlags(bool $currentValue): BitwiseOr + { + $magicGet = $this->createClassConstFetch('Symfony\Component\PropertyAccess\PropertyAccessor', 'MAGIC_GET'); + $magicSet = $this->createClassConstFetch('Symfony\Component\PropertyAccess\PropertyAccessor', 'MAGIC_SET'); + if (!$currentValue) { + return new BitwiseOr($magicGet, $magicSet); + } + + return new BitwiseOr( + new BitwiseOr( + $this->createClassConstFetch('Symfony\Component\PropertyAccess\PropertyAccessor', 'MAGIC_CALL'), + $magicGet, + ), + $magicSet, + ); + } + + private function shouldSkip(New_ $new_): bool + { + if (! $new_->class instanceof Node\Name) { + return true; + } + + if (! $this->isName($new_->class, 'Symfony\Component\PropertyAccess\PropertyAccessor')) { + return true; + } + + if (! $this->isBool($new_->args[0]->value)) { + return true; + } + + return false; + } +} diff --git a/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_false.php.inc b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_false.php.inc new file mode 100644 index 000000000000..3408fd1bf4f2 --- /dev/null +++ b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_false.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_flag.php.inc b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_flag.php.inc new file mode 100644 index 000000000000..b0ac9fdc7206 --- /dev/null +++ b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_flag.php.inc @@ -0,0 +1,15 @@ + diff --git a/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_true.php.inc b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_true.php.inc new file mode 100644 index 000000000000..aa48e05bf786 --- /dev/null +++ b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/Fixture/argument_true.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/PropertyAccessorCreationBooleanToFlagsRectorTest.php b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/PropertyAccessorCreationBooleanToFlagsRectorTest.php new file mode 100644 index 000000000000..706c23835c13 --- /dev/null +++ b/rules/symfony5/tests/Rector/New_/PropertyAccessorCreationBooleanToFlagsRector/PropertyAccessorCreationBooleanToFlagsRectorTest.php @@ -0,0 +1,31 @@ +doTestFileInfo($fileInfo); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return PropertyAccessorCreationBooleanToFlagsRector::class; + } +}