php-generator icon indicating copy to clipboard operation
php-generator copied to clipboard

Make `ClassLike::from` return type assert the subclass type

Open GromNaN opened this issue 5 months ago • 0 comments

  • 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.

GromNaN avatar Feb 26 '24 22:02 GromNaN