-
Notifications
You must be signed in to change notification settings - Fork 9
Added support for non-public enumeration constants #26
Conversation
Codecov Report
@@ Coverage Diff @@
## master #26 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 6 6
Lines 175 175
=====================================
Hits 175 175
Continue to review full report at Codecov.
|
d766df9
to
e44bec1
Compare
Previously, non-public constants were being included in enumerations. However, non-public constants cannot be accessed externally so they should not be part of the enumeration. This patch adds only public constants, whether declared explicitly or implicitly, to the enumeration.
Thanks for the PR! Looks good. I'm wondering though, given that The implementation could probably be simplified to the point that it doesn't even need a new method, just use |
91149f6
to
116bad9
Compare
116bad9
to
e7ece9c
Compare
|
||
before_install: | ||
- phpenv config-rm xdebug.ini || true | ||
- composer config --global github-oauth.github.com $GITHUB_TOKEN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For whatever reason, this configuration setting is not available in the PHP 7.3 build. It should be removed anyway, however, since the invalid token was causing Composer to choke every time it tried to check out a package, stalling the build for longer than necessary.
Not sure what this was doing here in the first place, considering one does not need a token to download public packages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It used to be the case that Composer would hit GitHub API rate limits when running under Travis CI because of the fact that GitHub counted every request coming from every composer install
under Travis CI against the same rate limit quota. The workaround was to supply a personal GitHub token.
In 2016 a change was made to GitHub's infrastructure to remove the rate limit, so it's no longer necessary: composer/composer#4884 (comment)
I think you can probably get rid of the secure environment variable in .travis.yml
now too (it contains GITHUB_TOKEN
).
As a bonus, I added support for PHP 7.3 since that is due for release soon. |
- php: 7.0 | ||
- php: 7.1 | ||
- php: 7.2 | ||
- php: 7.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
|
||
before_install: | ||
- phpenv config-rm xdebug.ini || true | ||
- composer config --global github-oauth.github.com $GITHUB_TOKEN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It used to be the case that Composer would hit GitHub API rate limits when running under Travis CI because of the fact that GitHub counted every request coming from every composer install
under Travis CI against the same rate limit quota. The workaround was to supply a personal GitHub token.
In 2016 a change was made to GitHub's infrastructure to remove the rate limit, so it's no longer necessary: composer/composer#4884 (comment)
I think you can probably get rid of the secure environment variable in .travis.yml
now too (it contains GITHUB_TOKEN
).
}, | ||
"config": { | ||
"platform": { | ||
"php": "5.3.99" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to update this instead of deleting it. The new value should be 7.1.99
.
Having it here ensures that even if I run composer update
under PHP 7.3, the lock file will only ever store dependencies that are at least compatible with PHP 7.1, which is important for running the 7.1 tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Libraries shouldn't have a lock file to begin with. I didn't remove the lock file because I'm trying to keep this PR as focused as possible, but if you insist, I will put this back in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd appreciate it. I'm aware of the arguments against committing lock files, but it's not something I want to change in this PR, if at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, it's done.
src/AbstractEnumeration.php
Outdated
{ | ||
$reflector = new ReflectionClass(get_called_class()); | ||
|
||
foreach ($reflector->getConstants() as $key => $value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested this, but I think we can make this better by using getReflectionConstants
directly:
foreach ($reflector->getConstants() as $key => $value) { | |
foreach ($reflector->getReflectionConstants() as $constant) { |
src/AbstractEnumeration.php
Outdated
|
||
foreach ($reflector->getConstants() as $key => $value) { | ||
new static($key, $value); | ||
if ($reflector->getReflectionConstant($key)->isPublic()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($reflector->getReflectionConstant($key)->isPublic()) { | |
if ($constant->isPublic()) { |
src/AbstractEnumeration.php
Outdated
foreach ($reflector->getConstants() as $key => $value) { | ||
new static($key, $value); | ||
if ($reflector->getReflectionConstant($key)->isPublic()) { | ||
new static($key, $value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new static($key, $value); | |
new static($constant->getName(), $constant->getValue()); |
Thanks for your work on this PR. 6.0.0 should be available shortly with this feature. |
The thought occurs that you might not need to increment the major version since it should be compatible with the 5.x series. |
I avoid doing that because a simple version constraint like |
I was using private consts for my enums to prevent people from accidently using them directly instead of by the method. This seems to have stopped me being able to do that. |
@joel-pi You can accomplish the same effect by using use Eloquent\Enumeration\AbstractValueMultiton;
final class ExampleValueMultiton extends AbstractValueMultiton
{
protected static function initializeMembers()
{
new static('FOO', 1);
new static('BAR', 2);
}
} Or if you don't need values, use use Eloquent\Enumeration\AbstractMultiton;
final class ExampleMultiton extends AbstractMultiton
{
protected static function initializeMembers()
{
new static('FOO');
new static('BAR');
}
} |
Brilliant - thanks! |
Previously, non-public constants were being included in enumerations.
However, non-public constants cannot be accessed externally so they
should not be part of the enumeration. This patch adds only public
constants, whether declared explicitly or implicitly, to the
enumeration.