hashar added a comment.

So I don't think the PHP namespace lookup works for static methods. Eg both() would not end up being resolved as \HamCrest\Matchers::both(). A lame test case:

A namespaced class definition:

<?php
namespace namespaced {
    class StaticClass {
        public static function hello() {
            return "Hello";
        }
    }
}

Script using it:

<?php
use namespaced\StaticClass;

spl_autoload_register( function () { 
    include 'namespaced.php';
} );

print "namespaced\StaticClass::hello():\n";
print ">>> " . namespaced\StaticClass::hello() . "\n";  // works
print "hello():\n";
print ">>> " . hello() . "\n";  // Call to undefined function hello() 

Output with PHP 5.6.29:

$ php exec.php
namespaced\StaticClass::hello():
>>> Hello
hello():
PHP Fatal error:  Call to undefined function hello() in /tmp/exec.php on line 11
PHP Stack trace:
PHP   1. {main}() /tmp/exec.php:0

Fatal error: Call to undefined function hello() in /tmp/exec.php on line 11

Call Stack:
    0.0002     231576   1. {main}() /tmp/exec.php:0

That might why Hamcrest has a Hamcrest\Util::registerGlobalFunctions();: which include a file defining a set of global functions which in turns invoke the static method using the full canonical path. Extracted from the file:

vendor/hamcrest/hamcrest-php/hamcrest/Hamcrest.php
<?php
if (!function_exists('both')) {
    function both(\Hamcrest\Matcher $matcher)
    {
        return \Hamcrest\Core\CombinableMatcher::both($matcher);
    }
}

So potentially in our tests, we can explicitly register the global functions using:

\Hamcrest\Util::registerGlobalFunctions();:

And can then use both().

Which works around the following PHP code that fails to lookup up the both() static method:

use Hamcrest\Matchers;
both();  // fails

TASK DETAIL
https://phabricator.wikimedia.org/T158674

EMAIL PREFERENCES
https://phabricator.wikimedia.org/settings/panel/emailpreferences/

To: hashar
Cc: gerritbot, hashar, Addshore, Aleksey_WMDE, aude, Aklapper, Th3d3v1ls, Ramalepe, Liugev6, Lewizho99, Maathavan, D3r1ck01, Izno, Wikidata-bugs, Mbch331
_______________________________________________
Wikidata-bugs mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/wikidata-bugs

Reply via email to