Skip to content

Commit

Permalink
tidy up bool values
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 14, 2024
1 parent 474a82e commit 71fd367
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAnnotationsDoctr
final class BoolDefaultFalse
{
/**
* @\Doctrine\ORM\Mapping\Column(type="boolean", options={"default"=false, "size"=100, "where"="name IS NULL"})
* @\Doctrine\ORM\Mapping\Column(type="boolean", unique=true, options={"default"=false, "size"=100, "where"="name IS NULL"})
*/
public $someProperty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SoftdeletableGedmo
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAnnotationsDoctrineMappingRector\Fixture;

/**
* @\Gedmo\Mapping\Annotation\SoftDeleteable(fieldName="deleted", timeAware="false")
* @\Gedmo\Mapping\Annotation\SoftDeleteable(fieldName="deleted", timeAware=false, someNumber=100)
* @\Doctrine\ORM\Mapping\Table
*/
final class SoftdeletableGedmo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAnnotationsDoctrineMapping
fields:
someProperty:
type: boolean
unique: true
options:
default: false
size: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAnnotationsDoctrineMapping
soft_deleteable:
field_name: "deleted"
time_aware: false
some_number: 100
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Rector\Doctrine\CodeQuality\AnnotationTransformer\ClassAnnotationTransformer;

use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Doctrine\CodeQuality\Contract\ClassAnnotationTransformerInterface;
use Rector\Doctrine\CodeQuality\DocTagNodeFactory;
use Rector\Doctrine\CodeQuality\Helper\NodeValueNormalizer;
use Rector\Doctrine\CodeQuality\Utils\CaseStringHelper;
use Rector\Doctrine\CodeQuality\ValueObject\EntityMapping;

Expand Down Expand Up @@ -50,13 +50,12 @@ private function createArrayItemNodes(array $softDeletableMapping): array
{
$arrayItemNodes = [];

foreach ($softDeletableMapping as $fieldKey => $fieldValue) {
if (is_bool($fieldValue)) {
$fieldValue = $fieldValue ? 'true' : 'false';
}
foreach ($softDeletableMapping as $fieldKey => $fieldValueNode) {
$fieldKey = CaseStringHelper::camelCase($fieldKey);

$camelCaseFieldKey = CaseStringHelper::camelCase($fieldKey);
$arrayItemNodes[] = new ArrayItemNode(new StringNode($fieldValue), $camelCaseFieldKey);
$fieldValueNode = NodeValueNormalizer::normalize($fieldValueNode);

$arrayItemNodes[] = new ArrayItemNode($fieldValueNode, $fieldKey);
}

return $arrayItemNodes;
Expand Down
23 changes: 23 additions & 0 deletions rules/CodeQuality/Helper/NodeValueNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\CodeQuality\Helper;

use Rector\BetterPhpDocParser\PhpDoc\StringNode;

final class NodeValueNormalizer
{
public static function normalize(mixed $value): mixed
{
if (is_bool($value)) {
return $value ? 'true' : 'false';
}

if (is_numeric($value)) {
return $value;
}

return new StringNode($value);
}
}
5 changes: 4 additions & 1 deletion rules/CodeQuality/NodeFactory/ArrayItemNodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey;
use Rector\Doctrine\CodeQuality\Helper\NodeValueNormalizer;
use Webmozart\Assert\Assert;

final class ArrayItemNodeFactory
Expand Down Expand Up @@ -63,7 +64,7 @@ public function create(array $propertyMapping, array $quotedFields = []): array
$fieldSingleValue = (string) $fieldSingleValue;
$fieldArrayItemNode = new ArrayItemNode($fieldSingleValue, new StringNode($fieldSingleKey));
} elseif (is_bool($fieldSingleValue)) {
$fieldSingleValue = $fieldSingleValue ? 'true' : 'false';
$fieldSingleValue = NodeValueNormalizer::normalize($fieldSingleValue);
$fieldArrayItemNode = new ArrayItemNode($fieldSingleValue, new StringNode($fieldSingleKey));
} elseif (is_string($fieldSingleKey)) {
$fieldArrayItemNode = new ArrayItemNode(new StringNode($fieldSingleValue), new StringNode(
Expand Down Expand Up @@ -97,6 +98,8 @@ public function create(array $propertyMapping, array $quotedFields = []): array
continue;
}

$fieldValue = NodeValueNormalizer::normalize($fieldValue);

$arrayItemNodes[] = new ArrayItemNode($fieldValue, $fieldKey);
}

Expand Down

0 comments on commit 71fd367

Please sign in to comment.