Page MenuHomePhabricator

Incorrect fix for `MediaWiki.PHPUnit.AssertionOrder.WrongOrder` and `array()`
Open, HighPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

  • Install packages: composer require squizlabs/php_codesniffer mediawiki/mediawiki-codesniffer dealerdirect/phpcodesniffer-composer-installer
  • Create files:
.phpcs.xml.dist
<?xml version="1.0"?>
<ruleset>
    <rule ref="MediaWiki.PHPUnit.AssertionOrder" />
</ruleset>
<?php

use PHPUnit\Framework\TestCase;

class Test extends TestCase {
    function test() {
        $this->assertEquals( $array, [ 'a', 'b', 'c' ] );
        $this->assertEquals( $array, array( 'a', 'b', 'c' ) );
        $this->assertEquals( $array, array( 'a' => 'b' ) );

        $this->assertEquals( $array, [ 'a', 'b', 'c' ], 'message' );
        $this->assertEquals( $array, array( 'a', 'b', 'c' ), 'message' );
        $this->assertEquals( $array, array( 'a' => 'b' ), 'message' );
    }
}
  • Run phpcbf on the file.

What happens?:

File is now

<?php

use PHPUnit\Framework\TestCase;

class Test extends TestCase {
    function test() {
        $this->assertEquals( [ 'a', 'b', 'c' ], $array );
        $this->assertEquals( array( 'a', $array, 'b', 'c' ) );
        $this->assertEquals( array( 'a' => 'b' ), $array );

        $this->assertEquals( [ 'a', 'b', 'c' ], $array, 'message' );
        $this->assertEquals( array( 'a', $array, 'b', 'c' ), 'message' );
        $this->assertEquals( array( 'a' => 'b' ), $array, 'message' );
    }
}

What should have happened instead?:

File is now

<?php

use PHPUnit\Framework\TestCase;

class Test extends TestCase {
    function test() {
        $this->assertEquals( [ 'a', 'b', 'c' ], $array );
        $this->assertEquals( array( 'a', 'b', 'c' ), $array );
        $this->assertEquals( array( 'a' => 'b' ), $array );

        $this->assertEquals( [ 'a', 'b', 'c' ], $array, 'message' );
        $this->assertEquals( array( 'a', 'b', 'c' ), $array, 'message' );
        $this->assertEquals( array( 'a' => 'b' ), $array, 'message' );
    }
}

Software version (skip for WMF-hosted wikis like Wikipedia): phpcs 3.7.2, mediawiki/mediawiki-codesniffer 41.0.0

Other information (browser name/version, screenshots, etc.):

Note that we are a third-party user of this package, and our coding standards (which are beyond my control) require that we use array( ... ) rather than [ ... ].

Looks like the sniff needs to also consider T_ARRAY in addition to T_OPEN_SHORT_ARRAY (and use parenthesis_closer instead of bracket_closer for the former, of course), and then T_CLOSE_PARENTHESIS in addition to T_CLOSE_SHORT_ARRAY as well.

Event Timeline

Krinkle triaged this task as High priority.Feb 19 2026, 2:15 PM
Krinkle moved this task from Untriaged to Accepted rule changes on the MediaWiki-Codesniffer board.
Krinkle subscribed.

Confirmed. This bug is still reproducible. And it is indeed specific to legacy array() syntax. If your input uses [] syntax, the bug doesn't happen.

Input A
$this->assertEquals( $array, array( 'a', 'b', 'c' ) );
Output A
$this->assertEquals( [ 'a', $array, 'b', 'c' ] );
Input B
$this->assertEquals( $array, [ 'a', 'b', 'c' ] );
Output B
$this->assertEquals( [ 'a', 'b', 'c' ], $array );