php-generator
php-generator copied to clipboard
Make `ClassLike::from` return type assert the subclass type
- new feature
- BC break? yes
- doc PR: nette/docs#???
When reading a ClassType
from a FQCN or a file, the result can be anything from the 4 subclasses. When we know the type of the object that is parsed, having a more strict return type has additional benefits:
- Assert the object type, and throw a PHP error if the type is not the expected one.
- Helps static analysis to identifies the methods that can be used on the object (IDE completion, error detection)
Usage:
If the type is unknown, use ClassLike::from($class)
.
Otherwise, when the expected type is known, use the more specific ClassType::from($fqcn)
, TraitType::from($fqcn)
, InterfaceType::from($fqcn)
, or EnumType::from($fqcn)
.
This is a breaking change for users that call ClassType::from($fqcn)
on something that is not a class name.