Skip to content

x: Added phpstan rules to verify access between layers in hexagonal arch… #2

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"name": "Frederik Hørgreen",
"homepage": "https://github.com/frederikhs"
},
{
"name": "Kim Winther Carlsen",
"homepage": "https://github.com/krchiper"
}
Comment on lines +15 to 18
Copy link
Contributor

Choose a reason for hiding this comment

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

husk også at tilføje dig selv til CODEOWNERS filen @ .github/CODEOWNERS. Med stjerne på repo og evt med overskrivning på src/Rules/DomainDrivenDesignRule.php samt fixtures

],
"config": {
Expand Down Expand Up @@ -41,6 +45,11 @@
"Hiper\\PHPStan\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Test\\": "tests/test-fixtures"
Copy link
Contributor

Choose a reason for hiding this comment

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

eneste jeg tænker er om namespace ikke skal være Test\Fixtures\DDD så de var adskilt fra class const reglens fixtures. I samme omfang kunne de også flyttes til en undermappe under test-fixtures (men de har ikke namespace så behøver ikke namespace ændring

}
},
"scripts": {
"phpstan": "phpstan analyse",
"phpunit": "phpunit tests"
Expand Down
6 changes: 5 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ services:
-
class: Hiper\PHPStan\Rules\TypedConstantRule
tags:
- phpstan.rules.rule
- phpstan.rules.rule
-
class: Hiper\PHPStan\Rules\DomainDrivenDesignRule
tags:
- phpstan.rules.rule
124 changes: 124 additions & 0 deletions src/Rules/DomainDrivenDesignRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Hiper\PHPStan\Rules;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<FullyQualified>
*/
class DomainDrivenDesignRule implements Rule {
#[\Override]
public function getNodeType(): string {
return FullyQualified::class;
}

#[\Override]
public function processNode(Node $node, Scope $scope): array {
$currentFile = $scope->getFile();

if (str_contains($currentFile, '/Domain/')) {
return $this->domainRule($node, $scope);
}

if (str_contains($currentFile, '/Application/')) {
return $this->applicationRule($node);
}

return [];
}

/**
* @param FullyQualified $node
*
* @return list<IdentifierRuleError>
*/
protected function domainRule(Node $node, Scope $scope): array {
$usedClass = $node->toCodeString();

if (str_contains($usedClass, '\\Infrastructures\\') || str_contains($usedClass, '\\Infrastructure\\')) {
return [
RuleErrorBuilder::message('The Domain layer must not reference ' . $usedClass . ' from the Infrastructure layer.')
->identifier('ddd.domainAccessInfrastructureViolation')
->build(),
];
}

if (str_contains($usedClass, '\\Applications\\') || str_contains($usedClass, '\\Application\\')) {
return [
RuleErrorBuilder::message('The Domain layer must not reference ' . $usedClass . ' from the Application layer.')
->identifier('ddd.domainAccessApplicationViolation')
->build(),
];
}

$currentFile = $scope->getFile();
if (str_contains($currentFile, '/Models/') || str_contains($currentFile, '/Model/')) {
if (str_contains($usedClass, '\\Repositories\\') || str_contains($usedClass, '\\Repository\\')) {
return [
RuleErrorBuilder::message('The Domain Model layer must not reference ' . $usedClass . ' from the Domain Repository layer.')
->identifier('ddd.domainModelAccessDomainRepositoryViolation')
->build(),
];
}

if (str_contains($usedClass, '\\Services\\') || str_contains($usedClass, '\\Service\\')) {
return [
RuleErrorBuilder::message('The Domain Model layer must not reference ' . $usedClass . ' from the Domain Service layer.')
->identifier('ddd.domainModelAccessDomainServiceViolation')
->build(),
];
}
}
if (str_contains($currentFile, '/Repositories/') || str_contains($currentFile, '/Repository/')) {
if (str_contains($usedClass, '\\Repositories\\') || str_contains($usedClass, '\\Repository\\')) {
return [
RuleErrorBuilder::message('The Domain Reposity layer must not reference ' . $usedClass . ' from the Domain Repository layer.')
->identifier('ddd.domainRepositoryAccessDomainRepositoryViolation')
->build(),
];
}
if (str_contains($usedClass, '\\Services\\') || str_contains($usedClass, '\\Service\\')) {
return [
RuleErrorBuilder::message('The Domain Reposity layer must not reference ' . $usedClass . ' from the Domain Service layer.')
->identifier('ddd.domainRepositoryAccessDomainServiceViolation')
->build(),
];
}
}
if (str_contains($currentFile, '/Services/') || str_contains($currentFile, '/Service/')) {
if (str_contains($usedClass, '\\Repositories\\') || str_contains($usedClass, '\\Repository\\')) {
return [
RuleErrorBuilder::message('The Domain Service layer must not reference ' . $usedClass . ' from the Domain Repository layer.')
->identifier('ddd.domainServiceAccessDomainRepositoryViolation')
->build(),
];
}
}

return [];
}

/**
* @param FullyQualified $node
*
* @return list<IdentifierRuleError>
*/
protected function applicationRule(Node $node): array {
$usedClass = $node->toCodeString();
if (str_contains($usedClass, '\\Infrastructures\\') || str_contains($usedClass, '\\Infrastructure\\')) {
return [
RuleErrorBuilder::message('The Application layer must not reference ' . $usedClass . ' from the Infrastructure layer.')
->identifier('ddd.applicationAccessInfrastructure')
->build(),
];
}

return [];
}
}
56 changes: 56 additions & 0 deletions tests/DomainDrivenDesignRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

use Hiper\PHPStan\Rules\DomainDrivenDesignRule;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @phpstan-type FixturePaths string[]
* @phpstan-type ExpectedError array{0: string, 1: int, 2?: string|null}
* @phpstan-type ExpectedErrors list<ExpectedError>
* @phpstan-type Fixture array{0: FixturePaths, 1: ExpectedErrors}
* @phpstan-type Fixtures Fixture[]
*
* @extends RuleTestCase<DomainDrivenDesignRule>
*/
class DomainDrivenDesignRuleTest extends RuleTestCase {
protected function getRule(): Rule {
return new DomainDrivenDesignRule();
}

/** @return Fixtures */
public static function fixtureProvider(): array {
return [
[[__DIR__ . '/test-fixtures/Application/Service/ApplicationClassThatAccessDomain.php'], []],
[[__DIR__ . '/test-fixtures/Application/Service/ApplicationClassThatAccessRepository.php'], []],
[[__DIR__ . '/test-fixtures/Application/Service/ApplicationClassThatAccessService.php'], []],
[[__DIR__ . '/test-fixtures/Application/Service/ApplicationClassThatAccessApplication.php'], []],
[[__DIR__ . '/test-fixtures/Application/Service/ApplicationClassThatAccessInfrastructure.php'], [['The Application layer must not reference \\Test\\Infrastructure\\Infrastructure from the Infrastructure layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Models/DomainClassThatAccessDomain.php'], []],
[[__DIR__ . '/test-fixtures/Domain/Models/DomainClassThatAccessApplication.php'], [['The Domain layer must not reference \\Test\\Application\\Service\\ApplicationClassThatAccessDomain from the Application layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Models/DomainClassThatAccessService.php'], [['The Domain Model layer must not reference \\Test\\Domain\\Service\\ServiceClassThatAccessDomain from the Domain Service layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Models/DomainClassThatAccessRepository.php'], [['The Domain Model layer must not reference \\Test\\Domain\\Repositories\\RepositoryClassThatAccessDomain from the Domain Repository layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Models/DomainClassThatAccessInfrastructure.php'], [['The Domain layer must not reference \\Test\\Infrastructure\\Infrastructure from the Infrastructure layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Repositories/RepositoryClassThatAccessDomain.php'], []],
[[__DIR__ . '/test-fixtures/Domain/Repositories/RepositoryClassThatAccessRepository.php'], [['The Domain Reposity layer must not reference \\Test\\Domain\\Repositories\\RepositoryClassThatAccessDomain from the Domain Repository layer.', 6]]],
[[__DIR__ . '/test-fixtures/Domain/Repositories/RepositoryClassThatAccessService.php'], [['The Domain Reposity layer must not reference \\Test\\Domain\\Service\\ServiceClassThatAccessDomain from the Domain Service layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Repositories/RepositoryClassThatAccessApplication.php'], [['The Domain layer must not reference \\Test\\Application\\Service\\ApplicationClassThatAccessDomain from the Application layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Repositories/RepositoryClassThatAccessInfrastructure.php'], [['The Domain layer must not reference \\Test\\Infrastructure\\Infrastructure from the Infrastructure layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Service/ServiceClassThatAccessDomain.php'], []],
[[__DIR__ . '/test-fixtures/Domain/Service/ServiceClassThatAccessRepository.php'], [['The Domain Service layer must not reference \\Test\Domain\Repositories\RepositoryClassThatAccessDomain from the Domain Repository layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Service/ServiceClassThatAccessService.php'], []],
[[__DIR__ . '/test-fixtures/Domain/Service/ServiceClassThatAccessApplication.php'], [['The Domain layer must not reference \\Test\\Application\Service\\ApplicationClassThatAccessDomain from the Application layer.', 8]]],
[[__DIR__ . '/test-fixtures/Domain/Service/ServiceClassThatAccessInfrastructure.php'], [['The Domain layer must not reference \\Test\\Infrastructure\\Infrastructure from the Infrastructure layer.', 8]]],
];
}

/**
* @param FixturePaths $paths_to_fixture
* @param ExpectedErrors $expected_errors
*/
#[DataProvider('fixtureProvider')]
public function testDomainDrivenDesignRules(array $paths_to_fixture, array $expected_errors): void {
$this->analyse($paths_to_fixture, $expected_errors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Test\Application\Service;

class ApplicationClassThatAccessApplication {
public function __construct(ApplicationClassThatAccessApplication $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Test\Application\Service;

class ApplicationClassThatAccessDomain {
public function __construct(ApplicationClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Application\Service;

use Test\Infrastructure\Infrastructure;

class ApplicationClassThatAccessInfrastructure {
public function __construct(Infrastructure $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Application\Service;

use Test\Domain\Repositories\ServiceClasssThatAccessDomain;

class ApplicationClassThatAccessRepository {
public function __construct(ServiceClasssThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Application\Service;

use Test\Domain\Service\ServiceClassThatAccessDomain;

class ApplicationClassThatAccessService {
public function __construct(ServiceClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Models;

use Test\Application\Service\ApplicationClassThatAccessDomain;

class DomainClassThatAccessApplication {
public function __construct(ApplicationClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Test\Domain\Models;

class DomainClassThatAccessDomain {
public function __construct(DomainClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Models;

use Test\Infrastructure\Infrastructure;

class DomainClassThatAccessInfrastructure {
public function __construct(Infrastructure $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Models;

use Test\Domain\Repositories\RepositoryClassThatAccessDomain;

class DomainClassThatAccessRepository {
public function __construct(RepositoryClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Models;

use Test\Domain\Service\ServiceClassThatAccessDomain;

class DomainClassThatAccessService {
public function __construct(ServiceClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Repositories;

use Test\Application\Service\ApplicationClassThatAccessDomain;

class RepositoryClassThatAccessApplication {
public function __construct(ApplicationClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Repositories;

use Test\Domain\Models\DomainClassThatAccessDomain;

class RepositoryClassThatAccessDomain {
public function __construct(DomainClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Repositories;

use Test\Infrastructure\Infrastructure;

class RepositoryClassThatAccessInfrastructure {
public function __construct(Infrastructure $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Test\Domain\Repositories;

class RepositoryClassThatAccessRepository {
public function __construct(RepositoryClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Repositories;

use Test\Domain\Service\ServiceClassThatAccessDomain;

class RepositoryClassThatAccessService {
public function __construct(ServiceClassThatAccessDomain $_) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Test\Domain\Service;

use Test\Application\Service\ApplicationClassThatAccessDomain;

class ServiceClassThatAccessApplication {
public function __construct(ApplicationClassThatAccessDomain $_) {

}
}
Loading