Skip to content

Getting started

Installation

Require PHPat with Composer:

composer require --dev phpat/phpat

Activate the extension using one of the following methods:

Automatic activation
composer require --dev phpstan/extension-installer
Manual activation
# 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
⚠️ Your architecture tests folder should be included in the PHPStan analysed paths.

You can configure some PHPat options as follows:

# phpstan.neon
parameters:
    phpat:
        ignore_built_in_classes: true

See the complete list of options in the Configuration section.

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;

final 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)
            )
            ->because('this will break our architecture, implement it another way! see /docs/howto.md');
    }
}

Usage

Run PHPStan as usual:

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