yii2: StringHelper::truncate(null, 10) causes error - userland fix

What steps will reproduce the problem?

Create a StringHelper::truncate() call to a null property. Quick note: I don’t think that this is a framework issue and think it is more userland code but wanted to open the issue in case someone else receives the error.

I solved it by wrapping the helper function in a userland check: image

What is the expected result?

Was expecting an upgrade to 8.1 to go smoothly. I had to change a few things in the user code to make this work. Not sure if documentation should be updated to note this? If not feel free to close this issue.

What do you get instead?

Deprecated: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated

Additional info

Error:

yii\base\ErrorException: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /app/vendor/yiisoft/yii2/helpers/BaseStringHelper.php:132
Q A
Yii version 2.0.47
PHP version 8.1
Operating system Docker yii2 container

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 26 (23 by maintainers)

Commits related to this issue

Most upvoted comments

I would add the null-checks everywhere (since we cannot do the arguments type-forcing), I’m just mentioning that because we used to argue that since the docs are not allowing null (like with StringHelper::truncate) we should not do any checks and make it a developer’s issue.

Let’s continue discussion about 2.1 here https://github.com/yiisoft/yii2/discussions/19831

Here let’s focus on the issue only please.

  1. We can drop PHP 5.4 but I’d make it Yii 2.1 then as @bizley suggested.
  2. Type-forcing for Yii2 might be very troublesome. That’s big amount of work and unless either someone performs it and sends a PR or sponsors one of the team memers to do it, I don’t think it is worth focusing on.

It’s because we are still supporting PHP 5.4.