Search code examples
phpdrupalassertdeprecated

PHP 7.3 Deprecated Namespace Assert in Drupal


English isn't my first language, please be patient.

From PHP 7.3 Deprecated Features

Namespaced assert()

Declaring a function called assert() inside a namespace is deprecated. The assert() function is subject to special handling by the engine, which may lead to inconsistent behavior when defining a namespaced function with the same name.

I'm working on Drupal 7.72, I was checking the process to migrate to PHP 7.3, when it comes to deprecated features I found that assert() function is used by Drupal core in several files in the folder \misc\typo3\. The ones I have identified as potential risks are the following:

  • docroot\misc\typo3\drupal-security\PharExtensionInterceptor.php
  • docroot\misc\typo3\phar-stream-wrapper\src\Interceptor\ConjunctionInterceptor.php
  • docroot\misc\typo3\phar-stream-wrapper\src\Interceptor\PharExtensionInterceptor.php
  • docroot\misc\typo3\phar-stream-wrapper\src\Interceptor\PharMetaDataInterceptor.php
  • docroot\misc\typo3\phar-stream-wrapper\src\PharStreamWrapper.php
  • docroot\misc\typo3\phar-stream-wrapper\src\Assertable.php
  • docroot\misc\typo3\phar-stream-wrapper\src\Behavior.php
  • docroot\misc\typo3\phar-stream-wrapper\src\Manager.php

I'm really confused about how Drupal is declaring assert functions even though they are deprecated, maybe I'm misreading the Documentation? There's some way to test these files to be sure that everything will be working on PHP 7.3?


Solution

  • Those files should not cause any problems regarding the deprecated namespaced assert().

    That deprecation refers to a namespaced assert() function, but what you have in those files are class methods.

    Here's a basic example of the difference in case my explanation is not clear:

    Namespaced assert function (deprecated)

    <?php
    namespace Foo;
    
    function assert() {}
    

    This will cause:

    Deprecated: Defining a custom assert() function is deprecated, as the function has special semantics


    Class method (not deprecated)

    <?php
    namespace Foo;
    
    class Something
    { 
        public function assert() {}
    }
    

    No problem because the assert function is contained in a class.