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) ); } - }