-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make ClassLike::from
return type assert the subclass type
#154
Conversation
ClassLike::from
return type assert the typeClassLike::from
return type assert the subclass type
src/PhpGenerator/ClassLike.php
Outdated
->fromClassReflection(new \ReflectionClass($class), $withBodies); | ||
|
||
if (!$class instanceof static) { | ||
throw new Nette\InvalidArgumentException("Object '$class' is not an instance of " . static::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message should be different so that the user understands what he is doing wrong. And please create a test case for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I'm thinking of a message that provides better guidance:
"Trait1" is not a "ClassType". Use TraitType::from() or ClassLike::from() instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test added and exception message improved. Is that all right?
Thanks! |
Since this is a BC break, for now I'll just put a warning instead of throwing exceptions, and I'll put this change in the bigger version. |
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: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)
, orEnumType::from($fqcn)
.This is a breaking change for users that call
ClassType::from($fqcn)
on something that is not a class name.