/phpat

PHP Architecture Tester - Easy to use architectural testing tool for PHP :heavy_check_mark:

Primary LanguagePHPMIT LicenseMIT

PHP Architecture Tester

Easy to use architecture testing tool for PHP

Version PHP Version PHPStan Version Contributions welcome


ℹ️ PHPat has been transformed into a PHPStan extension. Read the UPGRADE notes.
The standalone version (v0.9) will remain available and will receive critical bugfixes if required.

Introduction 📜

PHP Architecture Tester is a static analysis tool designed to verify architectural requirements.

It provides a natural language abstraction that enables you to define your own architectural rules and and assess their compliance in your code.

Check out the section WHAT TO TEST to see some examples of typical use cases.

Installation 💽

Require PHPat with Composer:

composer require --dev phpat/phpat

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, enable the extension in your PHPStan configuration:

# phpstan.neon
includes:
    - vendor/phpat/phpat/extension.neon

Configuration 🔧

You will need to register your test classes in your PHPStan configuration:

# phpstan.neon
services:
    -
        class: Tests\Architecture\MyFirstTest
        tags:
            - phpat.test
    -
        class: Tests\Architecture\MySecondTest
        tags:
            - phpat.test

⚠️ Your architecture tests folder should be included in the PHPStan analysed paths. See PHPat's own PHPat configuration as an example.

You can configure some PHPat options as follows:

# phpstan.neon
parameters:
    phpat:
        ignore_built_in_classes: true
Complete list of options
Name Description Default
ignore_doc_comments Ignore relations on Doc Comments false
ignore_built_in_classes Ignore relations with PHP+ext classes false
show_rule_names Show rule name to assertion message false

Test definition 📓

There are different Selectors available to select the classes involved in a rule, and a wide set of Assertions.

Here's an example test with a rule:

<?php

use PHPat\Selector\Selector;
use PHPat\Test\Builder\Rule;
use PHPat\Test\PHPat;
use App\Domain\SuperForbiddenClass;

class MyFirstTest
{
    public function test_domain_does_not_depend_on_other_layers(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::namespace('App\Domain'))
            ->shouldNotDependOn()
            ->classes(
                Selector::namespace('App\Application'),
                Selector::namespace('App\Infrastructure'),
                Selector::classname(SuperForbiddenClass::class),
                Selector::classname('/^SomeVendor\\\.*\\\ForbiddenSubfolder\\\.*/', true)
            );
    }
}

Usage 🚀

Run PHPStan as usual:

php vendor/bin/phpstan analyse -c phpstan.neon

Warning
The launch of early-stage releases (0.x.x) could break the API according to Semantic Versioning 2.0. We are using minor for breaking changes until the release of the stable 1.0.0 version.

PHP Architecture Tester is open source, contributions are welcome. Please have a look to the Contribution docs.


Sponsors (free license)

JetBrains Logo BlackFire Logo