Repository navigation
Description
Works great :) I don't think there are currently any other refactorings that can benefit from this now -- but I can imagine changing the f.e. the generate accessors to operate based on a list -- would allow to generate accessors for all properties f.e.
Otherwise, I merged the other PR, and just have to wait for a green build here :)
Originally posted by @dantleech in #773 (comment)
As discuss there I already had started to work on it.
But I realized that I might be taking the problem from the wrong side.
What I did is change the handler to make the offset optional and add another optional parameter for the properties names.
If names are provided I use WorseReflection to find the property.
Then I use the position and the visibility to retrieve the offset of the property, because the start of the position if before the visibility but the offset start after it. So I add the visibility length to the start position to get the offset.
Two things bugs me here:
- It feels kind of hacky to "build" the offset from scratch
- The handler should only be a bridge between the RPC extension and the actual transformers
So I came up with another solution.
I still handled both offsets and name in the handler.
But I changed the GenerateAccessor API to provide two functions:
generateFromOffsetgenerateFromPropertyName
The generateFromPropertyName has the responsibility to use WorseReflection to find the property and create the SymbolContext used by the transformer based on the position.
It feels less hacky since a Symbol can be build form a position.
But it's a bit more changes and I sill don't have a good view of the global project.
So I was wondering what is your thoughts about that and how you would prefer to implement it.
P.S.: If the second option is choose, it might also be a good idea to be able to generate a SymbolContext from a ReflectionMember.
Something like SymbolContext::fromReflection($member) ?
Or would it be against the original design ?
Activity
dantleech commentedon Jun 10, 2019
Option 2 is in the right direction, but it could also be a code smell -- adding more than 1 responsiblity to the class.
generateFromOffsetcompletely?I think this would be applied as a "class" refactoring (as with override methods etc)?
camilledejoye commentedon Jun 10, 2019
That's up to you, I don't know why Phpactor use the offset, historical reason ?
So depending of the reason we could drop it.
Either if we delete the offset handling we can keep the handler and use it to generate from names.
That's what I did by adding it to ContextMeny/menu.json file.
But I would also have seen it as a "transformer", but mainly because I find "class" refactoring unpractical. You have to go back to your class name to access the context menu and then choose the option.
But for what I have saw the transformer handler can only use transformers and not handlers.
Plus if we drop the offset support then there is no reason to use
SymbolConext.Standard reflection can provide all the needed information.
If we want to change the accessor generator I need to ask:
https://github.com/phpactor/code-transform/blob/2899a472284f3de6918a7cf2a426d3f838ba1457/lib/Adapter/WorseReflection/Refactor/WorseGenerateAccessor.php#L101-L113
Why is there a check for path ?
As I understand it we load the source from the FQCN (which mean an access on the filesystem), then we compare the paths.
If they don't match we keep the providing source, otherwise we use the source from the reflection.
What if I didn't save my buffer ?
The paths are the same but the
worseSourceCodewould not be the one I currently have isn't it ?Wouldn't it make more sense to only work with the source provided ?
dantleech commentedon Jun 10, 2019
Just because it was written as a contextual refactoring, but I think it makes more sense to explcitly supply the name.
Yes, me too. But the transformers do not currently allow interaction and apply to ALL code in the current file (f.e. all classes), they can be used as filters on the CLI, and to be could probably all be replaced by targeted class refactorings.
I think I would start with 1 as it's pretty intuitive and improves the situation.
But of course, this is outside the scope of this PR, I might see about implementing 1) at least :)
dantleech commentedon Jun 10, 2019
I am not 100% sure -- my bad for not commenting or adding a regression test. Caching could be a factor: WR will locate files from disk -- it caches classes by FQN, it usually understands "contextual" source and that will be used in preference if provided, but only on the first call.
I think there is an issue where it is called multiple times in the same process (e.g. once by the RPC handler, then again by the refactoring). Which resulted in the source from disk being used instead of hte provided source.
But, it might not be relevant in a refactoring...
camilledejoye commentedon Jun 10, 2019
Ok, I always saw "transform" as "transform class" instead of "transform file".
Yet another PR if we want to change that but I don't think it's a big priority right now.
Especially if implementing the context menu so that when encountering an unknown symbol we check for a "bigger" symbol.
If inside a method, use the method symbol, if not but inside a class then the class symbol, etc.
Do you want me to provide a PR with what I have right now and we can then use the code to discuss it more precisely ?
Or I first remove everything about the offset ?
dantleech commentedon Jun 10, 2019
exactly, should be easy to add separately, I created #777
Yeah, let's open a PR :)
camilledejoye commentedon Jun 10, 2019
What do you mean by contextual ?
dantleech commentedon Jun 10, 2019
The source code that is passed to
reflectOffset- which may not have a path (the VIM plugin sends the source code (the source context) and the file path only if it's available).camilledejoye commentedon Oct 13, 2019
I think we can close this no ?