Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ declare(strict_types=1);

- **Все изменения — через ветку + PR.** Прямой пуш в `master` запрещён.
- Создать ветку `task/<short-description>`, сделать коммиты, открыть PR, дождаться CI, смержить, удалить ветку.
- **Ссылка на PR — в каждом ответе**, где PR создан, обновлён или ожидает действия (`https://github.com/prikotov/coding-standard/pull/N`). Не заставляй владельца спрашивать.

Правила работы с git — ветки, коммиты, PR, релизы, `SemVer`:

Expand Down
120 changes: 104 additions & 16 deletions config/deptrac/depfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ deptrac:
layers:
# --- Main layers ---

# Entities only (Domain\Entity\*Model, except Embeddable VO); everything else under
# Domain lives in its own layer: DomainContract, DomainVo, DomainEnum, DomainDto,
# DomainSpecification, DomainEmbeddable.
- name: Domain
collectors:
- type: bool
must:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\.*
must_not:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\(Entity\\)?ValueObject\\.*Vo$
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Specification\\.*
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Enum\\.*
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\.*Dto$
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Entity\\(?!ValueObject\\).*$

- name: Application
collectors:
Expand Down Expand Up @@ -110,7 +102,46 @@ deptrac:
- name: DomainVo
collectors:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\(Entity\\)?ValueObject\\.*Vo$
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\ValueObject\\.*Vo$

# Domain exceptions: thrown by any domain type, caught by the layers that call domain
# services. Not a "contract" — VO, DTO and specifications may use them.
- name: DomainException
collectors:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Exception\\.*

# Domain contracts and pure domain types: everything under Domain that is not an
# entity, value object, enum, DTO or specification — service/repository interfaces
# and implementations, find criteria, calculators, helpers, normalizers, exceptions.
# This is the only part of Domain the Application layer may use directly;
# entities (Domain, DomainEmbeddable) stay behind handlers and repositories.
- name: DomainContract
collectors:
- type: bool
must:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\.*
must_not:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Entity\\.*
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\ValueObject\\.*Vo$
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Enum\\.*
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\.*Dto$
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Specification\\.*
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Exception\\.*

# Embeddable VO are a persistence part of entities: isolated like Domain (entities),
# not shared like DomainVo. See docs/conventions/layers/domain/entity.md.
- name: DomainEmbeddable
collectors:
- type: classLike
value: ^(?:[A-Za-z_][A-Za-z0-9_]*\\)?Common\\Module\\[^\\]+\\Domain\\Entity\\ValueObject\\.*Vo$

- name: DomainEnum
collectors:
Expand Down Expand Up @@ -177,15 +208,36 @@ deptrac:
ruleset:
# DTO boundary
Domain:
- DomainException
- Domain
- DomainContract
- DomainDto
- DomainEnum
- DomainSpecification
- DomainVo
- DomainEmbeddable
DomainVo:
- DomainException
- Domain
- DomainEnum
- DomainVo

DomainEmbeddable:
- DomainEnum

DomainException:
- DomainException
- DomainContract
- DomainVo
- DomainEnum
DomainContract:
- DomainException
- Domain
- DomainContract
- DomainDto
- DomainEnum
- DomainSpecification
- DomainVo
DomainEnum:
- DomainEnum
ApplicationDto:
Expand All @@ -199,6 +251,8 @@ deptrac:
- IntegrationDto
InfrastructureDto:
- Domain
- DomainContract
- DomainException
- DomainDto
- DomainEnum
- DomainVo
Expand All @@ -207,11 +261,14 @@ deptrac:
- InfrastructureDto
DomainDto:
- Domain
- DomainContract
- DomainDto
- DomainEnum
- DomainVo
DomainSpecification:
- Domain
- DomainContract
- DomainException
- DomainDto
- DomainEnum
- DomainSpecification
Expand All @@ -236,6 +293,8 @@ deptrac:
- DomainEnum
- DomainSpecification
- DomainVo
- DomainContract
- DomainException
ApplicationQueryHandler:
- Application
- +ApplicationQuery
Expand All @@ -244,6 +303,8 @@ deptrac:
- DomainEnum
- DomainSpecification
- DomainVo
- DomainContract
- DomainException

# Layer dependencies
Presentation:
Expand All @@ -252,14 +313,30 @@ deptrac:
- +ApplicationQuery
- ApplicationCommandHandler
- ApplicationQueryHandler
# Application orchestrates domain services, repositories and entities
# (docs/conventions/layers/application.md, core-patterns/mapper.md: mappers take Entity).
# Embeddable stay behind entities — not granted. No `+Layer` here: it is a transitive
# closure and would grant the handlers' dependencies.
Application:
- Application
- +ApplicationCommandHandler
- +ApplicationQueryHandler
- ApplicationCommandHandler
- ApplicationQueryHandler
- ApplicationDto
- ApplicationCommonDto
- ApplicationEnum
- Domain
- DomainDto
- DomainEnum
- DomainVo
- DomainSpecification
- DomainContract
- DomainException
Integration:
- Integration
- IntegrationDto
- Application
- DomainContract
- DomainException
- ApplicationCommand
- ApplicationQuery
- ApplicationCommandHandler
Expand All @@ -268,6 +345,7 @@ deptrac:
- ApplicationCommonDto
- ApplicationEnum
- Domain
- DomainEmbeddable
- DomainDto
- DomainEnum
- DomainVo
Expand All @@ -277,6 +355,9 @@ deptrac:
- InfrastructureDto
- InfrastructureEnum
- Domain
- DomainContract
- DomainException
- DomainEmbeddable
- DomainDto
- DomainEnum
- DomainVo
Expand Down Expand Up @@ -316,3 +397,10 @@ services:
autowire: true
tags:
- { name: kernel.event_subscriber }
- class: PrikotovCodingStandard\Deptrac\DomainTypeCatalogRule
autowire: true
tags:
- { name: kernel.event_subscriber }
- class: PrikotovCodingStandard\Deptrac\PolymorphismDependencyRule
tags:
- { name: kernel.event_subscriber }
144 changes: 144 additions & 0 deletions src/Deptrac/DomainTypeCatalogRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

declare(strict_types=1);

namespace PrikotovCodingStandard\Deptrac;

use Deptrac\Deptrac\Contract\Analyser\PostProcessEvent;
use Deptrac\Deptrac\Contract\Analyser\ViolationCreatingInterface;
use Deptrac\Deptrac\Contract\Result\Error;
use Deptrac\Deptrac\Core\Ast\AstMapExtractor;

/**
* Domain types form a closed catalog: a class under `Module\{Module}\Domain\` must
* live in a recognized type directory, and the Domain root namespace may contain
* only interfaces. Anything else — for example `Domain\TransactionFieldNormalizer`
* or `Domain\ValueObject\CleanupErrorList` (without the `Vo` suffix) — fits none of
* the convention types and is therefore a convention violation by definition,
* not a dependency to classify.
*
* This is the deptrac-side twin of MissingVoSuffixSniff (which covers only
* ValueObject): it checks the whole Domain catalog even in projects that do not run
* PHPCS.
*
* Register in depfile.yaml:
* services:
* - class: PrikotovCodingStandard\Deptrac\DomainTypeCatalogRule
* autowire: true
* tags:
* - { name: kernel.event_subscriber }
*/
final class DomainTypeCatalogRule implements ViolationCreatingInterface
{
private const DOC_REF = ' See: docs/conventions/core-patterns/ and docs/conventions/layers/domain.md';

private const MODULE_DOMAIN_PATTERN = '/^(?:[A-Za-z_][A-Za-z0-9_]*\\\\)?Common\\\\Module\\\\'
. '(?P<module>[A-Za-z][A-Za-z0-9]*)\\\\'
. 'Domain\\\\'
. '(?P<path>[^\\\\]+.*)$/';

/**
* Directories of Domain types found in the conventions and in the consumer
* projects (stocks2, TasK, task-orchestrator).
*
* @var list<string>
*/
private const ALLOWED_TYPE_DIRECTORIES = [
'Calculator',
'Criteria',
'Dto',
'Entity',
'Enum',
'Exception',
'Factory',
'Helper',
'List',
'Mapper',
'Provider',
'Repository',
'Service',
'Specification',
'Strategy',
'ValueObject',
];

public function __construct(private readonly AstMapExtractor $astMapExtractor)
{
}

public static function getSubscribedEvents(): array
{
return [
PostProcessEvent::class => ['onPostProcessEvent'],
];
}

public function onPostProcessEvent(PostProcessEvent $event): void
{
foreach ($this->astMapExtractor->extract()->getClassLikeReferences() as $reference) {
$className = $reference->getToken()->toString();
$violation = self::classify($className);
if ($violation !== null) {
$event->getResult()->addError(new Error($violation . '.' . self::DOC_REF));
}
}
}

/**
* Returns the violation message for a class outside the catalog, null if it fits.
*/
public static function classify(string $className): ?string
{
if (1 !== preg_match(self::MODULE_DOMAIN_PATTERN, $className, $matches)) {
return null;
}

$path = $matches['path'];

// Only interfaces directly in the Domain namespace (e.g. ChainDefinitionInterface).
if (str_contains($path, '\\') === false) {
return str_ends_with($path, 'Interface') === false
? sprintf(
'Class "%s" sits directly in the Domain namespace; only interfaces are allowed there. '
. 'The Domain catalog is closed: %s.',
$className,
implode(', ', self::ALLOWED_TYPE_DIRECTORIES),
)
: null;
}

$directory = explode('\\', $path, 2)[0];
if (in_array($directory, self::ALLOWED_TYPE_DIRECTORIES, true) === false) {
return sprintf(
'Class "%s" is in Domain\\%s, which is not a Domain type directory. '
. 'The Domain catalog is closed: %s.',
$className,
$directory,
implode(', ', self::ALLOWED_TYPE_DIRECTORIES),
);
}

// Classes in ValueObject must carry the "Vo" suffix (MissingVoSuffixSniff covers PHPCS projects).
if ($directory === 'ValueObject' && str_ends_with($path, 'Vo') === false) {
return sprintf(
'Class "%s" is in Domain\\ValueObject without the "Vo" suffix. '
. 'Value Object names are suffixed with "Vo" (docs/conventions/core-patterns/value-object.md).',
$className,
);
}

return null;
}

public function ruleName(): string
{
return 'DomainTypeCatalogRule';
}

public function ruleDescription(): string
{
return 'Domain classes must live in a recognized type directory; the Domain root'
. ' namespace contains only interfaces. A class fitting no convention type'
. ' is a violation by definition.';
}
}
Loading
Loading