Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 3 additions & 2 deletions src/Analyzer/PatternString.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public function matches(string $pattern): bool
return false;
}

if (!$this->containsWildcard($pattern) && str_starts_with($this->value, $pattern)) {
return true;
if (!$this->containsWildcard($pattern)) {
$slashedTerminatedPattern = str_ends_with($pattern, '\\')? $pattern : $pattern.'\\';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify these lines to explicit well the meaning?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, much better @fain182

return str_starts_with($this->value, $slashedTerminatedPattern) || $this->value == $pattern;
}

return $this->startsWithPattern($pattern);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Analyzer/ClassDependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function test_it_should_match(): void

public function test_it_should_not_match(): void
{
$this->assertTrue($this->classDependency->matches('Happy'));
$this->assertFalse($this->classDependency->matches('Happy'));
}

public function test_it_should_match_one_of(): void
{
$this->assertTrue($this->classDependency->matchesOneOf('Happy', 'Foo', 'Bar'));
$this->assertTrue($this->classDependency->matchesOneOf('HappyIsland', 'Foo', 'Bar'));
}

public function test_it_should_not_match_one_of(): void
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/Analyzer/FileVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,38 @@ class ApplicationLevelDto

$this->assertCount(1, $violations);
}

public function test_should_implement_exact_classname(): void
{
$code = <<< 'EOF'
<?php

namespace Foo;

interface Order
{
}

interface OrderTwo
{
}

class test implements Order
{
}
EOF;

/** @var FileParser $fp */
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('8.1'));
$fp->parse($code, 'relativePathName');

$cd = $fp->getClassDescriptions();

$violations = new Violations();

$notHaveDependencyOutsideNamespace = new Implement('Foo\Order');
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');

$this->assertCount(0, $violations, $violations->toString());
}
}
8 changes: 6 additions & 2 deletions tests/Unit/Analyzer/FullyQualifiedClassNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public function patternProvider(): array
['Food\Vegetables\Fruits\Banana', 'Food\Vegetables\Fruits\Banana', true],
['Food\Vegetables\Fruits\Banana', 'Food\Vegetables\*\Banana', true],
['Food\Vegetables\Fruits\Banana', 'Food\Vegetables', true],
['Food\Vegetables\Fruits\Banana', 'Food\Vegetables\*', true],
['Food\Vegetables\Fruits\Banana', 'Food\Vegetables\\', true],

['Food\Vegetables\Fruits\Banana', 'Food\Vegetables\*', true],
['Food\Vegetables\Fruits\Mango', '', false],
['Food\Veg', 'Food\Vegetables', false],
['Food\Vegetables', 'Food\Veg', false],
];
}

Expand All @@ -26,7 +30,7 @@ public function test_should_match_namespaces_with_wildcards(string $fqcn, string
{
$fqcn = FullyQualifiedClassName::fromString($fqcn);

$this->assertEquals($shouldMatch, $fqcn->matches($pattern));
$this->assertEquals($shouldMatch, $fqcn->matches($pattern), "{$fqcn->toString()} should ".($shouldMatch ? '' : 'not ')."match $pattern");
}

public function test_should_throw_if_invalid_namespace_is_passed(): void
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Expressions/ForClasses/ImplementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,22 @@ public function test_it_should_return_false_if_depends_on_namespace(): void
$implementConstraint->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}

public function test_it_should_check_the_complete_fqcn(): void
{
$interfaceName = '\Foo\Order';

$implementConstraint = new Implement($interfaceName);
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[FullyQualifiedClassName::fromString('\Foo\Orderable')],
null,
false,
false
);
$violations = new Violations();
$implementConstraint->evaluate($classDescription, $violations, '');
self::assertEquals(1, $violations->count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public function shouldMatchNamespacesProvider(): array
*
* @param mixed $expectedNamespace
* @param mixed $actualFQCN
* @param mixed $explanation
*/
public function test_it_should_match_namespace_and_descendants($expectedNamespace, $actualFQCN): void
public function test_it_should_match_namespace_and_descendants($expectedNamespace, $actualFQCN, $explanation): void
{
$haveNameMatching = new ResideInOneOfTheseNamespaces($expectedNamespace);

Expand All @@ -43,7 +44,7 @@ public function test_it_should_match_namespace_and_descendants($expectedNamespac
$violations = new Violations();
$haveNameMatching->evaluate($classDesc, $violations, $because);

self::assertEquals(0, $violations->count());
self::assertEquals(0, $violations->count(), $explanation);
}

public function test_it_should_return_false_if_not_reside_in_namespace(): void
Expand Down