Skip to content

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

Merged
merged 5 commits into from
Aug 30, 2020

Conversation

samsonasik
Copy link
Member

Ensure to make strict in_array.

Checklist:

  • Securely signed commits

Copy link
Member

@MGatner MGatner left a 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) {
Copy link
Member

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?

Copy link
Member Author

@samsonasik samsonasik Aug 29, 2020

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.

Copy link
Member Author

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)

see https://3v4l.org/lPubnY

Copy link
Member Author

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

Copy link
Member Author

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.

Copy link
Member

@MGatner MGatner Aug 29, 2020

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,
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed.

@samsonasik
Copy link
Member Author

All green 🎉

@MGatner MGatner merged commit cc8154f into codeigniter4:develop Aug 30, 2020
@samsonasik samsonasik deleted the in_array_strict branch August 30, 2020 13:26
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