-
Notifications
You must be signed in to change notification settings - Fork 93
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
fix: Improve PHP syntax #228
Conversation
ed7047a
to
e5427a5
Compare
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.
===
over==
is a welcome change, so are the docblocks and spread syntax.- Please avoid Yoda conditions, i.e., prefer
$params === null
overnull === $params
. - I would prefer if we continued using
empty($array)
instead of$array === []
. In many languages this would perform an identity compare and would always returnfalse
, so having this syntax may be confusing DX. Also I did some microbenchmarking comparing the two options andempty($array)
is consistently 2.34x faster.
Oops sorry, it's an habit. Would you agree if i add a phpcs rule to enforce "no yoda condition" ?
Then i'll change back :)
In PHP any empty array
That in fact is really surprising to me... i made two small benchmark here so you can see TL;DR; they are absolutely identical Mulitple arrays (empty, one element, multipes)
Both produce the same exact code after the same number of ops
With a single empty array
There again you can see the compiled code is the same (same number of ops)
So neither of them has a technical / performance one here :) But it's your lib, so it's your code style and i'll happily use the CS you want 😃 |
f4fac64
to
53d067a
Compare
Apply your feedback and rebased :) |
Absolutely, if you know how 😅 I found phpcs weird to work with and the available rules kind of limited. Feel free to suggest improvements to our rules as you see fit. (Preferably in a new PR) |
Thank you for benchmarking as well, the site you linked is certainly better suited than what I used when creating mine, and the VLD tab is nice. However, I think your benchmarks are flawed since they execute only 4 comparisons, leading to no observable time difference since the overhead of running the overall PHP script is too large. I adapted your benchmarks to run the comparisons for many more iterations and there you can clearly see a time difference:
The difference isn't as pronounced here as in my previous benchmark, and in general, both operations are quite fast. This probably borders on unnecessary micro-optimization in any case. By the way, looking at the VLD tab, there are different instructions used here:
So in fact, Again, it likely doesn't matter much and I agree with you that the strictness of |
Thank you for bearing with me throughout this back-and-forth, and sorry for the many delays. Once the 0 vs. 0.0 thing is resolved this is good to go from my side. |
One last thing, just as a note to why I keep changing the PR titles: I'm trying to establish Conventional Commits throughout this project, which requires a title of the form |
Forgot you use PHPCS ... i'll look soon to see how to do this :)
:) |
Oops sorry. I promise i'll try to think about it.... 🤝 |
What an objective and instructive answer, thank you! No problem for empty as i said it's your repo :)) |
Co-authored-by: Fabian Meyer <[email protected]>
Would you want to switch to Php-Cs-Fixer ? |
Yes, that looks like a good alternative! |
Ok i'll see what i can do :)) Could you make me a list (if you have it in mind) of code-style rules you're attached to ? :) |
Another set of small improvments:
===
over==
isset
orempty
array_merge
(better performance)