Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php80] Add DoctrineCoverterterAttributeDecorator to convert "false"/…
…"true" string to false/true on nullable arg on Doctrine\ORM\Mapping\Column (#5629) * [Php80] Add DoctrineCoverterterAttributeDecorator to convert "false"/"true" string to false/true on nullable arg on Doctrine\ORM\Mapping\Column * [ci-review] Rector Rectify * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
2ccf2de
commit a533e70
Showing
7 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...80/Rector/Class_/AnnotationToAttributeRector/Fixture/Doctrine/nullable_bool_value.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,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity(repositoryClass="App\Some\Class") | ||
*/ | ||
class NullableBoolValue | ||
{ | ||
/** | ||
* @ORM\Column(type="bigint", nullable="true") | ||
*/ | ||
private int $stop_ts; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity(repositoryClass: \App\Some\Class::class)] | ||
class NullableBoolValue | ||
{ | ||
#[ORM\Column(type: 'bigint', nullable: true)] | ||
private int $stop_ts; | ||
} | ||
|
||
?> |
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
45 changes: 45 additions & 0 deletions
45
rules/Php80/AttributeDecorator/DoctrineCoverterterAttributeDecorator.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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\AttributeDecorator; | ||
|
||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\Php80\Contract\ConverterAttributeDecoratorInterface; | ||
|
||
final class DoctrineCoverterterAttributeDecorator implements ConverterAttributeDecoratorInterface | ||
{ | ||
public function getAttributeName(): string | ||
{ | ||
return 'Doctrine\ORM\Mapping\Column'; | ||
} | ||
|
||
public function decorate(Attribute $attribute): void | ||
{ | ||
foreach ($attribute->args as $arg) { | ||
if (! $arg->name instanceof Identifier) { | ||
continue; | ||
} | ||
|
||
if ($arg->name->toString() !== 'nullable') { | ||
continue; | ||
} | ||
|
||
$value = $arg->value; | ||
if (! $value instanceof String_) { | ||
continue; | ||
} | ||
|
||
if (! in_array($value->value, ['true', 'false'], true)) { | ||
continue; | ||
} | ||
|
||
$arg->value = $value->value === 'true' ? new ConstFetch(new Name('true')) : new ConstFetch(new Name('false')); | ||
break; | ||
} | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
rules/Php80/Contract/ConverterAttributeDecoratorInterface.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\Contract; | ||
|
||
use PhpParser\Node\Attribute; | ||
|
||
interface ConverterAttributeDecoratorInterface | ||
{ | ||
public function getAttributeName(): string; | ||
|
||
public function decorate(Attribute $attribute): void; | ||
} |
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
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