-
Notifications
You must be signed in to change notification settings - Fork 1.9k
apply strict in_array with pass true to 3rd parameter #3565
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
Conversation
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.
Looks good! Curious about the credit card one.
@@ -245,9 +245,11 @@ public function valid_cc_number(string $ccNumber = null, string $type): bool | |||
} | |||
|
|||
// Make sure it's a valid length for this card | |||
$lengths = explode(',', $info['length']); | |||
$lengths = array_map(function ($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.
Is this one going to be worth the extra effort to ensure strict types?
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.
phpstan complain about that, I can add @phpstan-ignore-line
ignore it if you want.
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 seems casting to int is required:
$info = [
'length' => '1,2,3',
];
$lengths = explode(',', $info['length']);
var_dump(in_array(1, $lengths, true));
var_dump(in_array('1', $lengths, true));
$lengths = array_map(function ($value) {
return (int) $value;
}, explode(',', $info['length']));
var_dump(in_array(1, $lengths, true));
Got the following output:
bool(false)
bool(true)
bool(true)
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.
another solution is cast the strlen()
itself to string.
if (! in_array((string) strlen($ccNumber), $lengths, true))
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.
Updated to cast the strlen()
value itself to string. It should be ok now.
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 meant if the strict version of in_array
was worth the trouble of a custom array mapping, for performance and code clarity.
Edit: Just saw your updated version, original question is moot!
270, | ||
(float) 90, | ||
(float) 180, | ||
(float) 270, |
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.
Why not just write these as 90.0
, 180.0
, 270.0
?
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.
because we can pass "90" as float ref https://3v4l.org/BFb9u
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 don't understand the difference. Isn't (float) 90 === 90.0
?
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.
oh, you're correct. I will update to 90.0
, not in front of pc right now
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.
fixed.
…tead of 2 for server protocol
All green 🎉 |
Ensure to make strict
in_array
.Checklist: