From c9d19aa31e72f0e87d1a7193cf356ac0a07c55f3 Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Tue, 13 Aug 2019 15:48:28 +0200 Subject: [PATCH 1/6] Use prefix "is" for getters on boolean fields --- 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 69e26765a..eb5529815 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -253,7 +253,7 @@ public function addAccessorMethod(string $propertyName, string $methodName, $ret public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): void { - $methodName = 'get'.Str::asCamelCase($propertyName); + $methodName = ('bool' === $returnType ? 'is' : 'get').Str::asCamelCase($propertyName); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } From a3454949686cc5330f997a6d77cd89da09582776 Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Wed, 14 Aug 2019 09:42:19 +0200 Subject: [PATCH 2/6] Handle property names prefixed with "is" If a property is named "isFoo" (or "is_foo"), the getter for this property should be named "isFoo()" instead of "isIsFoo()". --- src/Util/ClassSourceManipulator.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index eb5529815..63286a390 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -253,7 +253,13 @@ 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); + if ('bool' === $returnType) { + $snakeCasedPropertyName = Str::asSnakeCase($propertyName); + $propertyNameWithoutPrefix = (0 === strpos($snakeCasedPropertyName, 'is_')) ? substr($snakeCasedPropertyName, 3) : $propertyName; + $methodName = 'is'.Str::asCamelCase($propertyNameWithoutPrefix); + } else { + $methodName = 'get'.Str::asCamelCase($propertyName); + } $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } From 2799c3a7b3ad2b4d4ca62fcd2be8cc008bc248df Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Sun, 18 Aug 2019 19:43:59 +0200 Subject: [PATCH 3/6] Add unit test --- tests/Util/ClassSourceManipulatorTest.php | 16 +++++++++++ .../fixtures/add_getter/User_simple_bool.php | 28 +++++++++++++++++++ .../add_getter/User_simple_bool_prefixed.php | 28 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 tests/Util/fixtures/add_getter/User_simple_bool.php create mode 100644 tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index 732261026..9eea0ab59 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -100,6 +100,22 @@ public function getAddGetterTests() 'User_simple.php', ]; + yield 'normal_getter_add_bool' => [ + 'User_simple.php', + 'fooProp', + 'bool', + [], + 'User_simple_bool.php' + ]; + + yield 'normal_getter_add_bool_prefixed' => [ + 'User_simple.php', + 'isFooProp', + 'bool', + [], + 'User_simple_bool_prefixed.php' + ]; + yield 'getter_no_props_comments' => [ 'User_no_props.php', 'fooProp', diff --git a/tests/Util/fixtures/add_getter/User_simple_bool.php b/tests/Util/fixtures/add_getter/User_simple_bool.php new file mode 100644 index 000000000..1b0eb7c41 --- /dev/null +++ b/tests/Util/fixtures/add_getter/User_simple_bool.php @@ -0,0 +1,28 @@ +id; + } + + public function isFooProp(): ?bool + { + return $this->fooProp; + } +} diff --git a/tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php b/tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php new file mode 100644 index 000000000..129a6a7af --- /dev/null +++ b/tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php @@ -0,0 +1,28 @@ +id; + } + + public function isFooProp(): ?bool + { + return $this->isFooProp; + } +} From ca1a2f15235a790ce90ff26e05a85f2526b88fa3 Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Fri, 20 Sep 2019 18:56:08 +0200 Subject: [PATCH 4/6] Do *not* handle property names prefixed with "is" --- src/Util/ClassSourceManipulator.php | 9 +----- tests/Util/ClassSourceManipulatorTest.php | 8 ------ .../add_getter/User_simple_bool_prefixed.php | 28 ------------------- 3 files changed, 1 insertion(+), 44 deletions(-) delete mode 100644 tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 63286a390..045ad25df 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -253,14 +253,7 @@ public function addAccessorMethod(string $propertyName, string $methodName, $ret public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): void { - if ('bool' === $returnType) { - $snakeCasedPropertyName = Str::asSnakeCase($propertyName); - $propertyNameWithoutPrefix = (0 === strpos($snakeCasedPropertyName, 'is_')) ? substr($snakeCasedPropertyName, 3) : $propertyName; - $methodName = 'is'.Str::asCamelCase($propertyNameWithoutPrefix); - } else { - $methodName = 'get'.Str::asCamelCase($propertyName); - } - + $methodName = ('bool' === $returnType ? 'is' : 'get').Str::asCamelCase($propertyName); $this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines); } diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index 9eea0ab59..7f9129c06 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -108,14 +108,6 @@ public function getAddGetterTests() 'User_simple_bool.php' ]; - yield 'normal_getter_add_bool_prefixed' => [ - 'User_simple.php', - 'isFooProp', - 'bool', - [], - 'User_simple_bool_prefixed.php' - ]; - yield 'getter_no_props_comments' => [ 'User_no_props.php', 'fooProp', diff --git a/tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php b/tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php deleted file mode 100644 index 129a6a7af..000000000 --- a/tests/Util/fixtures/add_getter/User_simple_bool_prefixed.php +++ /dev/null @@ -1,28 +0,0 @@ -id; - } - - public function isFooProp(): ?bool - { - return $this->isFooProp; - } -} From 128b863987bf07a81fce6a6bff43e49747f620a0 Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Sun, 20 Sep 2020 17:41:54 +0200 Subject: [PATCH 5/6] CS --- tests/Util/ClassSourceManipulatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index 7f9129c06..c9158d5bf 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -105,7 +105,7 @@ public function getAddGetterTests() 'fooProp', 'bool', [], - 'User_simple_bool.php' + 'User_simple_bool.php', ]; yield 'getter_no_props_comments' => [ From 97f892f36df3b962fdc87ee7b8b5ead3828deabc Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Thu, 21 Oct 2021 18:18:24 +0200 Subject: [PATCH 6/6] Fix test --- tests/Util/ClassSourceManipulatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index c9158d5bf..cc072bf44 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -101,7 +101,7 @@ public function getAddGetterTests() ]; yield 'normal_getter_add_bool' => [ - 'User_simple.php', + 'legacy/User_simple.php', 'fooProp', 'bool', [],