Skip to content

Commit

Permalink
feat: add ShouldHaveOnlyOnePublicMethod rule
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Oct 3, 2023
1 parent cf8fac1 commit cdf07f6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldHaveOnlyOnePublicMethod;

use PHPat\Rule\Extractor\Declaration\OnePublicMethodExtractor;

class HasOnlyOnePublicMethodRule extends ShouldHaveOnlyOnePublicMethod
final class HasOnlyOnePublicMethodRule extends ShouldHaveOnlyOnePublicMethod
{
use OnePublicMethodExtractor;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldHaveOnlyOnePublicMethod;

Expand Down
14 changes: 8 additions & 6 deletions src/Rule/Extractor/Declaration/OnePublicMethodExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\MethodReflection;
use ReflectionClass;
use ReflectionMethod;

trait OnePublicMethodExtractor
{
Expand All @@ -21,9 +18,14 @@ public function getNodeType(): string
*/
protected function meetsDeclaration(Node $node, Scope $scope): bool
{
$reflectionClass = new ReflectionClass($node->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;
}
}
9 changes: 8 additions & 1 deletion tests/fixtures/Special/ClassWithOnePublicMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldHaveOnlyOnePublicMethod;

Expand Down Expand Up @@ -45,5 +45,4 @@ protected function getRule(): Rule
self::getContainer()->getByType(FileTypeMapper::class)
);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldHaveOnlyOnePublicMethod;

Expand All @@ -10,7 +10,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\FixtureClass;
use Tests\PHPat\fixtures\Special\ClassWithOnePublicMethod;
use Tests\PHPat\unit\FakeTestParser;

Expand Down Expand Up @@ -44,5 +43,4 @@ protected function getRule(): Rule
self::getContainer()->getByType(FileTypeMapper::class)
);
}

}

0 comments on commit cdf07f6

Please sign in to comment.