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

[Utility] [CS] Use simplified symfony version comparison and minor cs fixes #939

Conversation

robfrawley
Copy link
Collaborator

@robfrawley robfrawley commented May 21, 2017

Q A
Branch? 1.0
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

This changes the internal behavior of SymfonyFramework::kernelVersionCompare() by using the Kernel::VERSION_ID constant instead of the separated version numbers (major, minor, patch). The resulting API behavior is identical. To see only these changes, reference commit 39d820.

The second commit contains automated CS fixes from a PHP CS Fixer run.

Also of interest, I added the internal docblock to the SymfonyFramework class, which I suggest we more regularly adopt for code we don't intend to be used by consuming projects.

@robfrawley robfrawley added the State: Reviewing This item is being reviewed to determine if it should be accepted. label May 21, 2017
@robfrawley robfrawley added this to the 1.8.1 milestone May 21, 2017
}

return version_compare($kernel, $vernum, $operator);
return version_compare(Kernel::VERSION_ID, sprintf("%d%'.02d%'.02d", $major, $minor ?: 0, $patch ?: 0), $operator);
Copy link
Contributor

@rpkamp rpkamp May 21, 2017

Choose a reason for hiding this comment

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

Why are you padding the minor and patch components? The previous code didn't do this and I don't see a reason why it would be needed.

Nice use of ?: by the way 💯

Copy link
Collaborator Author

@robfrawley robfrawley May 21, 2017

Choose a reason for hiding this comment

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

Regarding the padding, this isn't using the same comparison value; hence the change. More specifically, the padding is required because we're now comparing to the Kernel::VERSION_ID constant, which would be assigned the value int(20801) for version 2.8.1. This requires we change the input parameters of, say int(2), int(3), null(), into 20300.

Also, I love ?: 👍

Copy link
Collaborator Author

@robfrawley robfrawley May 21, 2017

Choose a reason for hiding this comment

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

By the way, I found that the :? is actually not required, as null is cast to 0 automatically, but I found this made the usage more clear, and I don't like relying on PHP's internal type casting behavior.

Copy link
Contributor

@rpkamp rpkamp May 22, 2017

Choose a reason for hiding this comment

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

Ah, my mistake, I thought you were comparing against VERSION, not VERSION_ID.
That being said I can't find any documentation on the VERSION constant, so I'm not sure how it's constructed but from what I've seen thusfar numeric comparison should suffice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As for simple numeric comparison, that isn't possible due to the nature of this method signature: it allows passing in the comparison operator, hence the usage of version_compare.

The VERSION constant isn't a bad choice, but it is sometimes followed by a string value like -dev and -beta and while I know version_compare can deal with those, it just doesn't seem as clear to me. From my 100% unscientific polling of other projects, usage of VERSION_ID seems to be the most often used way to version detect, which was another reason I'd chosen it originally.

This change actually originated from a bug in symfony/symfony#master where the kernel's VERSION_MAJOR was accidentally set to 0 causing our tests to fail: I settled on VERION_ID after looking at what others used.

Do you feel that using VERSION instead of VERSION_ID would add any value?

Copy link
Contributor

Choose a reason for hiding this comment

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

Using VERSION instead of VERSION_ID is more in line with what the code previously did, but other projects use VERSION_ID and that works for them so there is some empirical evidence that it works I'd say go for it.

Of course, feature detection (class_exists, method_exists et al) is always better than kernel version comparison, but that's beside the point of this PR.

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, feature detection is of course preferable. Thankfully, this class is no longer required (for either version or feature detection) on the 2.x branch. You may also feel better (as I do) that version detection is only used once or twice in the production codebase; the majority of calls occur in the test suite.

:-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Lol, that does make me feel better 😃

@robfrawley robfrawley merged commit f65a530 into liip:1.0 Jul 8, 2017
@robfrawley robfrawley changed the title Use simplified Symfony version comparison operation and CS fixes [Utility] [CS] Use simplified symfony version comparison and minor cs fixes Jul 13, 2017
@robfrawley robfrawley mentioned this pull request Aug 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
State: Reviewing This item is being reviewed to determine if it should be accepted.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants