Skip to content
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

One reflector to rule them all #772

Merged
merged 1 commit into from
Oct 11, 2021
Merged

One reflector to rule them all #772

merged 1 commit into from
Oct 11, 2021

Conversation

kukulich
Copy link
Collaborator

One reflector to rule them all
one reflector to find them
one reflector to bring them all
and in the reflection bind them

@kukulich kukulich force-pushed the one branch 3 times, most recently from c8d4480 to 004a5ea Compare September 18, 2021 22:29
@kukulich
Copy link
Collaborator Author

This is PoC of big BC break :)

Motivation:

<?php

namespace Foo;

const SOME_CONSTANT = 0;

class Boo
{
     public function method($parameter = SOME_CONSTANT)
     {}
}

It's not possible with ClassReflector and StringSourceLocator/SingleFileSourceLocator to resolve default value of the parameter.
ClassReflector can create the ReflectionClass but it's not possible to get the default value because it has to be resolved by ConstantReflector and there's no ConstantReflector in the ReflectionClass. It only partly works thanks to CompileNodeToValue and global functions defined and constant.

I propose to have one Reflector that can resolve everything based on the SourceLocator.

It can also improve the types. There are a lot of places where current interface Reflector is as the parameter but we almost everytime expect that it's ClassReflector.

I've also removed the ugly circular reference in https://github.com/Roave/BetterReflection/blob/5.0.x/src/BetterReflection.php#L89


There's one class Reflector in the PR but it should be interface and there should be one implementation, eg. BetterReflector, OneReflector (as 1Password) or any other name. Or the interface can be eg. ReflectorInterface`.

@kukulich
Copy link
Collaborator Author

@Ocramius Please look at this PR (when you have time) and tell me if it will be mergeable :) If yes, please also add the opinion about the names :) I’ll then make the build green.

Copy link
Member

@asgrim asgrim left a comment

Choose a reason for hiding this comment

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

The concept here seems fine; there is a finite number of "types of symbols to reflect on" so growth is reasonably controlled, I think our concern originally (why we split into ClassReflector/FunctionReflector) is uncertainty over how many symbol types we'd want to reflect about. Now the library is much more defined, it seems reasonable.

I do also like that it solves the circular dependency issue 👍

@asgrim
Copy link
Member

asgrim commented Sep 21, 2021

There's one class Reflector in the PR but it should be interface and there should be one implementation, eg. BetterReflector, OneReflector (as 1Password) or any other name. Or the interface can be eg. ReflectorInterface`.

I'd say keep the interface named Reflector (this becomes clear where the BC break is then - because the interface is changed, rather than changing interface into a class). For the implementation name I'm not worried about, maybe BetterReflector is fine, but I'll let @Ocramius opine later on that 😁

@kukulich kukulich mentioned this pull request Sep 25, 2021
5 tasks
@Ocramius Ocramius added this to the 5.0.0 milestone Sep 26, 2021
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

@kukulich from my PoV, approach is very much valid! It's a major BC break, but easy to migrate to it

src/Reflector/Reflector.php Outdated Show resolved Hide resolved
src/Reflector/Reflector.php Outdated Show resolved Hide resolved
src/Reflector/Reflector.php Outdated Show resolved Hide resolved
@kukulich
Copy link
Collaborator Author

@Ocramius what do you think about the names of interface and the class?

@kukulich
Copy link
Collaborator Author

@Ocramius @asgrim Could you please give me a clue about the class and interface names? :) This PR blocks other my PR.

@Ocramius
Copy link
Member

@kukulich Reflector for the interface, DefaultReflector for the implementation? WDYT?

DefaultReflector could also be SourceLocatorBasedReflector or such, but IMO not that important there.

@kukulich kukulich force-pushed the one branch 2 times, most recently from c5cd412 to 7cec9a9 Compare October 11, 2021 15:25
@kukulich kukulich marked this pull request as ready for review October 11, 2021 15:58
@kukulich
Copy link
Collaborator Author

@Ocramius @asgrim Ready to review :)

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

One last change needed :D

src/Reflector/DefaultReflector.php Outdated Show resolved Hide resolved
one reflector to find them
one reflector to bring them all
and in the reflection bind them
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

🚢

@Ocramius Ocramius self-assigned this Oct 11, 2021
@Ocramius Ocramius merged commit a4c07be into Roave:5.0.x Oct 11, 2021
@Ocramius
Copy link
Member

Awesome, thanks @kukulich!

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.

3 participants