Skip to content

[ErrorHandler] resolve class constant types when patching return types #58536

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

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Oct 10, 2024

Q A
Branch? 7.2
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

account for types like the ones added in #58493

@xabbuh xabbuh requested a review from yceruto as a code owner October 10, 2024 19:47
@carsonbot carsonbot added this to the 7.2 milestone Oct 10, 2024
@carsonbot carsonbot changed the title [ErrorHandler] resolve class constant types when patching return types [ErrorHandler] resolve class constant types when patching return types Oct 10, 2024
@stof
Copy link
Member

stof commented Oct 11, 2024

Can you add a test covering this (probably in the existing tests DebugClassLoaderTest::testReturnType) ?

default => $definingClass,
};

if (!\defined($definingClass.'::'.$definingClass)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will happen here for things like Cookie::SAMESITE_* ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ignore them for now. The changes from this PR require that you use the full constant name. We could ship support for wildcards in a follow-up PR (I would like to ship this one as soon as possible to make the job green again and supporting wildcards requires a bit more work).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with ignoring them (we were already ignoring those cases before, see the comment below). But I'm wondering whether this will be ignored cleaning here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, returning here (which means not reaching the line where the $returnTypes property is written for the current method means that no patching happens afterwards. The same is done for example further down for the resource type.


$constant = new \ReflectionClassConstant($definingClass, $constantName);

if (method_exists(\ReflectionClassConstant::class, 'getType') && $constantType = $constant->getType()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace the method_exists check with a PHP_VERSION_ID check ? This has 2 benefits:

  • it will make it clearer when we can simplify that check thanks to our min requirement
  • it will be resolved at compile-time

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

$constant = new \ReflectionClassConstant($definingClass, $constantName);

if (method_exists(\ReflectionClassConstant::class, 'getType') && $constantType = $constant->getType()) {
$n = $constantType->getName();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the constant type guaranteed to be a ReflectionNamedType in PHP ? https://github.com/php/php-src/blob/9345582471d5dfa8b783b1e666d8b76620f542f9/ext/reflection/php_reflection.stub.php#L626C32-L626C47 seems to answer "no"

Copy link
Member Author

@xabbuh xabbuh Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the stub is not correct here. I added a test covering this line and $constantType is indeed an instance of ReflectionNamedType.

I stand corrected here. A constant can indeed have union or intersection types (see the examples in the RFC: https://wiki.php.net/rfc/typed_class_constants).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, if the type you use is a named type, it will. But do constant types allows using union or intersection types ? Those are not named types.

Copy link
Member Author

@xabbuh xabbuh Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they can be union or intersection types (see my edit above). I've opted to ignore them for now in this PR. We can consider to add support for them in the future (similar to the support for wildcards). But I do not see an urgent need for that right now (for wildcards that's a bit different as we make use of them in Symfony).

@xabbuh xabbuh force-pushed the pr-58493 branch 2 times, most recently from 3c91c04 to 60553b9 Compare October 11, 2024 21:21
@xabbuh
Copy link
Member Author

xabbuh commented Oct 11, 2024

@stof test added

@xabbuh xabbuh merged commit c3f10c3 into symfony:7.2 Oct 14, 2024
8 of 10 checks passed
@xabbuh xabbuh deleted the pr-58493 branch October 14, 2024 09:01
@Zausenec
Copy link

With 01eb32c danog\MadelineProto not working

Attempted to load class "AbstractAPI" from namespace "danog\MadelineProto".
Did you forget a "use" statement for "danog\MadelineProto\Namespace\AbstractAPI"?

Both classes danog\MadelineProto\AbstractAPI and danog\MadelineProto\Namespace\AbstractAPI are exists
Without this commit all works as expected

@xabbuh
Copy link
Member Author

xabbuh commented Nov 30, 2024

@Zausenec please open a new issue and provide a small example application that allows to reproduce it.

@Zausenec
Copy link

Zausenec commented Nov 30, 2024

@Zausenec please open a new issue and provide a small example application that allows to reproduce it.

#59051

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants