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

v2 refactoring #18

Merged
merged 29 commits into from
Apr 1, 2019
Merged

v2 refactoring #18

merged 29 commits into from
Apr 1, 2019

Conversation

Gummibeer
Copy link
Collaborator

@Gummibeer Gummibeer commented Mar 19, 2019

solves: #10

I think the biggest change is the drop of $map and therefor the custom value via an array map. The Values are also forced to lower-case.
To customize value & index override the getValue() or getIndex() method.

@brendt atm we only support the default index to retrieve an instance and also listed in getIndices(). Do you have an idea how to solve this? The moment I try to construct an instance and call getIndex() in resolveFromStaticMethods() I end up in an endless round because the construct calls isValue() & isIndex() which will call resolve().

The only way I could think about would be to call all the methods and there getIndex() after I build the map with default indices. Or do we drop make() with index support in total?

@Gummibeer Gummibeer self-assigned this Mar 19, 2019
@Gummibeer Gummibeer requested a review from brendt March 19, 2019 09:26
Copy link
Collaborator

@brendt brendt left a comment

Choose a reason for hiding this comment

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

We should also add an UPGRADING entry, explaining all the breaking changes.

src/Enumerable.php Outdated Show resolved Hide resolved
src/Enum.php Show resolved Hide resolved
@brendt
Copy link
Collaborator

brendt commented Mar 19, 2019

atm we only support the default index to retrieve an instance and also listed in getIndices(). Do you have an idea how to solve this? The moment I try to construct an instance and call getIndex() in resolveFromStaticMethods() I end up in an endless round because the construct calls isValue() & isIndex() which will call resolve().

Yeah, I know, this is the reason why the README states this when using anonymous value classes:

Note that one drawback of this approach is that the value of the enum is always the name of the static function, there's no way of mapping it.

Continuing with what you said:

The only way I could think about would be to call all the methods and there getIndex() after I build the map with default indices. Or do we drop make() with index support in total?

I believe the same problem happens if you're making a enum from a value that's overridden. We should look into a solution for the problem, as I think it's required to be able to construct an enum from its index.

Can we prevent the loop by building some sort of value/index cache first, before validating whether the enum is valid?

@Gummibeer
Copy link
Collaborator Author

@brendt I can try to find a way to initialize the mapping, call all custom value/index getter, override the initialized mapping and mark the class as initialized. An idea would be to have a boolean stating if the enum is fully resolved or not and if it isn't we won't call isValidIndex/Value().

@brendt
Copy link
Collaborator

brendt commented Mar 19, 2019

@Gummibeer sounds like a good idea.

@Gummibeer
Copy link
Collaborator Author

The new unittest suite should demonstrate better what is possible and what's not. I've dropped the RecursiveEnum now because I haven't found a real scenario why I would need to create an instance of the current enum in a non-static method.
If this is a real-world scenario and required I will put the solution in __call() and corresponding unittest back in.
Except the two duplication enums I've also choose some more understandable and real enums instead of foo().

This is more something like a "let the code represent what i was thought for" than a real rewrite/refactoring. So the idea is the same and now everything should be consistent. So you can instantiate make() an instance by the name, value or index. The real values and indices from the getters are respected also for instantiation.

@Gummibeer
Copy link
Collaborator Author

v2.0.0-dev.1

throw new InvalidIndexException($value, static::class);
}

$name = array_combine(static::getIndices(), array_keys(static::resolve()))[$value];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any change we can cache these array_combine calls so that this hasn't be done very time an enum is constructed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, will try to add some more poor-man caches to keep these values.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hey, adding the caches has no impact on performance. I've added a speed test and the uncached make() call needs the same time like the cached one.
And adding two static variables and two methods for a single call without any performance impact isn't a good trade!?

Spatie\Enum\OldTests\WeekDayEnumSpeedTest::can_make_faster_from_cache_than_build
Failed asserting that 1.4 is less than 1.4.

Spatie\Enum\OldTests\WeekDayEnumSpeedTest::can_load_stable_from_cache
Failed asserting that 0.4 is less than 0.38000000000000006.

Spatie\Enum\OldTests\WeekDayEnumSpeedTest::can_make_faster_from_cache_than_build
Failed asserting that 0.6 is less than 0.6.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're right, let's keep it as is

src/Enum.php Show resolved Hide resolved
src/Enum.php Show resolved Hide resolved
@brendt
Copy link
Collaborator

brendt commented Mar 28, 2019

@Gummibeer great PR! Could you maybe also elaborate how the recursive loop problem has been solved?

@Gummibeer
Copy link
Collaborator Author

@brendt I've dropped the recursive support because I don't see a usage/reason!?
If you want it I can add the fix again - pass the call from __call() to __callStatic().

@brendt
Copy link
Collaborator

brendt commented Mar 29, 2019

Right, I forget, sorry! Dropping it is entirely fine!

As far as I'm concerned, all looks good! I'd say: feel free to merge this in and tag 2.0!

@brendt brendt marked this pull request as ready for review March 29, 2019 07:23
@Gummibeer
Copy link
Collaborator Author

@brendt I've updated the changelog and readme and also add it to the http://docs.spatie.be during https://github.com/spatie/docs.spatie.be/pull/398

could you give a final approve? and update the repo description link if the docs PR is merged?

@brendt
Copy link
Collaborator

brendt commented Apr 1, 2019

Tom, looks really good! I've added one remark about the performance thing, it can be kept the way it is. I've asked the team to take a look at your docs PR. Meanwhile you're free to merge this and tag v2!

@Gummibeer Gummibeer merged commit d888c18 into master Apr 1, 2019
@Gummibeer Gummibeer deleted the ft-v2 branch April 1, 2019 08:39
@Gummibeer
Copy link
Collaborator Author

@brendt
Copy link
Collaborator

brendt commented Apr 1, 2019

Congratulations!

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

Successfully merging this pull request may close these issues.

2 participants