đ§Ŧ Type Assertions â
toBeAbstract() â
$this
->allClasses()
->fromRaw('<?php abstract class Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeAbstract(),
);toNotBeAbstract() â
Opposite of toBeAbstract(). Interfaces, traits, enums and anonymous classes always pass: only an abstract class is a violation.
$this
->allClasses()
->fromDir('src/Controller')
->should(
static fn (Expr $assert): Expr => $assert->toNotBeAbstract(),
);toBeAnonymousClasses() â
A PHP script is considered an anonymous class only if it explicitly returns the anonymous class using a return statement at the root level of the script.
WARNING
Simply instantiating an anonymous class (new class {}) is not sufficient. The script must use return new class {} to be recognized as an anonymous class.
$this
->allClasses()
->fromRaw('<?php return new class {};')
->should(
static fn (Expr $assert): Expr => $assert->toBeAnonymousClasses(),
);toBeClasses() â
$this
->allClasses()
->fromRaw('<?php class Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeClasses(),
);toBeEnums() â
Must be a valid Unit Enum or Backed Enum.
$this
->allClasses()
->fromRaw('<?php enum Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeEnums(),
);toBeBackedEnums() â
Must be a backed enumeration. If ScalarType is not specified, int and string are accepted. See PHP Backed Enumerations.
use StructuraPhp\Structura\Enums\ScalarType;
$this
->allClasses()
->fromRaw('<?php enum Foo: string {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeBackedEnums(ScalarType::String),
);toBeFinal() â
$this
->allClasses()
->fromRaw('<?php final class Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeFinal(),
);toNotBeFinal() â
Opposite of toBeFinal(). Interfaces, traits, enums and anonymous classes always pass: only a final class is a violation. Useful for base classes meant to be extended.
$this
->allClasses()
->fromDir('src/Base')
->should(
static fn (Expr $assert): Expr => $assert->toNotBeFinal(),
);toBeInterfaces() â
$this
->allClasses()
->fromRaw('<?php interface Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeInterfaces(),
);toBeInvokable() â
$this
->allClasses()
->fromRaw('<?php class Foo { public function __invoke() {} }')
->should(
static fn (Expr $assert): Expr => $assert->toBeInvokable(),
);toNotBeInvokable() â
Opposite of toBeInvokable(). Shortcut for toNotHaveMethod('__invoke'): fails if the resource declares an __invoke() method.
$this
->allClasses()
->fromDir('src/Domain')
->should(
static fn (Expr $assert): Expr => $assert->toNotBeInvokable(),
);toBeReadonly() â
$this
->allClasses()
->fromRaw('<?php readonly class Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeReadonly(),
);toBeTraits() â
$this
->allClasses()
->fromRaw('<?php trait Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeTraits(),
);toBeAttributes() â
Must be:
- A syntax-compliant attribute
- Instantiable by a class reflection
- Using valid flags
$this
->allClasses()
->fromRaw('<?php #[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)] class Foo {}')
->should(
static fn (Expr $assert): Expr => $assert->toBeAttributes(\Attribute::TARGET_CLASS_CONSTANT),
);