phparkitect / phparkitect
Enforce architectural constraints in your PHP applications
Fund package maintenance!
micheleorselli
AlessandroMinoccheri
Installs: 1 818 008
Dependents: 15
Suggesters: 2
Security: 0
Stars: 754
Watchers: 10
Forks: 40
Open Issues: 47
Requires
- php: ^7.1|^8
- ext-json: *
- nikic/php-parser: ~4
- ondram/ci-detector: ^4.1
- phpstan/phpdoc-parser: ^1.2
- symfony/console: ^3.0|^4.0|^5.0|^6.0|^7.0
- symfony/event-dispatcher: ^3.0|^4.0|^5.0|^6.0|^7.0
- symfony/finder: ^3.0|^4.0|^5.0|^6.0|^7.0
- symfony/polyfill-php80: ^1.20
- webmozart/assert: ^1.9
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mikey179/vfsstream: ^1.6
- phpspec/prophecy: ^1.10
- phpunit/phpunit: ^7.5|^9.0|^10.0
- roave/security-advisories: dev-master
- symfony/var-dumper: ^3.0|^4.0|^5.0|^6.0|^7.0
- vimeo/psalm: ^4.6
- dev-main
- 0.3.33
- 0.3.32
- 0.3.31
- 0.3.30
- 0.3.29
- 0.3.28
- 0.3.27
- 0.3.26
- 0.3.25
- 0.3.24
- 0.3.23
- 0.3.22
- 0.3.21
- 0.3.20
- 0.3.19
- 0.3.18
- 0.3.17
- 0.3.16
- 0.3.15
- 0.3.14
- 0.3.13
- 0.3.12
- 0.3.11
- 0.3.10
- 0.3.9
- 0.3.8
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.32
- 0.2.31
- 0.2.30
- 0.2.29
- 0.2.28
- 0.2.27
- 0.2.26
- 0.2.25
- 0.2.24
- 0.2.23
- 0.2.22
- 0.2.21
- 0.2.20
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.15
- 0.1.14
- 0.1.13
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/github_actions/dot-github/workflows/actions/download-artifact-4.1.7
- dev-php8.3
- dev-fix-ci
- dev-single-letter-class
- dev-fix-cs-fixer-bug-on-ci
- dev-that-immutable
- dev-no-progress-bar-on-ci
- dev-358-regression-with-pattern-validation
- dev-bug-regex-input
- dev-add-php82-to-ci
- dev-dead-code-removed
- dev-inject-classdescription
- dev-issue-323
- dev-no-class
- dev-rename-doc-block-expressions
- dev-expression-test-command
- dev-filter-rule-from-cli
- dev-e2e-refactoring
- dev-add-php82-to-tests
- dev-phar-experiment
- dev-check-command-refactoring
- dev-fix-version
- dev-readme-refresh
- dev-check-dependencies-recursively
- dev-should/exclude
- dev-bug-depends-only-on-namespaces
- dev-do-not-extend
This package is auto-updated.
Last update: 2024-12-03 22:39:19 UTC
README
Introduction
PHPArkitect helps you to keep your PHP codebase coherent and solid, by permitting to add some architectural constraint check to your workflow. You can express the constraint that you want to enforce, in simple and readable PHP code, for example:
Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new HaveNameMatching('*Controller')) ->because('it\'s a symfony naming convention');
Installation
Using Composer
composer require --dev phparkitect/phparkitect
Using a Phar
Sometimes your project can conflict with one or more of PHPArkitect's dependencies. In that case you may find the Phar (a self-contained PHP executable) useful.
The Phar can be downloaded from GitHub:
wget https://github.com/phparkitect/arkitect/releases/latest/download/phparkitect.phar
chmod +x phparkitect.phar
./phparkitect.phar check
Usage
To use this tool you need to launch a command via Bash:
phparkitect check
With this command phparkitect
will search all rules in the root of your project the default config file called phparkitect.php
.
You can also specify your configuration file using --config
option like this:
phparkitect check --config=/project/yourConfigFile.php
By default, a progress bar will show the status of the ongoing analysis.
Using a baseline file
If there are a lot of violations in your codebase and you can't fix them now, you can use the baseline feature to instruct the tool to ignore past violations.
To create a baseline file, run the check
command with the generate-baseline
parameter as follows:
phparkitect check --generate-baseline
This will create a phparkitect-baseline.json
, if you want a different file name you can do it with:
phparkitect check --generate-baseline=my-baseline.json
It will produce a json file with the current list of violations.
If is present a baseline file with the default name will be used automatically.
To use a different baseline file, run the check
command with the use-baseline
parameter as follows:
phparkitect check --use-baseline=my-baseline.json
To avoid using the default baseline file, you can use the skip-baseline
option:
phparkitect check --skip-baseline
Line numbers in baseline
By default, the baseline check also looks at line numbers of known violations. When a line before the offending line changes, the line numbers change and the check fails despite the baseline.
With the optional flag ignore-baseline-linenumbers
, you can ignore the line numbers of violations:
phparkitect check --ignore-baseline-linenumbers
Warning: When ignoring line numbers, phparkitect can no longer discover if a rule is violated additional times in the same file.
Configuration
Example of configuration file phparkitect.php
<?php declare(strict_types=1); use Arkitect\ClassSet; use Arkitect\CLI\Config; use Arkitect\Expression\ForClasses\HaveNameMatching; use Arkitect\Expression\ForClasses\NotHaveDependencyOutsideNamespace; use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; use Arkitect\Rules\Rule; return static function (Config $config): void { $mvcClassSet = ClassSet::fromDir(__DIR__.'/mvc'); $rules = []; $rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new HaveNameMatching('*Controller')) ->because('we want uniform naming'); $rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain')) ->should(new NotHaveDependencyOutsideNamespace('App\Domain')) ->because('we want protect our domain'); $config ->add($mvcClassSet, ...$rules); };
PHPArkitect can detect violations also on DocBlocks custom annotations (like @Assert\NotBlank
or @Serializer\Expose
).
If you want to disable this feature you can add this simple configuration:
$config->skipParsingCustomAnnotations();
Available rules
Hint: If you want to test how a Rule work, you can use the command like phparkitect debug:expression <RuleName> <arguments>
to check which class satisfy the rule in your current folder.
For example: phparkitect debug:expression ResideInOneOfTheseNamespaces App
Currently, you can check if a class:
Depends on a namespace
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain')) ->should(new DependsOnlyOnTheseNamespaces('App\Domain', 'Ramsey\Uuid')) ->because('we want to protect our domain from external dependencies except for Ramsey\Uuid');
Doc block contains a string
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain\Events')) ->should(new ContainDocBlockLike('@psalm-immutable')) ->because('we want to enforce immutability');
Doc block not contains a string
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new NotContainDocBlockLike('@psalm-immutable')) ->because('we don\'t want to enforce immutability');
Extend another class
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new Extend('App\Controller\AbstractController')) ->because('we want to be sure that all controllers extend AbstractController'); You can add multiple parameters, the violation will happen when none of them match
Has an attribute (requires PHP >= 8.0)
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new HaveAttribute('AsController')) ->because('it configures the service container');
Have a name matching a pattern
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Service')) ->should(new HaveNameMatching('*Service')) ->because('we want uniform naming for services');
Implements an interface
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new Implement('ContainerAwareInterface')) ->because('all controllers should be container aware');
Not implements an interface
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Infrastructure\RestApi\Public')) ->should(new NotImplement('ContainerAwareInterface')) ->because('all public controllers should not be container aware');
Is abstract
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Customer\Service')) ->should(new IsAbstract()) ->because('we want to be sure that classes are abstract in a specific namespace');
Is trait
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Customer\Service\Traits')) ->should(new IsTrait()) ->because('we want to be sure that there are only traits in a specific namespace');
Is final
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain\Aggregates')) ->should(new IsFinal()) ->because('we want to be sure that aggregates are final classes');
Is readonly
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain\ValueObjects')) ->should(new IsReadonly()) ->because('we want to be sure that value objects are readonly classes');
Is interface
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Interfaces')) ->should(new IsInterface()) ->because('we want to be sure that all interfaces are in one directory');
Is enum
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Enum')) ->should(new IsEnum()) ->because('we want to be sure that all classes are enum');
Is not abstract
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain')) ->should(new IsNotAbstract()) ->because('we want to avoid abstract classes into our domain');
Is not trait
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain')) ->should(new IsNotTrait()) ->because('we want to avoid traits in our codebase');
Is not final
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Infrastructure\Doctrine')) ->should(new IsNotFinal()) ->because('we want to be sure that our adapters are not final classes');
Is not readonly
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain\Entity')) ->should(new IsNotReadonly()) ->because('we want to be sure that there are no readonly entities');
Is not interface
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('Tests\Integration')) ->should(new IsNotInterface()) ->because('we want to be sure that we do not have interfaces in tests');
Is not enum
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new IsNotEnum()) ->because('we want to be sure that all classes are not enum');
Not depends on a namespace
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Application')) ->should(new NotDependsOnTheseNamespaces('App\Infrastructure')) ->because('we want to avoid coupling between application layer and infrastructure layer');
Not extend another class
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Controller\Admin')) ->should(new NotExtend('App\Controller\AbstractController')) ->because('we want to be sure that all admin controllers not extend AbstractController for security reasons'); You can add multiple parameters, the violation will happen when one of them match
Don't have dependency outside a namespace
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App\Domain')) ->should(new NotHaveDependencyOutsideNamespace('App\Domain', ['Ramsey\Uuid'], true)) ->because('we want protect our domain except for Ramsey\Uuid');
Not have a name matching a pattern
$rules[] = Rule::allClasses() ->that(new ResideInOneOfTheseNamespaces('App')) ->should(new NotHaveNameMatching('*Manager')) ->because('*Manager is too vague in naming classes');
Reside in a namespace
$rules[] = Rule::allClasses() ->that(new HaveNameMatching('*Handler')) ->should(new ResideInOneOfTheseNamespaces('App\Application')) ->because('we want to be sure that all CommandHandlers are in a specific namespace');
Not reside in a namespace
$rules[] = Rule::allClasses() ->that(new Extend('App\Domain\Event')) ->should(new NotResideInTheseNamespaces('App\Application', 'App\Infrastructure')) ->because('we want to be sure that all events not reside in wrong layers');
You can also define components and ensure that a component:
- should not depend on any component
- may depend on specific components
- may depend on any component
Check out this demo project to get an idea on how write rules.
Rule Builders
PHPArkitect offers some builders that enable you to implement more readable rules for specific contexts.
Component Architecture Rule Builder
Thanks to this builder you can define components and enforce dependency constraints between them in a more readable fashion.
<?php declare(strict_types=1); use Arkitect\ClassSet; use Arkitect\CLI\Config; use Arkitect\Expression\ForClasses\HaveNameMatching; use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; use Arkitect\RuleBuilders\Architecture\Architecture; use Arkitect\Rules\Rule; return static function (Config $config): void { $classSet = ClassSet::fromDir(__DIR__.'/src'); $layeredArchitectureRules = Architecture::withComponents() ->component('Controller')->definedBy('App\Controller\*') ->component('Service')->definedBy('App\Service\*') ->component('Repository')->definedBy('App\Repository\*') ->component('Entity')->definedBy('App\Entity\*') ->component('Domain')->definedBy('App\Domain\*') ->where('Controller')->mayDependOnComponents('Service', 'Entity') ->where('Service')->mayDependOnComponents('Repository', 'Entity') ->where('Repository')->mayDependOnComponents('Entity') ->where('Entity')->shouldNotDependOnAnyComponent() ->where('Domain')->shouldOnlyDependOnComponents('Domain') ->rules(); // Other rule definitions... $config->add($classSet, $serviceNamingRule, $repositoryNamingRule, ...$layeredArchitectureRules); };
Excluding classes when parser run
If you want to exclude some classes from the parser you can use the except
function inside your config file like this:
$rules[] = Rule::allClasses() ->except('App\Controller\FolderController\*') ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new HaveNameMatching('*Controller')) ->because('we want uniform naming');
You can use wildcards or the exact name of a class.
Optional parameters and options
You can add parameters when you launch the tool. At the moment you can add these parameters and options:
-v
: with this option you launch Arkitect with the verbose mode to see every parsed file--config
: with this parameter, you can specify your config file instead of the default. like this:
phparkitect check --config=/project/yourConfigFile.php
--target-php-version
: With this parameter, you can specify which PHP version should use the parser. This can be useful to debug problems and to understand if there are problems with a different PHP version. Supported PHP versions are: 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2--stop-on-failure
: With this option the process will end immediately after the first violation.
Run only a specific rule
For some reasons, you might want to run only a specific rule, you can do it using runOnlyThis
like this:
$rules[] = Rule::allClasses() ->except('App\Controller\FolderController\*') ->that(new ResideInOneOfTheseNamespaces('App\Controller')) ->should(new HaveNameMatching('*Controller')) ->because('we want uniform naming') ->runOnlyThis();
Integrations
Laravel
If you plan to use Arkitect with Laravel, smortexa wrote a nice wrapper with some predefined rules for laravel: https://github.com/smortexa/laravel-arkitect