đ Method Assertions â
toHaveMethod() â
$this
->allClasses()
->fromRaw('<?php class Foo { public function bar() {} }')
->should(fn(Expr $expr) => $expr->toHaveMethod('bar'));toNotHaveMethod() â
Opposite of toHaveMethod(). Fails if the resource declares the given method.
- Forbid magic accessors:
toNotHaveMethod('__get'). - Keep Value Objects free of setters:
toNotHaveMethod('setValue').
$this
->allClasses()
->fromDir('src/Domain')
->should(fn(Expr $expr) => $expr->toNotHaveMethod('__get'));toHaveNoStaticMethod() â
Fails for every static method declared by the resource, whatever its visibility. Static properties and static closures are not methods and are ignored.
Example: Forbid static constructors (make(), fromArray()âĻ) on classes resolved by the container.
$this
->allClasses()
->fromDir('src/Domain/*/Action')
->should(fn(Expr $expr) => $expr->toHaveNoStaticMethod());toHaveOnlyPrivateProperties() â
Fails for every property that is not private: declared properties (static or not) and properties promoted by the constructor. One violation is reported per property, at its line.
$this
->allClasses()
->fromDir('src/Domain/*/Action')
->should(fn(Expr $expr) => $expr->toHaveOnlyPrivateProperties());toHaveOnlyPublicMethods() â
Fails for every public method whose name is not in the given list; private and protected methods are always allowed. Method names are compared case-insensitively, as in PHP.
Example: keep invokable classes focused: only __construct() and __invoke() are public.
$this
->allClasses()
->fromDir('src/Domain/*/Action')
->should(fn(Expr $expr) => $expr->toHaveOnlyPublicMethods(['__construct', '__invoke']));toHaveOnlyPublicProperties() â
Opposite constraint of toHaveOnlyPrivateProperties(): fails for every property that is not public, declared or promoted by the constructor. A promoted readonly property without visibility is public. Useful for DTOs built on public readonly properties.
$this
->allClasses()
->fromDir('src/Dto')
->should(fn(Expr $expr) => $expr->toHaveOnlyPublicProperties());toHaveConstructor() â
$this
->allClasses()
->should(fn(Expr $expr) => $expr->toHaveConstructor());toNotHaveConstructor() â
Opposite of toHaveConstructor(). Shortcut for toNotHaveMethod('__construct').
$this
->allClasses()
->fromDir('src/Enum')
->should(fn(Expr $expr) => $expr->toNotHaveConstructor());toHaveDestructor() â
$this
->allClasses()
->should(fn(Expr $expr) => $expr->toHaveDestructor());