Skip to content

Commit

Permalink
Fix namespace selector matching similar namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jan 4, 2024
1 parent 334a95f commit d045f3a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Selector/ClassNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function matches(ClassReflection $classReflection): bool
return $this->matchesRegex($namespace);
}

return str_starts_with(\trimSeparators($namespace), \trimSeparators($this->namespace));
return str_starts_with(\trimSeparators($namespace).'\\', \trimSeparators($this->namespace).'\\');
}

private function matchesRegex(string $namespace): bool
Expand Down
14 changes: 7 additions & 7 deletions tests/fixtures/FixtureClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public function usingStaticMethod(): void
}

/**
* @param SimpleClass $p
* @param SimpleClassTwo $p2 Parameter with description
* @param \Tests\PHPat\fixtures\Simple\SimpleClassThree $p3
* @param array<SimpleClassFour> $p4
* @param SimpleClassFive|SimpleClassSix $p5_6
* @param InterfaceWithTemplate<ClassImplementing> $t
* @return SimpleInterface Some nice description here
* @param SimpleClass $p
* @param SimpleClassTwo $p2 Parameter with description
* @param SimpleClassThree $p3
* @param array<SimpleClassFour> $p4
* @param SimpleClassFive|SimpleClassSix $p5_6
* @param InterfaceWithTemplate<ClassImplementing> $t
* @return SimpleInterface Some nice description here
* @throws SimpleException
*/
public function methodWithDocBlocks($p, $p2, $p3, $p4, $p5_6, $t)
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/Ns/Foo/ClassUnderFooNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\fixtures\Ns\Foo;

class ClassUnderFooNamespace {}
5 changes: 5 additions & 0 deletions tests/fixtures/Ns/FooBar/ClassUnderFooBarNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\fixtures\Ns\FooBar;

class ClassUnderFooBarNamespace {}
49 changes: 49 additions & 0 deletions tests/unit/rules/ShouldBeNamed/ClassnamespaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldBeNamed;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\ShouldBeNamed\ClassnameRule;
use PHPat\Rule\Assertion\Declaration\ShouldBeNamed\ShouldBeNamed;
use PHPat\Selector\ClassNamespace;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\Ns\Foo\ClassUnderFooNamespace;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<ClassnameRule>
* @internal
* @coversNothing
*/
class ClassnamespaceTest extends RuleTestCase
{
public const RULE_NAME = 'test_FixtureClassUnderNamespaceShouldBeNamed';

public function testRule(): void
{
// Class under FooBar should not subject to the rule
$this->analyse(['tests/fixtures/Ns/FooBar/ClassUnderFooBarNamespace.php'], []);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldBeNamed::class,
[new ClassNamespace('Tests\PHPat\fixtures\Ns\Foo', false)],
[],
[],
['isRegex' => false, 'classname' => ClassUnderFooNamespace::class]
);

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

0 comments on commit d045f3a

Please sign in to comment.