From e2d6c94fe03f90a43b2f01e774970bff1f6b7ba4 Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Mon, 9 May 2022 17:44:19 +0000 Subject: [PATCH] Use prefix "is" for getters on boolean fields (#456) --- src/Util/ClassSourceManipulator.php | 3 +- tests/Util/ClassSourceManipulatorTest.php | 8 ++++++ .../fixtures/add_getter/User_simple_bool.php | 28 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/Util/fixtures/add_getter/User_simple_bool.php diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 69e26765a..045ad25df 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -253,8 +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); } diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index 732261026..cc072bf44 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -100,6 +100,14 @@ public function getAddGetterTests() 'User_simple.php', ]; + yield 'normal_getter_add_bool' => [ + 'legacy/User_simple.php', + 'fooProp', + 'bool', + [], + 'User_simple_bool.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; + } +}