Skip to content

Commit 876c779

Browse files
authored
feature #1493 [make:entity] managing keyword prefixes (is, has) for boolean properties getters
1 parent 2c90181 commit 876c779

File tree

6 files changed

+125
-3
lines changed

6 files changed

+125
-3
lines changed

src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function handleEmailConfirmation(Request $request, UserInterface $user):
4343
{
4444
$this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, $user-><?= $id_getter ?>(), $user-><?= $email_getter?>());
4545

46-
$user->setIsVerified(true);
46+
$user->setVerified(true);
4747

4848
$this->entityManager->persist($user);
4949
$this->entityManager->flush();

src/Util/ClassSourceManipulator.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,24 @@ public function addAccessorMethod(string $propertyName, string $methodName, $ret
270270

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

277+
private function getGetterName(string $propertyName, $returnType): string
278+
{
279+
if ('bool' !== $returnType) {
280+
return 'get'.Str::asCamelCase($propertyName);
281+
}
282+
283+
// exclude is & has from getter definition if already in property name
284+
if (0 !== strncasecmp($propertyName, 'is', 2) && 0 !== strncasecmp($propertyName, 'has', 3)) {
285+
return 'is'.Str::asCamelCase($propertyName);
286+
}
287+
288+
return Str::asLowerCamelCase($propertyName);
289+
}
290+
277291
public function addSetter(string $propertyName, ?string $type, bool $isNullable, array $commentLines = []): void
278292
{
279293
$builder = $this->createSetterNodeBuilder($propertyName, $type, $isNullable, $commentLines);
@@ -436,7 +450,7 @@ private function addCustomGetter(string $propertyName, string $methodName, $retu
436450

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

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

469+
private function getSetterName(string $propertyName, $type): string
470+
{
471+
if ('bool' === $type && 0 === strncasecmp($propertyName, 'is', 2)) {
472+
return 'set'.Str::asCamelCase(substr($propertyName, 2));
473+
}
474+
475+
return 'set'.Str::asCamelCase($propertyName);
476+
}
477+
455478
private function addSingularRelation(BaseRelation $relation): void
456479
{
457480
$typeHint = $this->addUseStatementIfNecessary($relation->getTargetClassName());

tests/Util/ClassSourceManipulatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ public function getAddGetterTests(): \Generator
106106
'User_simple_bool.php',
107107
];
108108

109+
yield 'getter_bool_begins_with_is' => [
110+
'User_simple.php',
111+
'isFooProp',
112+
'bool',
113+
[],
114+
'User_bool_begins_with_is.php',
115+
];
116+
117+
yield 'getter_bool_begins_with_has' => [
118+
'User_simple.php',
119+
'hasFooProp',
120+
'bool',
121+
[],
122+
'User_bool_begins_with_has.php',
123+
];
124+
109125
yield 'getter_no_props_comments' => [
110126
'User_no_props.php',
111127
'fooProp',
@@ -180,6 +196,15 @@ public function getAddSetterTests(): \Generator
180196
[],
181197
'User_simple_null_type.php',
182198
];
199+
200+
yield 'setter_bool_begins_with_is' => [
201+
'User_simple.php',
202+
'isFooProp',
203+
'bool',
204+
false,
205+
[],
206+
'User_bool_begins_with_is.php',
207+
];
183208
}
184209

185210
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
#[ORM\Entity]
8+
class User
9+
{
10+
#[ORM\Id]
11+
#[ORM\GeneratedValue]
12+
#[ORM\Column()]
13+
private ?int $id = null;
14+
15+
public function getId(): ?int
16+
{
17+
return $this->id;
18+
}
19+
20+
public function hasFooProp(): ?bool
21+
{
22+
return $this->hasFooProp;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
#[ORM\Entity]
8+
class User
9+
{
10+
#[ORM\Id]
11+
#[ORM\GeneratedValue]
12+
#[ORM\Column()]
13+
private ?int $id = null;
14+
15+
public function getId(): ?int
16+
{
17+
return $this->id;
18+
}
19+
20+
public function isFooProp(): ?bool
21+
{
22+
return $this->isFooProp;
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
#[ORM\Entity]
8+
class User
9+
{
10+
#[ORM\Id]
11+
#[ORM\GeneratedValue]
12+
#[ORM\Column()]
13+
private ?int $id = null;
14+
15+
public function getId(): ?int
16+
{
17+
return $this->id;
18+
}
19+
20+
public function setFooProp(bool $isFooProp): static
21+
{
22+
$this->isFooProp = $isFooProp;
23+
24+
return $this;
25+
}
26+
}

0 commit comments

Comments
 (0)