Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Conversation

Bilge
Copy link
Contributor

@Bilge Bilge commented Nov 17, 2018

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.

@codecov
Copy link

codecov bot commented Nov 17, 2018

Codecov Report

Merging #26 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #26   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           6      6           
  Lines         175    175           
=====================================
  Hits          175    175
Impacted Files Coverage Δ
src/AbstractEnumeration.php 100% <100%> (ø) ⬆️
src/AbstractMultiton.php 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9a5bb77...bec2991. Read the comment docs.

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.
@ezzatron
Copy link
Contributor

Thanks for the PR! Looks good.

I'm wondering though, given that getReflectionConstants was introduced in 7.1, and 7.0 security support ends in 12 days from now, I think it might be a better approach to just drop 7.0 support, and implement this feature without needing to check whether the reflection API is available.

The implementation could probably be simplified to the point that it doesn't even need a new method, just use getReflectionConstants instead of getConstants, and check isPublic inside the main loop before creating each instance.

@Bilge Bilge force-pushed the non-public-constants branch from 91149f6 to 116bad9 Compare November 20, 2018 10:19
@Bilge Bilge force-pushed the non-public-constants branch from 116bad9 to e7ece9c Compare November 20, 2018 10:40

before_install:
- phpenv config-rm xdebug.ini || true
- composer config --global github-oauth.github.com $GITHUB_TOKEN
Copy link
Contributor Author

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.

Copy link
Contributor

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).

@Bilge
Copy link
Contributor Author

Bilge commented Nov 20, 2018

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
Copy link
Contributor

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
Copy link
Contributor

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, it's done.

{
$reflector = new ReflectionClass(get_called_class());

foreach ($reflector->getConstants() as $key => $value) {
Copy link
Contributor

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:

Suggested change
foreach ($reflector->getConstants() as $key => $value) {
foreach ($reflector->getReflectionConstants() as $constant) {


foreach ($reflector->getConstants() as $key => $value) {
new static($key, $value);
if ($reflector->getReflectionConstant($key)->isPublic()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ($reflector->getReflectionConstant($key)->isPublic()) {
if ($constant->isPublic()) {

foreach ($reflector->getConstants() as $key => $value) {
new static($key, $value);
if ($reflector->getReflectionConstant($key)->isPublic()) {
new static($key, $value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
new static($key, $value);
new static($constant->getName(), $constant->getValue());

@ezzatron ezzatron merged commit 82130d8 into eloquent:master Nov 22, 2018
@ezzatron ezzatron added this to the Next release milestone Nov 22, 2018
@ezzatron
Copy link
Contributor

Thanks for your work on this PR. 6.0.0 should be available shortly with this feature.

@Bilge
Copy link
Contributor Author

Bilge commented Nov 22, 2018

The thought occurs that you might not need to increment the major version since it should be compatible with the 5.x series.

@ezzatron
Copy link
Contributor

I avoid doing that because a simple version constraint like ^5 can end up resolving to different minor versions depending on the PHP version used to do the resolution. It's bitten me before, and it can be quite confusing.

@joel-pi
Copy link

joel-pi commented Dec 12, 2019

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.

@ezzatron
Copy link
Contributor

@joel-pi You can accomplish the same effect by using AbstractValueMultiton instead of AbstractEnumeration:

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 AbstractMultiton:

use Eloquent\Enumeration\AbstractMultiton;

final class ExampleMultiton extends AbstractMultiton
{
    protected static function initializeMembers()
    {
        new static('FOO');
        new static('BAR');
    }
}

@joel-pi
Copy link

joel-pi commented Dec 12, 2019

Brilliant - thanks!

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.

3 participants