Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[make:entity] managing keyword prefixes (is, has) for boolean properties getters #1493

Merged
merged 10 commits into from
Mar 27, 2024
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;
}
}