WordPress.org

Make WordPress Core

Ticket #50899: 50899-remove-create_function.patch

File 50899-remove-create_function.patch, 2.6 KB (added by jrf, 7 months ago)
  • tests/phpunit/includes/plural-form-function.php

    From 859641a87650daff5a5cfbb681029a9ff70aeada Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Tue, 11 Aug 2020 04:05:58 +0200
    Subject: [PATCH] PHP 8.0: change `create_function()` to closure
    
    `create_function()` has been removed in PHP 8.
    
    While not optimal due to the usage of `eval`, this change _should_ work and shouldn't cause any real world issues as the logic is only used within the test suite.
    ---
     .../phpunit/includes/plural-form-function.php | 36 ++++++++++---------
     tests/phpunit/tests/pomo/pluralForms.php      |  4 ---
     2 files changed, 19 insertions(+), 21 deletions(-)
     rewrite tests/phpunit/includes/plural-form-function.php (62%)
    
    diff --git a/tests/phpunit/includes/plural-form-function.php b/tests/phpunit/includes/plural-form-function.php
    dissimilarity index 62%
    index 576ece6b03..67498198e9 100644
    a b  
    1 <?php
    2 
    3 /**
    4  * Legacy plural form function.
    5  *
    6  * @param int $nplurals
    7  * @param string $expression
    8  */
    9 function tests_make_plural_form_function( $nplurals, $expression ) {
    10         $expression = str_replace( 'n', '$n', $expression );
    11         $func_body  = "
    12                 \$index = (int)($expression);
    13                 return (\$index < $nplurals)? \$index : $nplurals - 1;";
    14 
    15         // phpcs:ignore WordPress.PHP.RestrictedPHPFunctions.create_function_create_function
    16         return create_function( '$n', $func_body );
    17 }
     1<?php
     2
     3/**
     4 * Legacy plural form function.
     5 *
     6 * @param int $nplurals
     7 * @param string $expression
     8 */
     9function tests_make_plural_form_function( $nplurals, $expression ) {
     10        $closure = function ( $n ) use ( $nplurals, $expression ) {
     11                $expression = str_replace( 'n', $n, $expression );
     12
     13                // phpcs:ignore Squiz.PHP.Eval -- This is test code, not production.
     14                $index = (int) eval( 'return ' . $expression . ';' );
     15                return ( $index < $nplurals ) ? $index : $nplurals - 1;
     16        };
     17
     18        return $closure;
     19}
  • tests/phpunit/tests/pomo/pluralForms.php

    diff --git a/tests/phpunit/tests/pomo/pluralForms.php b/tests/phpunit/tests/pomo/pluralForms.php
    index 032efc8006..2d53c29c56 100644
    a b class PluralFormsTest extends WP_UnitTestCase { 
    7474         * @group external-http
    7575         */
    7676        public function test_regression( $lang, $nplurals, $expression ) {
    77                 if ( version_compare( phpversion(), '7.2', '>=' ) ) {
    78                         $this->markTestSkipped( 'Lambda functions are deprecated in PHP 7.2' );
    79                 }
    80 
    8177                require_once dirname( dirname( __DIR__ ) ) . '/includes/plural-form-function.php';
    8278
    8379                $parenthesized = self::parenthesize_plural_expression( $expression );