Skip to content

Commit

Permalink
feature #1493 [make:entity] managing keyword prefixes (is, has) for b…
Browse files Browse the repository at this point in the history
…oolean properties getters
  • Loading branch information
ClemRiviere authored Mar 27, 2024
1 parent 2c90181 commit 876c779
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handleEmailConfirmation(Request $request, UserInterface $user):
{
$this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, $user-><?= $id_getter ?>(), $user-><?= $email_getter?>());

$user->setIsVerified(true);
$user->setVerified(true);

$this->entityManager->persist($user);
$this->entityManager->flush();
Expand Down
27 changes: 25 additions & 2 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,24 @@ public function addAccessorMethod(string $propertyName, string $methodName, $ret

public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): void
{
$methodName = ('bool' === $returnType ? 'is' : 'get').Str::asCamelCase($propertyName);
$methodName = $this->getGetterName($propertyName, $returnType);
$this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines);
}

private function getGetterName(string $propertyName, $returnType): string
{
if ('bool' !== $returnType) {
return 'get'.Str::asCamelCase($propertyName);
}

// exclude is & has from getter definition if already in property name
if (0 !== strncasecmp($propertyName, 'is', 2) && 0 !== strncasecmp($propertyName, 'has', 3)) {
return 'is'.Str::asCamelCase($propertyName);
}

return Str::asLowerCamelCase($propertyName);
}

public function addSetter(string $propertyName, ?string $type, bool $isNullable, array $commentLines = []): void
{
$builder = $this->createSetterNodeBuilder($propertyName, $type, $isNullable, $commentLines);
Expand Down Expand Up @@ -436,7 +450,7 @@ private function addCustomGetter(string $propertyName, string $methodName, $retu

private function createSetterNodeBuilder(string $propertyName, $type, bool $isNullable, array $commentLines = []): Builder\Method
{
$methodName = 'set'.Str::asCamelCase($propertyName);
$methodName = $this->getSetterName($propertyName, $type);
$setterNodeBuilder = (new Builder\Method($methodName))->makePublic();

if ($commentLines) {
Expand All @@ -452,6 +466,15 @@ private function createSetterNodeBuilder(string $propertyName, $type, bool $isNu
return $setterNodeBuilder;
}

private function getSetterName(string $propertyName, $type): string
{
if ('bool' === $type && 0 === strncasecmp($propertyName, 'is', 2)) {
return 'set'.Str::asCamelCase(substr($propertyName, 2));
}

return 'set'.Str::asCamelCase($propertyName);
}

private function addSingularRelation(BaseRelation $relation): void
{
$typeHint = $this->addUseStatementIfNecessary($relation->getTargetClassName());
Expand Down
25 changes: 25 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ public function getAddGetterTests(): \Generator
'User_simple_bool.php',
];

yield 'getter_bool_begins_with_is' => [
'User_simple.php',
'isFooProp',
'bool',
[],
'User_bool_begins_with_is.php',
];

yield 'getter_bool_begins_with_has' => [
'User_simple.php',
'hasFooProp',
'bool',
[],
'User_bool_begins_with_has.php',
];

yield 'getter_no_props_comments' => [
'User_no_props.php',
'fooProp',
Expand Down Expand Up @@ -180,6 +196,15 @@ public function getAddSetterTests(): \Generator
[],
'User_simple_null_type.php',
];

yield 'setter_bool_begins_with_is' => [
'User_simple.php',
'isFooProp',
'bool',
false,
[],
'User_bool_begins_with_is.php',
];
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Util/fixtures/add_getter/User_bool_begins_with_has.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

public function getId(): ?int
{
return $this->id;
}

public function hasFooProp(): ?bool
{
return $this->hasFooProp;
}
}
24 changes: 24 additions & 0 deletions tests/Util/fixtures/add_getter/User_bool_begins_with_is.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

public function getId(): ?int
{
return $this->id;
}

public function isFooProp(): ?bool
{
return $this->isFooProp;
}
}
26 changes: 26 additions & 0 deletions tests/Util/fixtures/add_setter/User_bool_begins_with_is.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

public function getId(): ?int
{
return $this->id;
}

public function setFooProp(bool $isFooProp): static
{
$this->isFooProp = $isFooProp;

return $this;
}
}

0 comments on commit 876c779

Please sign in to comment.