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

Use sprintf in exception messages #2

Merged
merged 2 commits into from
Dec 31, 2023

Conversation

szepeviktor
Copy link
Contributor

@szepeviktor szepeviktor commented Dec 29, 2023

... and is_a returns a boolean, so use !
and safe complex curly syntax

@codecov-commenter
Copy link

codecov-commenter commented Dec 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (3e7da6d) 94.87% compared to head (c6f925f) 95.00%.
Report is 4 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main       #2      +/-   ##
============================================
+ Coverage     94.87%   95.00%   +0.12%     
+ Complexity       19       18       -1     
============================================
  Files             2        2              
  Lines            39       40       +1     
============================================
+ Hits             37       38       +1     
  Misses            2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -28,17 +28,17 @@ public static function fromName(string $fqn, string $keyName): \BackedEnum
*/
public static function tryFromName(string $fqn, string $keyName): ?\BackedEnum
{
if (is_a($fqn, \BackedEnum::class, true) === false) {
throw new InvalidArgumentException('It is only possible to get names of backedEnums, "' . $fqn . '" provided');
if (!is_a($fqn, \BackedEnum::class, true)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Although in this case, the function always returns a boolean, native PHP functions are notorious for returning mixed types. the NOT operator (!) does not only check against a type, but also casts the value implicitly to a boolean, so if the return value of a function is 0 or [] it will also result in the exception being thrown. As it is complex to explain in code reviews when a NOT operator is allowed and when it is not 😛, I try to give a good example and never use it at all, even though in this case it would be possible to use it without causing any unwanted bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 🍏

Copy link
Contributor Author

@szepeviktor szepeviktor Dec 31, 2023

Choose a reason for hiding this comment

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

native PHP functions are notorious

There is a solution for this problem: https://github.com/thecodingmachine/safe

}

if (!defined("$fqn::$keyName")) {
if (!defined("{$fqn}::{$keyName}")) {
Copy link
Owner

Choose a reason for hiding this comment

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

Is this a code style or optimization I'm not aware of or just a personal preference?

Copy link
Contributor Author

@szepeviktor szepeviktor Dec 30, 2023

Choose a reason for hiding this comment

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

  • curly braces make it safer
  • easier to spot bugs
  • explicit variable start and end
  • and finally: use sprintf 😉

https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

if (!defined("$fqn::$keyName")) {
if (defined("{$fqn}::{$keyName}") === false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a new ✨ === false

Copy link
Owner

@PrinsFrank PrinsFrank left a comment

Choose a reason for hiding this comment

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

Thank you!!!!

@PrinsFrank PrinsFrank merged commit 7f3bae2 into PrinsFrank:main Dec 31, 2023
7 checks passed
@szepeviktor szepeviktor deleted the exception branch December 31, 2023 15:33
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.

3 participants