How to fix the “Passing null to parameter of type string is deprecated” error when deploying static content in Magento 2.4.4?

If we create a custom theme in Magento 2.4.4, we may encounter the following error when deploying static content: “Passing null to parameter of type string is deprecated”.

What causes this error? As of version 2.4.4, Magento uses PHP8.1. It’s actually PHP8.1 that throws that error. We can find more information about it on the PHP.Watch website. Essentially, a null parameter is passed when a string is expected.

So where does this happen in Magento when we deploy our custom theme? The code that causes this error is found in magento\framework\App\Utility\Files.php.

    /**
     * Accumulate files from themes
     *
     * @param string $area
     * @param string $locale
     * @param string $filePattern
     * @param array $result
     * @return void
     */
    private function accumulateThemeStaticFiles($area, $locale, $filePattern, &$result)
    {
        foreach ($this->themePackageList->getThemes() as $themePackage) {
            $themeArea = $themePackage->getArea();
            if ($area == '*' || $area == $themeArea) {
                $files = [];
                $themePath = str_replace(DIRECTORY_SEPARATOR, '/', $themePackage->getPath());
                $paths = [
                    $themePath . "/web",
                    $themePath . "/*_*/web",
                    $themePath . "/web/i18n/{$locale}",
                    $themePath . "/*_*/web/i18n/{$locale}"
                ];
                $this->_accumulateFilesByPatterns($paths, $filePattern, $files);
                $regex = '#^' . $themePath .
                    '/((?P<module>[a-z\d]+_[a-z_\d]+)/)?web/(i18n/(?P<locale>[a-z_]+)/)?(?P<path>.+)$#i';
                foreach ($files as $file) {
                    if (preg_match($regex, $file, $matches)) {
                        $result[] = [
                            $themeArea,
                            $themePackage->getVendor() . '/' . $themePackage->getName(),
                            $matches['locale'],
                            $matches['module'],
                            $matches['path'],
                            $file,
                        ];
                    } else {
                        throw new \UnexpectedValueException("Could not parse theme static file '$file'");
                    }
                }

                if (!$files) {
                    $result[] = [
                        $themeArea,
                        $themePackage->getVendor() . '/' . $themePackage->getName(),
                        null,
                        null,
                        null,
                        null
                    ];
                }
            }
        }
    }

When Magento assembles the assets associated with a theme, it creates the information for an empty one, if none are found. This is where the null values are passed.

To fix the issue, we can modify the code to pass empty strings instead of null values. To achieve this in the core code, we need to build a patch like this one.

diff --git a/App/Utility/Files.php b/App/Utility/Files.php
index 00c1e89..b872c08 100644
--- a/App/Utility/Files.php
+++ b/App/Utility/Files.php
@@ -956,10 +956,10 @@ class Files
                     $result[] = [
                         $themeArea,
                         $themePackage->getVendor() . '/' . $themePackage->getName(),
-                        null,
-                        null,
-                        null,
-                        null
+                        '',
+                        '',
+                        '',
+                        ''
                     ];
                 }
             }

If we don’t want to go through the trouble of creating a patch, we can simply add an image to our custom theme. For example, we could add a theme preview image just like in our Magento parent theme.

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Magento Luma</title>
    <parent>Magento/blank</parent>
    <media>
        <preview_image>media/preview.jpg</preview_image>
    </media>
</theme>

Or we could add a logo image.

Now, we will no longer receive the error message when deploying our static content.

Watch on Youtube!

Do you have other Magento 2 issues that you would like help with? Let me know.

4 thoughts on “How to fix the “Passing null to parameter of type string is deprecated” error when deploying static content in Magento 2.4.4?

  1. Thanks for the tutorial.
    I’m not a programmer, I have no way of knowing how these patches are created, they should be in that file. Can there be a more detailed tutorial?

Leave a Reply

Your email address will not be published.