<?php #[Attribute()] final class Sealed { public array $permitted = []; public function __construct(string ...$permitted) { $this->permitted = $permitted; } } function validate(string $classname): void { $class = new ReflectionClass($classname); $validate_against = static function($parent) use($classname) { if ($sealed = $parent->getAttributes(Sealed::class)) { if (!in_array($classname, $sealed[0]->newInstance()->permitted, true)) { echo "> ERROR: $classname cannot inherit from sealed ". ( $parent->isInterface() ? 'interface' : ($parent->isTrait() ? 'trait' : 'class') ) ." ".$parent->getName()."\n"; return false; } } return true; }; if ($parent = $class->getParentClass()) { if (!$validate_against($parent)) { return; } } foreach($class->getInterfaces() as $interface) { if (!$validate_against($interface)) { return; } } foreach($class->getTraits() as $trait) { if (!$validate_against($trait)) { return; } } echo "> OK: $classname\n"; } #[Sealed(Bar::class)] interface FooInterface { } #[Sealed(Baz::class)] trait FooTrait { } final class Bar implements FooInterface {} final class Baz implements FooInterface { use FooTrait; } #[Sealed(Quux::class)] class Qux { use FooTrait; } class Quux extends Qux {} class Biz extends Qux {} validate(Bar::class); validate(Baz::class); validate(Qux::class); validate(Quux::class); validate(Biz::class);
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`