Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

GenerateAccessor handles both offset and names#24

Merged
dantleech merged 11 commits intophpactor:masterfrom
camilledejoye:generate-accessors-rework
Jun 20, 2019
Merged

GenerateAccessor handles both offset and names#24
dantleech merged 11 commits intophpactor:masterfrom
camilledejoye:generate-accessors-rework

Conversation

@camilledejoye
Copy link
Copy Markdown
Contributor

Go along with phpactor/phpactor#778

@dantleech
Copy link
Copy Markdown
Contributor

dantleech commented Jun 10, 2019 via email

@dantleech
Copy link
Copy Markdown
Contributor

dantleech commented Jun 10, 2019 via email

@camilledejoye
Copy link
Copy Markdown
Contributor Author

camilledejoye commented Jun 10, 2019

That's probably fine, we're testing the transformation. We can validate the method name in other tests.

I think we didn't understand each over because I forgot to update the test so it uses the property name when calling the generate method.
For me the goal is not to test the property name but just to ensure that not matter how we name the property, the tests won't fail.

I think this could be an exception to that law. The reflection API is a tree structure and is self-contained (and immutable).

We can keep it self-contained and immutable, for instance with ReflectionClass:

    public function namespace(): string
    {
        return $this->name()->namespace();
    }

But I think this way it makes more sense.
It will allow to shorten the code and to have something logical like a property which provides the name of it's own class.

Still it's an early optimization, it could wait another PR.
Especially since there is some overlapping between some classes like ReflectionInterface and ReflectionClass in WR.
So we could handle this and in the same time provide a new abstract class or trait to centralize some parts of the code.

@camilledejoye
Copy link
Copy Markdown
Contributor Author

camilledejoye commented Jun 10, 2019

Another idea while I think about it.
It might be interesting to refactor the formatName() method into it's own class, like GetterFormatter or something.
I would remove some dependencies from the WorseGenerateAccessor and allow for a more configurable option to the user.

But once again, maybe a too early optimization... I kind of often do that ><

EDIT:
One more thing, to deal with a property name we need the class now.
So right now we got an exception if the file contains more that one class.
It might be more in the spirit of Phpactor to also provide the class name of the property to be able to handle multiple class in the same file ?

@dantleech
Copy link
Copy Markdown
Contributor

dantleech commented Jun 10, 2019

I think we didn't understand each over because I forgot to update the test so it uses the property name when calling the generate method.

Replied to comment in the code.

We can keep it self-contained and immutable, for instance with ReflectionClass:

I guess my question is: what value do we gain?

Positive:

  • it's shorter, and easier to mock.

Negative:

  • There are no 2 ways to do the same thing.
  • All implementations need to add another method.

Neutral:

  • We shouldn't mock WorseReflection anyway -- it's too complicated by nature.

It also depends on how you look at the role of the name / namespace. If you consider the name to be an FQN, then the namespace should be in the name object, if you think that the class is in a namespace AND it has a name, then it would make sense to have a separate namespace method.

It may well be the second way is more correct (possibly) but the first way is how it is currently modelled, so we should keep it like that, at least for now.
Note that BetterReflection returns the FQN from name -- as a string. Anyway, another discussion :)

@camilledejoye
Copy link
Copy Markdown
Contributor Author

I agree with you.
Plus there is no rush, so I propose to let the things are they are for now and it will always be time to change it later is the need is stronger.

@dantleech
Copy link
Copy Markdown
Contributor

It might be interesting to refactor the formatName() method into it's own class, like GetterFormatter or something.

Yeah, let's discuss in another issue -- there have also been requests for generating "setters" also (:scream: , but I guess can be valid for things like DTOs). But in general, all for extracting classes :+1:

It might be more in the spirit of Phpactor to also provide the class name of the property to be able to handle multiple class in the same file ?

What has been done in this situation before is to pass the cursor offset and let Phpactor determine the name of the class. I'm not sure where ... I'll try and look.

@camilledejoye
Copy link
Copy Markdown
Contributor Author

camilledejoye commented Jun 10, 2019

Yeah, let's discuss in another issue -- there have also been requests for generating "setters" also (scream , but I guess can be valid for things like DTOs). But in general, all for extracting classes +1

Not a big fan of setters neither, even for DTOs. Most of the time I think we are better of with immutable objects in those cases so no need for setters.
I will let this one to some one else :)

Is there a todolist or idea box somewhere to register this kind of things for rainy days with nothing better to do ? :D

What has been done in this situation before is to pass the cursor offset and let Phpactor determine the name of the class

That's a possibility, but isn't it asking to Phpactor to do the job of the underlying plugin used by the editor ?
I just kept thinking about Phpactor as a binary, as a human I would like to tell Phpactor: "Generate the accesor for property in class ClassName in the stream SourceCode"

@dantleech
Copy link
Copy Markdown
Contributor

Is there a todolist or idea box somewhere to register this kind of things for rainy days with nothing better to do ? :D

Just the issue list :)

Camille Dejoye added 3 commits June 10, 2019 17:15
Should had been in the previous commit but forgot.
In order to bo able to test the retriving of the class from the cursor
position I couldn't use the cursor to find out the name of the property.
@camilledejoye
Copy link
Copy Markdown
Contributor Author

What has been done in this situation before is to pass the cursor offset and let Phpactor determine the name of the class. I'm not sure where ... I'll try and look.

I tried to do something like that.
If that's ok to you I think you are good to go.
Like last time I will update the corresponding PR for Phpactor with a composer update once this one will be merged.

Copy link
Copy Markdown
Contributor

@dantleech dantleech left a comment

Choose a reason for hiding this comment

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

Thanks again for your effort on this! Some CS comments relating to docblocks, but in general it looks good :)

],
'multiple-classes' => [
'generateAccessor6.test',
$propertyName,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why do we add $propertyName when it is always the same?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would have slightly preferred to keep "constants" as a value in the test rather than repeating here (we don't vary in this test). But not important :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I always try to keep tests configurable, not saying it's a good thing.
It makes my life terrible some time ! :D
But that's why, I just don't like the idea of coupling even... Early optimization, sill got some work to do on that ><

@dantleech dantleech merged commit 7ad69ff into phpactor:master Jun 20, 2019
@dantleech
Copy link
Copy Markdown
Contributor

Thanks @elythyr

@camilledejoye
Copy link
Copy Markdown
Contributor Author

My pleasure, I will update the composer.lock for the PR on phpactor

@camilledejoye camilledejoye deleted the generate-accessors-rework branch June 21, 2019 19:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants