Skip to content

[3.10] [PHP 8.1] Fixes mod_menu/menu.php type null instead of string#36773

Merged
zero-24 merged 2 commits intojoomla:3.10-devfrom
beat:patch-12
Jan 23, 2022
Merged

[3.10] [PHP 8.1] Fixes mod_menu/menu.php type null instead of string#36773
zero-24 merged 2 commits intojoomla:3.10-devfrom
beat:patch-12

Conversation

@beat
Copy link
Copy Markdown
Contributor

@beat beat commented Jan 21, 2022

Fixes Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in administrator/modules/mod_menu/menu.php on line 383

Pull Request for Issue # none

Summary of Changes

Sometimes item is null, but in PHP 8.1, trim accepts only string type, not null.

Testing Instructions

Code-review could perfectly be enough as it's obvious.

Look at admin area of 3.10, and see error.

Actual result BEFORE applying this Pull Request

Passing null to parameter #1 ($string) of type string is deprecated in administrator/modules/mod_menu/menu.php on line 383

Expected result AFTER applying this Pull Request

That warning disappears.

Documentation Changes Required

None.

Fixes Deprecated: trim(): `Passing null to parameter #1 ($string) of type string is deprecated in administrator/modules/mod_menu/menu.php on line 383`
@beat
Copy link
Copy Markdown
Contributor Author

beat commented Jan 21, 2022

Drone failure here is due to a server issue (database connection), independent of this PR.

@richard67 richard67 added the PHP 8.x PHP 8.x deprecated issues label Jan 21, 2022
@richard67
Copy link
Copy Markdown
Member

I have tested this item ✅ successfully on d53fc78

Code review.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36773.

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Jan 22, 2022

I have tested this item ✅ successfully on d53fc78


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36773.

@Fedik Fedik removed PR-3.10-dev PHP 8.x PHP 8.x deprecated issues labels Jan 22, 2022
@Fedik
Copy link
Copy Markdown
Member

Fedik commented Jan 22, 2022

R2C


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36773.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Jan 22, 2022
@Fedik Fedik added PHP 8.x PHP 8.x deprecated issues PR-3.10-dev labels Jan 22, 2022

// Exclude if link is invalid
if (!in_array($item->type, array('separator', 'heading', 'container')) && trim($item->link) === '')
if (!in_array($item->type, array('separator', 'heading', 'container')) && trim((string) $item->link) === '')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!in_array($item->type, array('separator', 'heading', 'container')) && trim((string) $item->link) === '')
if (!in_array($item->type, array('separator', 'heading', 'container')) && empty($item->link))

Isnt an empty check better here vs trim and === ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Empty will not be true when the string is just a bunch of spaces so we need the trim, or am I wrong?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With empty also $item->link = null will fail the test here with trim it would have passed this test. But to me $item->link = null should be catched here too right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Personal taste: I don't like using empty() as it's basically same as (! (bool) $value) just that if $value doesn't exist, it considers it "empty" too.
https://www.php.net/manual/en/function.empty.php

Additionally, string '0' is considered as empty too.
Additionally, iirc typecastings have slightly changed over the lifecycle of PHP and might change in the future.

So as personal taste, I don't find empty() to be a type-clean function. Thus I'm not willing to take the risk of using empty() as suggested.

My change was a BC safe change because it does exactly what PHP does anyway, without changing anything in the behavior. If casting to (string) is not acceptable, I'm ok to merge a different way, with is_null($item->type) which would then give valid warnings for non-null non-string $item->link.

Updated PR with the slightly cleaner and equivalent fix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Side note: After reading again, my reply was way too factual and might read as being hard factual. Please accept my appologizes. I'm non-native English. The comment doesn't convey properly my gratitude. Fixing it here:

Thank you @zero-24 and @richard67 for your feedbacks and aim to have Joomla as clean as possible! 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No problem and thanks for your PRs for PHP8.1

@zero-24
Copy link
Copy Markdown
Contributor

zero-24 commented Jan 23, 2022

Will take this thanks @beat

@zero-24 zero-24 merged commit 0a1c48f into joomla:3.10-dev Jan 23, 2022
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jan 23, 2022
@zero-24 zero-24 added this to the Joomla 3.10.6 milestone Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PHP 8.x PHP 8.x deprecated issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants