Skip to content

Fixed strlen(): Passing null to parameter in SalesRule/Model/Resource/Rule/Collection.php #3675

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 6 commits into from
Feb 5, 2024

Conversation

kiatng
Copy link
Contributor

@kiatng kiatng commented Nov 23, 2023

…rce/Rule/Collection.php

PHP8.1 OM v20.2

    [type] => 8192:E_DEPRECATED
    [message] => strlen(): Passing null to parameter #1 ($string) of type string is deprecated
    [file] => .../app/code/core/Mage/SalesRule/Model/Resource/Rule/Collection.php
    [line] => 75
    [uri] => /checkout/cart/add/uenc/.../product/7233/form_key/KHbMmBqhuxasoRHy/

@github-actions github-actions bot added the Component: SalesRule Relates to Mage_SalesRule label Nov 23, 2023
@kiatng kiatng added PHP 8.1 Related to PHP 8.1 and removed Component: SalesRule Relates to Mage_SalesRule labels Nov 23, 2023
Copy link
Contributor

@sreichel sreichel left a comment

Choose a reason for hiding this comment

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

Strict type safe?

@kiatng
Copy link
Contributor Author

kiatng commented Nov 28, 2023

In our context, $couponCode is expected to be string|null. If it's other types, let it throws error?

@sreichel
Copy link
Contributor

sreichel commented Nov 28, 2023

The method says string (or empty string) but its an public method and we dont have type hint for parameters, so we can code this

$m = new Mage_SalesRule_Model_Resource_Rule_Collection();
$m->setValidationFilter(1, 1, false); # false couponCode

This would bypass !== null and run strlen(false). No error actually, but maybe later. And an error with strict types on.

@kiatng
Copy link
Contributor Author

kiatng commented Nov 28, 2023

but maybe later. And and an error with strict types on.

That's the point, we want the error with strict type on. If we cast for scalar type, no error. If we want strict type, we shouldn't cast?

In short, my arguments

  1. We want to fix the null deprecation notice for PHP8.
  2. We fix it by checking for null only, no casting.
  3. When we introduce strict type, no change is required as we want the errors for wrong type.

What do you think?

@sreichel
Copy link
Contributor

sreichel commented Nov 28, 2023

If we cast for scalar type, no error. If we want strict type, we shouldn't cast?

If we want strict types, we have to cast ... that is what php does internally w/o strict types on.

"we shouldn't cast" as soon we have typed method parameters.

  1. yes, but it is not only about null. Why not fix it "completly"?
  2. casting only when it makes sense (passing scalar to php interal, like strlen)
  3. dont get it. Turn on strict types should work w/o error.

@github-actions github-actions bot added the Component: SalesRule Relates to Mage_SalesRule label Jan 31, 2024
@kiatng
Copy link
Contributor Author

kiatng commented Feb 1, 2024

I have second thought. Compare

if (strlen($couponCode)) { // S0
// vs
if ($couponCode !== null && strlen($couponCode)) { //S1
// vs
if (is_scalar($couponCode) && strlen((string)$couponCode)) { //S2

If $couponCode is non-scalar, say an array, S0 and S1 will throw an error "Fatal error: Uncaught TypeError: strlen(): Argument #1 ($string) must be of type string, array given" whereas S2 won't. IMHO, S1 seems more correct.

@sreichel
Copy link
Contributor

sreichel commented Feb 1, 2024

Mhh. With an non-scalar value (S2) should ignore that if-statement w/o throwing an error. Not? (thought is was the change about)

@kiatng
Copy link
Contributor Author

kiatng commented Feb 1, 2024

This PR and PR #3775, I think an error should be thrown, it'll aid debugging. If there is a bug, it will be silent and make it hard to uncover the bug.

[edit] Like PR #3774, no exception on typo, the bug lives on and quite difficult, took hours, to uncover.

[edit]

(thought is was the change about)

This PR is to fix null deprecation, not about suppressing errors.

@kiatng kiatng requested a review from sreichel February 1, 2024 05:19
addison74
addison74 previously approved these changes Feb 5, 2024
@fballiano fballiano changed the title Fixed strlen(): Passing null to parameter #1 in SalesRule/Model/Resou… Fixed strlen(): Passing null to parameter in SalesRule/Model/Resource/Rule/Collection.php Feb 5, 2024
fballiano
fballiano previously approved these changes Feb 5, 2024
@fballiano
Copy link

For some reason I cannot merge because it triggers an error:

Screenshot 2024-02-05 alle 10 21 37

I'll try again later

@fballiano fballiano dismissed stale reviews from addison74 and themself via f8ceaf1 February 5, 2024 10:23
@fballiano fballiano merged commit d573ece into OpenMage:main Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: SalesRule Relates to Mage_SalesRule PHP 8.1 Related to PHP 8.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants