Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclude root #423

Merged
merged 3 commits into from
Apr 30, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ $rules[] = Rule::allClasses()
```php
$rules[] = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Domain'))
->should(new NotHaveDependencyOutsideNamespace('App\Domain', ['Ramsey\Uuid']))
->should(new NotHaveDependencyOutsideNamespace('App\Domain', ['Ramsey\Uuid'], true))
->because('we want protect our domain except for Ramsey\Uuid');
```

Expand Down
2 changes: 1 addition & 1 deletion src/Analyzer/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(

$lexer = new Emulative([
'usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'],
'phpVersion' => $targetPhpVersion->get() ?? phpversion(),
'phpVersion' => $targetPhpVersion->get() ?? phpversion(),
]);

$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $lexer);
Expand Down
4 changes: 2 additions & 2 deletions src/Analyzer/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class NameResolver extends NodeVisitorAbstract
* @param ErrorHandler|null $errorHandler Error handler
* @param array $options Options
*/
public function __construct(ErrorHandler $errorHandler = null, array $options = [])
public function __construct(?ErrorHandler $errorHandler = null, array $options = [])
{
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing());
$this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
Expand Down Expand Up @@ -307,7 +307,7 @@ protected function resolveAttrGroups(Node $node): void
}
}

private function addAlias(Stmt\UseUse $use, int $type, Name $prefix = null): void
private function addAlias(Stmt\UseUse $use, int $type, ?Name $prefix = null): void
{
// Add prefix for group uses
/** @var Name $name */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class NotHaveDependencyOutsideNamespace implements Expression
private $namespace;
/** @var array */
private $externalDependenciesToExclude;
/** @var bool */
private $excludeCoreNamespace;

public function __construct(string $namespace, array $externalDependenciesToExclude = [])
public function __construct(string $namespace, array $externalDependenciesToExclude = [], bool $excludeCoreNamespace = false)
{
$this->namespace = $namespace;
$this->externalDependenciesToExclude = $externalDependenciesToExclude;
$this->excludeCoreNamespace = $excludeCoreNamespace;
}

public function describe(ClassDescription $theClass, string $because): Description
Expand All @@ -46,6 +49,10 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str
continue;
}

if ($this->excludeCoreNamespace && '' === $externalDep->getFQCN()->namespace()) {
continue;
}

$violation = Violation::createWithErrorLine(
$theClass->getFQCN(),
ViolationMessage::withDescription(
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Violation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Violation implements \JsonSerializable
/** @var string */
private $error;

public function __construct(string $fqcn, string $error, int $line = null)
public function __construct(string $fqcn, string $error, ?int $line = null)
{
$this->fqcn = $fqcn;
$this->error = $error;
Expand Down
8 changes: 4 additions & 4 deletions tests/E2E/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public function test_you_can_ignore_the_default_baseline(): void

protected function runCheck(
$configFilePath = null,
bool $stopOnFailure = null,
string $useBaseline = null,
?bool $stopOnFailure = null,
?string $useBaseline = null,
$generateBaseline = false,
bool $skipBaseline = false
): ApplicationTester {
Expand Down Expand Up @@ -181,7 +181,7 @@ protected function runCheck(
return $appTester;
}

protected function assertCheckHasErrors(ApplicationTester $applicationTester, string $expectedOutput = null): void
protected function assertCheckHasErrors(ApplicationTester $applicationTester, ?string $expectedOutput = null): void
{
$this->assertEquals(self::ERROR_CODE, $applicationTester->getStatusCode());
if (null != $expectedOutput) {
Expand All @@ -191,7 +191,7 @@ protected function assertCheckHasErrors(ApplicationTester $applicationTester, st
}
}

protected function assertCheckHasNoErrorsLike(ApplicationTester $applicationTester, string $expectedOutput = null): void
protected function assertCheckHasNoErrorsLike(ApplicationTester $applicationTester, ?string $expectedOutput = null): void
{
$this->assertEquals(self::ERROR_CODE, $applicationTester->getStatusCode());
if (null != $expectedOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function test_it_should_return_false_if_depends_on_namespace(): void
$notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace('myNamespace');
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[new ClassDependency('myNamespace', 100), new ClassDependency('another\class', 200)],
[
new ClassDependency('myNamespace', 100),
new ClassDependency('another\class', 200),
new ClassDependency('\DateTime', 300),
],
[],
null,
false,
Expand All @@ -77,7 +81,7 @@ public function test_it_should_return_false_if_depends_on_namespace(): void
$because = 'we want to add this rule for our software';
$violations = new Violations();
$notHaveDependencyOutsideNamespace->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());
self::assertEquals(2, $violations->count());
}

public function test_it_should_not_return_violation_error_if_dependency_excluded(): void
Expand All @@ -100,4 +104,25 @@ public function test_it_should_not_return_violation_error_if_dependency_excluded
$notHaveDependencyOutsideNamespace->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}

public function test_it_should_not_return_violation_error_if_core_dependency_excluded(): void
{
$notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace('myNamespace', [], true);
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[new ClassDependency('\DateTime', 100)],
[],
null,
false,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$notHaveDependencyOutsideNamespace->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
}
Loading