From cdf07f61ab3a3eb86e46ea4a1ca8f00e570f6ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=20G=C3=B3mez?= Date: Tue, 3 Oct 2023 16:40:56 +0200 Subject: [PATCH] feat: add ShouldHaveOnlyOnePublicMethod rule --- .../HasOnlyOnePublicMethodRule.php | 4 ++-- .../ShouldHaveOnlyOnePublicMethod.php | 2 +- .../Declaration/OnePublicMethodExtractor.php | 14 ++++++++------ .../fixtures/Special/ClassWithOnePublicMethod.php | 9 ++++++++- .../ClassWithOnlyOnePublicMethodTest.php | 3 +-- ...lementationClassWithOnlyOnePublicMethodTest.php | 4 +--- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Rule/Assertion/Declaration/ShouldHaveOnlyOnePublicMethod/HasOnlyOnePublicMethodRule.php b/src/Rule/Assertion/Declaration/ShouldHaveOnlyOnePublicMethod/HasOnlyOnePublicMethodRule.php index f173636b..2da78bdb 100644 --- a/src/Rule/Assertion/Declaration/ShouldHaveOnlyOnePublicMethod/HasOnlyOnePublicMethodRule.php +++ b/src/Rule/Assertion/Declaration/ShouldHaveOnlyOnePublicMethod/HasOnlyOnePublicMethodRule.php @@ -1,10 +1,10 @@ -getClassReflection()->getName()); - $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC); + $reflectionClass = $node->getClassReflection()->getNativeReflection(); + $methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC); - return count($methods) === 1; + $methodsWithoutConstructor = array_filter( + $methods, + fn (\ReflectionMethod $method) => $method->getName() !== '__construct' + ); + + return count($methodsWithoutConstructor) === 1; } } diff --git a/tests/fixtures/Special/ClassWithOnePublicMethod.php b/tests/fixtures/Special/ClassWithOnePublicMethod.php index 009cea99..2614aa75 100644 --- a/tests/fixtures/Special/ClassWithOnePublicMethod.php +++ b/tests/fixtures/Special/ClassWithOnePublicMethod.php @@ -6,8 +6,15 @@ class ClassWithOnePublicMethod { public const CONSTANT = 'constant'; public string $property = 'property'; + public string $anotherProperty; - public function publicMethod(): bool { + public function __construct() + { + $this->anotherProperty = 'anotherProperty'; + } + + public function publicMethod(): bool + { return true; } } diff --git a/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/ClassWithOnlyOnePublicMethodTest.php b/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/ClassWithOnlyOnePublicMethodTest.php index 663e419b..72b9c67e 100644 --- a/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/ClassWithOnlyOnePublicMethodTest.php +++ b/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/ClassWithOnlyOnePublicMethodTest.php @@ -1,4 +1,4 @@ -getByType(FileTypeMapper::class) ); } - } diff --git a/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/GoodImplementationClassWithOnlyOnePublicMethodTest.php b/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/GoodImplementationClassWithOnlyOnePublicMethodTest.php index 98d44655..dd784e7f 100644 --- a/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/GoodImplementationClassWithOnlyOnePublicMethodTest.php +++ b/tests/unit/rules/ShouldHaveOnlyOnePublicMethod/GoodImplementationClassWithOnlyOnePublicMethodTest.php @@ -1,4 +1,4 @@ -getByType(FileTypeMapper::class) ); } - }