Problem/Motivation

Deprecated function: mb_substr(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\commerce_paypal\CheckoutSdk::formatAddress() (line 464 of /home/mysite/public_html/modules/contrib/commerce_paypal/src/CheckoutSdk.php)
#0 /home/mysite/public_html/core/includes/bootstrap.inc(346): _drupal_error_handler_real()
#1 [internal function]: _drupal_error_handler()
#2 /home/mysite/public_html/modules/contrib/commerce_paypal/src/CheckoutSdk.php(464): mb_substr()
#3 /home/mysite/public_html/modules/contrib/commerce_paypal/src/CheckoutSdk.php(352): Drupal\commerce_paypal\CheckoutSdk::formatAddress()
#4 /home/mysite/public_html/modules/contrib/commerce_paypal/src/CheckoutSdk.php(133): Drupal\commerce_paypal\CheckoutSdk->prepareOrderRequest()
#5 /home/mysite/public_html/modules/contrib/commerce_paypal/src/Plugin/Commerce/PaymentGateway/Checkout.php(446): Drupal\commerce_paypal\CheckoutSdk->updateOrder()
#6 /home/mysite/public_html/modules/contrib/commerce/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php(179): Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway\Checkout->createPayment()
#7 /home/mysite/public_html/modules/contrib/commerce/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowWithPanesBase.php(546): Drupal\commerce_payment\Plugin\Commerce\CheckoutPane\PaymentProcess->buildPaneForm()
#8 [internal function]: Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowWithPanesBase->buildForm()
#9 /home/mysite/public_html/core/lib/Drupal/Core/Form/FormBuilder.php(531): call_user_func_array()
#10 /home/mysite/public_html/core/lib/Drupal/Core/Form/FormBuilder.php(278): Drupal\Core\Form\FormBuilder->retrieveForm()
#11 /home/mysite/public_html/core/lib/Drupal/Core/Form/FormBuilder.php(219): Drupal\Core\Form\FormBuilder->buildForm()
#12 /home/mysite/public_html/modules/contrib/commerce/modules/checkout/src/Controller/CheckoutController.php(143): Drupal\Core\Form\FormBuilder->getForm()
#13 [internal function]: Drupal\commerce_checkout\Controller\CheckoutController->formPage()
#14 /home/mysite/public_html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#15 /home/mysite/public_html/core/lib/Drupal/Core/Render/Renderer.php(564): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#16 /home/mysite/public_html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#17 /home/mysite/public_html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#18 /home/mysite/public_html/vendor/symfony/http-kernel/HttpKernel.php(158): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#19 /home/mysite/public_html/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#20 /home/mysite/public_html/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#21 /home/mysite/public_html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#22 /home/mysite/public_html/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#23 /home/mysite/public_html/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#24 /home/mysite/public_html/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle()
#25 /home/mysite/public_html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#26 /home/mysite/public_html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#27 /home/mysite/public_html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#28 /home/mysite/public_html/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle()
#29 /home/mysite/public_html/index.php(19): Drupal\Core\DrupalKernel->handle()
#30 {main}

Steps to reproduce

This error seems to be appearing during the checkout process as the location in dblog shows
/checkout/order number/payment

referrer is /checkout/order number/review

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

miketsai created an issue. See original summary.

matheusmaciel’s picture

Assigned: Unassigned » matheusmaciel

I'll be working on this issue.

matheusmaciel’s picture

Assigned: matheusmaciel » Unassigned
Status: Active » Needs review
StatusFileSize
new2.89 KB

I worked on the CheckoutSdk.php file changing the mb_substr(): passing the first parameter as a string for those that were passing a null value. Waiting for the review.

Johnny Santos’s picture

Assigned: Unassigned » Johnny Santos

Going in for the review

99gs3’s picture

Thanks for the patch.

Test so far seems to have resolved the issue.

andregp’s picture

Assigned: Johnny Santos » andregp

I talked with Johnny Santos and I'm doing a review here to help...

andregp’s picture

Assigned: andregp » Unassigned
Status: Needs review » Needs work

The best thing here would be to check for each function being called inside the mb_substr() method to be sure if they can or not return NULL. In come cases the null coalescing operator (??) is not necessary. For example on the line 479

  $return['name'] = [
-   'full_name' => mb_substr($address->getGivenName() . ' ' . $address->getFamilyName(), 0, 300),
+   'full_name' => mb_substr(($address->getGivenName() . ' ' . $address->getFamilyName()) ?? '', 0, 300),
  ];

The first parameter will never be null because it is being concatenated with the string ' '.
There are some functions tough that can return null, such as label() (inside 'brand_name' => mb_substr($order->getStore()->label(), 0, 127), at line 371) because the documentation of label() says "@return string|\Drupal\Core\StringTranslation\TranslatableMarkup|null
The label of the entity, or NULL if there is no label defined.
"
Some functions inside the mb_substr() are not from this module, so we need to check the documentation outside. I did this and the other functions I found that may return null are getLocality() (the one responsible for the error in the IS) and getPostalCode() at lines 464 and 466 as their documentation declares @return mixed (which can include null).

andregp’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB
new1.74 KB

I reverted some changes to keep null coalescing operator only where the functions could return a null value as the first param of mb_substr().

elber’s picture

Assigned: Unassigned » elber
elber’s picture

Assigned: elber » Unassigned
Status: Needs review » Reviewed & tested by the community

Hi I revised the patch and it's good for me.

The issue's scope was resolved, and the coalescing operator sintaxe was wrote correctly.

This doc has a lot informations about coalescing operator (https://www.php.net/manual/pt_BR/migration70.new-features.php)

  • jsacksick committed 709e848 on 8.x-1.x authored by andregp
    Issue #3277096 by andregp, matheusmaciel, elber: Deprecated function:...
jsacksick’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.