From 23dd5348eceb65c22215ff0d9c78d5d20c4272c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Tue, 26 Mar 2024 19:20:25 +0100 Subject: [PATCH 01/10] Refactor addGetter prefix definition for boolean properties --- src/Util/ClassSourceManipulator.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index cde3cade1..5e9ca96ad 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -270,10 +270,29 @@ 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); + $prefix = $this->getGetterPrefix($propertyName, $returnType); + $methodName = $prefix ? $prefix.Str::asCamelCase($propertyName) : Str::asLowerCamelCase($propertyName); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } + /** + * @return string|null : getter prefix or null if no prefix to append + */ + private function getGetterPrefix($propertyName, $returnType): ?string + { + if ('bool' !== $returnType) { + return 'get'; + } + + // exclude is & has from getter definition if already in property name + $propertyName = strtolower($propertyName); + if (!str_starts_with($propertyName, 'is') && !str_starts_with($propertyName, 'has')) { + return 'is'; + } + + return null; + } + public function addSetter(string $propertyName, ?string $type, bool $isNullable, array $commentLines = []): void { $builder = $this->createSetterNodeBuilder($propertyName, $type, $isNullable, $commentLines); From a5f5444361635e594e189a3d912cf5c333329a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Tue, 26 Mar 2024 19:20:44 +0100 Subject: [PATCH 02/10] Adding unit tests concerning boolean properties getter prefixes --- tests/Util/ClassSourceManipulatorTest.php | 16 +++++++++++++ .../add_getter/User_bool_begins_with_has.php | 24 +++++++++++++++++++ .../add_getter/User_bool_begins_with_is.php | 24 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/Util/fixtures/add_getter/User_bool_begins_with_has.php create mode 100644 tests/Util/fixtures/add_getter/User_bool_begins_with_is.php diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index 534d9fc15..8fc2ec62f 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -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', diff --git a/tests/Util/fixtures/add_getter/User_bool_begins_with_has.php b/tests/Util/fixtures/add_getter/User_bool_begins_with_has.php new file mode 100644 index 000000000..20771c7b4 --- /dev/null +++ b/tests/Util/fixtures/add_getter/User_bool_begins_with_has.php @@ -0,0 +1,24 @@ +id; + } + + public function hasFooProp(): ?bool + { + return $this->hasFooProp; + } +} diff --git a/tests/Util/fixtures/add_getter/User_bool_begins_with_is.php b/tests/Util/fixtures/add_getter/User_bool_begins_with_is.php new file mode 100644 index 000000000..e26c0a7c1 --- /dev/null +++ b/tests/Util/fixtures/add_getter/User_bool_begins_with_is.php @@ -0,0 +1,24 @@ +id; + } + + public function isFooProp(): ?bool + { + return $this->isFooProp; + } +} From 7f539a2572396a458d6e7a26d6de17e110652aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 10:59:33 +0100 Subject: [PATCH 03/10] Refactor getterName definition --- src/Util/ClassSourceManipulator.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 5e9ca96ad..f910b0219 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -270,27 +270,23 @@ public function addAccessorMethod(string $propertyName, string $methodName, $ret public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): void { - $prefix = $this->getGetterPrefix($propertyName, $returnType); - $methodName = $prefix ? $prefix.Str::asCamelCase($propertyName) : Str::asLowerCamelCase($propertyName); + $methodName = $this->getGetterName($propertyName, $returnType); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } /** * @return string|null : getter prefix or null if no prefix to append */ - private function getGetterPrefix($propertyName, $returnType): ?string + private function getGetterName($propertyName, $returnType): ?string { if ('bool' !== $returnType) { - return 'get'; + return 'get' . Str::asCamelCase($propertyName); } - // exclude is & has from getter definition if already in property name - $propertyName = strtolower($propertyName); - if (!str_starts_with($propertyName, 'is') && !str_starts_with($propertyName, 'has')) { - return 'is'; + if (!strncasecmp($propertyName, 'is', 2) && !strncasecmp($propertyName, 'has', 3)) { + return 'is' . Str::asCamelCase($propertyName); } - - return null; + return Str::asLowerCamelCase($propertyName); } public function addSetter(string $propertyName, ?string $type, bool $isNullable, array $commentLines = []): void From 536c4e0ef9c7ee2e2ca67642148e8da57b4144e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 13:28:09 +0100 Subject: [PATCH 04/10] Fixing str compare & php-cs-fixer --- src/Util/ClassSourceManipulator.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index f910b0219..8c9e632e4 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -282,10 +282,12 @@ private function getGetterName($propertyName, $returnType): ?string if ('bool' !== $returnType) { return 'get' . Str::asCamelCase($propertyName); } + // exclude is & has from getter definition if already in property name - if (!strncasecmp($propertyName, 'is', 2) && !strncasecmp($propertyName, 'has', 3)) { + if (0 !== strncasecmp($propertyName, 'is', 2) && 0 !== strncasecmp($propertyName, 'has', 3)) { return 'is' . Str::asCamelCase($propertyName); } + return Str::asLowerCamelCase($propertyName); } From 265e3235eabfaa28bc5dfb17d6eb6dcdd9a0819e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 13:29:38 +0100 Subject: [PATCH 05/10] Fixing php-cs-fixer --- src/Util/ClassSourceManipulator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 8c9e632e4..b8d6e7786 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -280,12 +280,12 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN private function getGetterName($propertyName, $returnType): ?string { if ('bool' !== $returnType) { - return 'get' . Str::asCamelCase($propertyName); + 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 'is'.Str::asCamelCase($propertyName); } return Str::asLowerCamelCase($propertyName); From 16082aa11f5794abf1c0bac610a25dcac6aa6dc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 13:32:44 +0100 Subject: [PATCH 06/10] Fixing getGetterName return type (always string) --- src/Util/ClassSourceManipulator.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index b8d6e7786..b0a94ec88 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -273,11 +273,8 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN $methodName = $this->getGetterName($propertyName, $returnType); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } - - /** - * @return string|null : getter prefix or null if no prefix to append - */ - private function getGetterName($propertyName, $returnType): ?string + + private function getGetterName($propertyName, $returnType): string { if ('bool' !== $returnType) { return 'get'.Str::asCamelCase($propertyName); From 0281cd90ac2261bf706f7ca7d8cd6b9f8d3cd7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 21:14:21 +0100 Subject: [PATCH 07/10] Enhancing getGetterName signature --- src/Util/ClassSourceManipulator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index b0a94ec88..3a04f3d2a 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -273,8 +273,8 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN $methodName = $this->getGetterName($propertyName, $returnType); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } - - private function getGetterName($propertyName, $returnType): string + + private function getGetterName(string $propertyName, $returnType): string { if ('bool' !== $returnType) { return 'get'.Str::asCamelCase($propertyName); From af7a161e85bbd76a4bf34b206bfcbda04871979e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 21:29:24 +0100 Subject: [PATCH 08/10] Managing 'is' keyword for boolean properties setter --- src/Util/ClassSourceManipulator.php | 13 ++++++++-- tests/Util/ClassSourceManipulatorTest.php | 9 +++++++ .../add_setter/User_bool_begins_with_is.php | 26 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/Util/fixtures/add_setter/User_bool_begins_with_is.php diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 3a04f3d2a..5a4b8c7ae 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -273,7 +273,7 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN $methodName = $this->getGetterName($propertyName, $returnType); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } - + private function getGetterName(string $propertyName, $returnType): string { if ('bool' !== $returnType) { @@ -450,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) { @@ -466,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()); diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index 8fc2ec62f..07fa14784 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -196,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', + ]; } /** diff --git a/tests/Util/fixtures/add_setter/User_bool_begins_with_is.php b/tests/Util/fixtures/add_setter/User_bool_begins_with_is.php new file mode 100644 index 000000000..7b52a6158 --- /dev/null +++ b/tests/Util/fixtures/add_setter/User_bool_begins_with_is.php @@ -0,0 +1,26 @@ +id; + } + + public function setFooProp(bool $isFooProp): static + { + $this->isFooProp = $isFooProp; + + return $this; + } +} From c0381534c0611fb5926609d0e1c19c421c18b43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 21:36:38 +0100 Subject: [PATCH 09/10] Fixing php-cs-fixer --- src/Util/ClassSourceManipulator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 5a4b8c7ae..78365bbba 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -273,7 +273,7 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN $methodName = $this->getGetterName($propertyName, $returnType); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } - + private function getGetterName(string $propertyName, $returnType): string { if ('bool' !== $returnType) { From 85dfab01523d9f9093b43673026729e53ed2f492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Rivi=C3=A8re?= Date: Wed, 27 Mar 2024 21:48:14 +0100 Subject: [PATCH 10/10] Fixing setIsVerified > setVerified EmailVerifier --- src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php b/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php index 6e86e66ca..35a1c4e23 100644 --- a/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php +++ b/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php @@ -43,7 +43,7 @@ public function handleEmailConfirmation(Request $request, UserInterface $user): { $this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, $user->(), $user->()); - $user->setIsVerified(true); + $user->setVerified(true); $this->entityManager->persist($user); $this->entityManager->flush();