Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/Rule/Extractor/Declaration/PublicMethodNamedExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,19 @@ protected function meetsDeclaration(Node $node, Scope $scope, array $params = []
$reflectionClass = $node->getClassReflection()->getNativeReflection();
$methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);

$methodsWithoutConstructor = array_filter(
$methodsMeetingDeclaration = array_filter(
$methods,
fn ($method) => $method->getName() !== '__construct'
);

foreach ($methodsWithoutConstructor as $method) {
if ($params['isRegex'] === true) {
if (preg_match($params['name'], $method->getName()) !== 1) {
function ($method) use ($params) {
if ($method->getName() === '__construct') {
return false;
}

continue;
}

if ($method->getName() !== $params['name']) {
return false;
return $params['isRegex'] === true
? preg_match($params['name'], $method->getName()) === 1
: $method->getName() === $params['name'];
}
}
);

return true;
return count($methodsMeetingDeclaration) === 1;
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/Special/ClassWithTwoSimilarlyNamedMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Tests\PHPat\fixtures\Special;

final class ClassWithTwoSimilarlyNamedMethods
{
public function exampleOne(): bool
{
return true;
}

public function exampleTwo(): bool
{
return true;
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/Special/ClassWithTwoUnrelatedNamedMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Tests\PHPat\fixtures\Special;

final class ClassWithTwoUnrelatedNamedMethods
{
public function bar(): bool
{
return true;
}

public function foo(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldHaveOnlyOnePublicMethodNamed;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\ShouldHaveOnlyOnePublicMethodNamed\HasOnlyOnePublicMethodNamedRule;
use PHPat\Selector\Classname;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\FixtureClass;
use Tests\PHPat\fixtures\Special\ClassWithTwoUnrelatedNamedMethods;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<HasOnlyOnePublicMethodNamedRule>
* @internal
* @coversNothing
*/
class MoreThanOnePublicMethodNamedWithRegexTest extends RuleTestCase
{
public const RULE_NAME = 'testFixtureClassShouldHaveOnlyOnePublicMethodNamed';

public function testRule(): void
{
// Should succeed as 'foo' is not named like 'ba*'
$this->analyse(['tests/fixtures/Special/ClassWithTwoUnrelatedNamedMethods.php'], []);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ClassWithTwoUnrelatedNamedMethods::class,
[new Classname(FixtureClass::class, false)],
[],
[],
['name' => '/^ba[a-zA-Z0-9]+/', 'isRegex' => true]
);

return new HasOnlyOnePublicMethodNamedRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
self::createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldHaveOnlyOnePublicMethodNamed;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\ShouldHaveOnlyOnePublicMethodNamed\HasOnlyOnePublicMethodNamedRule;
use PHPat\Rule\Assertion\Declaration\ShouldHaveOnlyOnePublicMethodNamed\ShouldHaveOnlyOnePublicMethodNamed;
use PHPat\Selector\Classname;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\Special\ClassWithTwoSimilarlyNamedMethods;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<HasOnlyOnePublicMethodNamedRule>
* @internal
* @coversNothing
*/
class SimilarlyNamedPublicMethodsNamedWithRegexTest extends RuleTestCase
{
public const RULE_NAME = 'testFixtureClassShouldHaveOnlyOnePublicMethodNamed';

public function testRule(): void
{
$this->analyse(['tests/fixtures/Special/ClassWithTwoSimilarlyNamedMethods.php'], [
[sprintf('%s should have only one public method named %s', ClassWithTwoSimilarlyNamedMethods::class, '/^example[a-zA-Z0-9]+/'), 7],
]);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldHaveOnlyOnePublicMethodNamed::class,
[new Classname(ClassWithTwoSimilarlyNamedMethods::class, false)],
[],
[],
['name' => '/^example[a-zA-Z0-9]+/', 'isRegex' => true]
);

return new HasOnlyOnePublicMethodNamedRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
self::createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}