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

Trait support - #9

Merged
dantleech merged 22 commits into
phpactor:masterfrom
dshoreman:trait-support
Feb 3, 2019
Merged

dantleech merged 22 commits into
phpactor:masterfrom
dshoreman:trait-support

Conversation

@dshoreman

@dshoreman dshoreman commented Jan 29, 2019 •

Copy link
Copy Markdown
Contributor

Adds underlying Trait prototypes, builders etc required for phpactor/phpactor#726.

Todo

  • Make it work
  • Create new ClassLikeUpdater abstract class
  • Move common TraitUpdater and ClassUpdater code to ClassLikeUpdater
  • Refactor out switch in updateProperties method
  • Commit patch to phpactor/code-transform
  • Submit PR for phpactor/code-transform patch
  • Tests for Adapter\WorseReflection\WorseBuilderFactoryTest
  • Tests for Adapter\UpdaterTestCase

Notes

  • Some places had type hints and return types loosened from ClassFoo to ClassLikeFoo in order to receive Trait-related objects. Might need to rework this to avoid accepting Interface-related things where we don't want them.
  • ClassBuilder and TraitBuilder, along with ClassUpdater and TraitUpdater share many similarities. Could probably refactor these too to avoid too much duplication potentially causing issues down the line.

@dantleech

dantleech commented Jan 29, 2019 •

Copy link
Copy Markdown
Contributor

Looks good so far :)

Some places had type hints and return types loosened from ClassFoo to ClassLikeFoo in order to receive Trait-related objects. Might need to rework this to avoid accepting Interface-related things where we don't want them.

This is probably acceptable, in cases where we know we return a more specific type we can add a virtual method declaration (e.g. @method \Acme\ActualImplementation foobar() in the class docblock

ClassBuilder and TraitBuilder, along with ClassUpdater and TraitUpdater share many similarities. Could probably refactor these too to avoid too much duplication potentially causing issues down the line.

For sure, although the builders don't worry as much as the updaters. The updating code can be broadly refactored, as currently it is very loose and clumsy, and I'm sure some mistakes will be duplicated in the trait updater. Perhaps a first step woul be to create an abstract ClassLikeUpdater and push some methods up?

dshoreman added a commit to dshoreman/code-transform that referenced this pull request Jan 30, 2019
Fixes phpactor/phpactor#726, but relies on phpactor/code-builder#9 to
get the underlying trait() method added with all its supporting code.
@dshoreman

dshoreman commented Jan 30, 2019 •

Copy link
Copy Markdown
Contributor Author

Looks good so far :)

Thanks! That's a welcome and much need sanity check so I know I'm not going down a completely wrong path

Perhaps a first step woul be to create an abstract ClassLikeUpdater and push some methods up?

That's pretty much exactly what I was thinking, but I didn't want to go ahead if you preferred a simple copy approach. I'll work on this later today and see what I can come up with

Begin refactoring common ClassUpdater and TraitUpdater methods into a
single unified ClassLikeUpdater.

Move resolvePropertyName() to abstract updater

Move second foreach loop. First is causing havoc
Been trying to move just the foreach loop, but everything results in
failed tests. Moving the entire method is finally passing with some
slightly looser type hinting which can be cleaned up in the next commit.
Only 2 lines differ between trait and class so this *should* be fine.

Handle Class and Trait constants together

Now that things pass, we can lump everything back into one method and
use a switch to diffrentiate between Class and Trait declarations.

The other option here is `if $classNode instanceof TraitDeclaration
elseif ... else throw exception`. Either would work equally well.
Much of this is the same as updateConstants, but let's take it one step
at a time. All tests are still passing, which is a miracle in itself.

Remove unnecessary use statements

Fix PEBKAC
@dshoreman

dshoreman commented Jan 31, 2019 •

Copy link
Copy Markdown
Contributor Author

First stage of the ClassLikeUpdater is now pushed up, but updateProperties() and updateConstants() share a lot of code so it's not quite finished yet.

Attempts at moving the first foreach loop without the rest of the method proved troublesome - the exact same code that passed tests in the "real" classes would fail when cut/pasted into the abstract. I've worked around it for now by moving the entire method and adding a switch where the code differed. It's not the most beautiful code in the world, but it's something I can work on before this gets merged.

To avoid any issues, I first want to add some tests to Adapter\UpdaterTestCase. Once that's done, I'll come back and do some more refactoring within ClassLikeUpdater.

@dantleech

Copy link
Copy Markdown
Contributor

👍 looks good so far :)

These tests could've been done by modifying the `provideProperties()`
method and altering the templates based on the test it's providing for
which happens to be the first and only parameter* to providers methods

Such a "feature" would also require changing NOWDOC to HEREDOC though,
which in turn would mean escaping all the `$`s and making it unreadable.

* The parameter is entirely undocumented - I discovered it by calling
`func_get_args()` in the provider. It probably can't be relied on.
Moves `updateConstants` back into the concrete `ClassUpdater`, and
removes the call to it from the `TraitUpdater`.
Rather than having an ugly switch, this lets the concrete classes define
the members and memberDeclarations themselves. Almost good to go now.

Would probably be better with an `openBrace` or `startPoint` method
returning `$node->classMembers->openBrace`, instead of the generic
`members` but It's only used once and I couldn't decide what to call it.
dshoreman added a commit to dshoreman/code-transform that referenced this pull request Feb 3, 2019
Fixes phpactor/phpactor#726, but relies on phpactor/code-builder#9 to
get the underlying trait() method added with all its supporting code.
@dshoreman dshoreman changed the title [WIP] Trait support Trait support Feb 3, 2019
@dshoreman

dshoreman commented Feb 3, 2019 •

Copy link
Copy Markdown
Contributor Author

@dantleech All done! I've submitted phpactor/code-transform#23 which connects the dots if you want to try it locally. Otherwise, we should pretty much be good to go.

I've also uncommented the TODO: Traits not currently supported block in Adapter\WorseReflection\WorseBuilderFactory (7c644fb) to get some new builder tests passing. This probably inherently enables support for a lot of other trait-related bits, but I've only tested the original issue manually in Vim.

Let me know if there's anything I've missed!

@dantleech

Copy link
Copy Markdown
Contributor

Looks perfect :) Just left a comment about potentially duplicated tests

@dshoreman

dshoreman commented Feb 3, 2019 •

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback :)

After replying to that comment, I took another look at the tests. I've gone ahead and removed most of the duplicates, leaving behind:

  • testSimpleTrait
  • testSimpleTraitWithNamespace
  • testTraitWithProperty
  • testTraitWithMethod

...which should hopefully keep all bases covered.

I've also updated phpactor/code-transform#23 to get rid of the variable method. Let me know if you would rather an if/else instead of the shorthand and I'll update it. Seems Travis is unhappy with something... Looking into it now ;)
Never mind. Of course trait() doesn't exist, that's what this one adds! 🤦‍♂️

@dantleech
dantleech merged commit 63c3335 into phpactor:master Feb 3, 2019
@dantleech

Copy link
Copy Markdown
Contributor

Awesome! thanks for all your work on this. This library is far from being perfect, but I think this is a nice step in the right direction.

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