Custom Assert â
To create a custom rule:
- For class analysis, implement the
StructuraPhp\Structura\Contracts\ExprInterfaceinterface. - For script analysis, implement the
StructuraPhp\Structura\Contracts\ExprScriptInterfaceinterface.
Creating a Custom Rule â
php
<?php
final readonly class CustomRule implements ExprInterface
{
public function __construct(
private string $message = '',
) {}
public function __toString(): string
{
return 'exemple'; // Name of the rule
}
public function assert(ClassDescription $class): bool
{
return true; // Must return false if the test fails
}
public function getViolation(ClassDescription $class): ViolationValueObject
{
return new ViolationValueObject(
'error message', // Console output
$this::class,
$class->lines,
$class->getFileBasename(),
$this->message,
);
}
}Using a Custom Rule â
To use a custom rule, add it using the addExpr() method:
php
$this
->allClasses()
->fromRaw('<?php class Foo {}')
->should(fn(Expr $expr) => $expr
->addExpr(new CustomRule('foo'))
);TIP
Use existing rules as examples for implementing your own.