zendframework / zend-code

BSD 3-Clause "New" or "Revised" License
1.68k stars 79 forks source link

Feature: Self as PHP Internal Type #173

Closed homersimpsons closed 4 years ago

homersimpsons commented 4 years ago

Add self as a PHP Internal Type (#161)

Why is the new feature needed? What purpose does it serve?

As per https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration self is a PHP valid type.

Currently adding self will add a \ (backslash) prefix leading to an undefined \self class.

How will users use the new feature?

Users will now be able to declare self as a Method return Type.

$methodGenerator->setReturnType('self');

Add tests for the new feature.

Test written to test this as a MethodGenerator return type. Test written to test this in TypeGenerator tests.

Ocramius commented 4 years ago

Hmm, need to evaluate this: in practice, self is an alias to the current class, but it is problematic to use when generating inheritances.

Specifically, self on a generated inheritance tree in parameters will lead to a crash (by design), which is why I didn't include self nor its less known sibling static in ReflectionType on purpose.

Since zendframework/zend-code is built to generate code, and that code will never adhere to personal quality standards expected by humans, I'd rather say that self should not be used in this component, and instead the alias should be resolved to the target symbol.

michalbundyra commented 4 years ago

I agree here with @Ocramius, this is not going to be addressed.

We can use some Coding Standard rules to replace current class name with self.