phpat
phpat copied to clipboard
Assertions on a class structure without relations
Enhancement description At the moment, every assertion requires specifying another selector for a relation. There are no assertions which can assert a certain characteristic of the selected classes. For example, one could want to assert that every class matching a selector is final.
Example usage:
<?php
use PhpAT\Rule\Rule;
use PhpAT\Selector\Selector;
use PhpAT\Test\ArchitectureTest;
class ExampleTest extends ArchitectureTest
{
public function testEntitesAreFinal(): Rule
{
return $this->newRule
->classesThat(Selector::haveClassName('App\Entity\*'))
->mustBeFinal()
->build();
}
}
Would such assertions be accepted as pull requests?
Do you think phpat is the best tool for that (vs rector, php-cs-fixer and tests)?
Suggested approach or solution
Creating a new collection of assertions which look only at the AST of the matched classes and not depend on its relations. Perhaps for these assertions a simple AST processing would be needed.
This is right in the limit between class relations and pure static code analysis 🤔
Theoretically the same assertion could be done with 'App\*' must not extend 'App\Entity\*'. On the other hand, this opens a door for mustBeAbstract, mustBeAnInterface, etc... which can help you in case you want to organise your classes in some specific way.
I will put a debate label on this issue for some time and I will ask for opinion to more people. Feel free to share this issue on social networks so that more people can discuss the usefulness of this kind of features (and if it has place on this tool).
I agree this is may be suitable for a static code analysis tool like PHPStan.
which can help you in case you want to organise your classes in some specific way.
I'm looking at phpat as a tool specifically to enforce a certain class organisation in addition to enforcing dependency rules between modules.
The PHP Architecture Tester is really good at easy composition of selectors of class types and providing generic assertions. Other static code analysis tools are not structured to be configured so easily. For example, in order to configure PHPStan to check for certain classes to be final you need to create a new rule class, detect the class via AST rather than an easy selector, very possibly you'd need a new class representing the classes matching the AST selection and it's not as easy to extend the rule to other class groups.
This could be an argument that PHPStan should be making things easier to configure (e.g. similarly to how you'd configure a predefined rector), but also phpat could add this because of how it provides easy selection of class groups.
Ok, I think some little static checks can be suitable for phpat, because they define how classes are able to relate with others. This can be done but there is previous work to do to accept rules without destination classes. I will put this enhancement on hold until the tool is ready to handle it 😄
FWIW, see this Twitter thread with PHPStan author: https://twitter.com/OndrejMirtes/status/1239531736981274626
Implemented in https://github.com/carlosas/phpat/releases/tag/0.10.1 More assertions on the way 🙂