drush icon indicating copy to clipboard operation
drush copied to clipboard

Lexer.php/ParserAbstract.php errors when calling namespaced class in php-cli

Open headbank opened this issue 1 year ago • 0 comments

Describe the bug If I include/require a class that is namespaced while in drush php-cli, and subsequently call that class, the call fails with this error output:

Undefined array key 265 Lexer.php:168 [warning]
Undefined array key "" ParserAbstract.php:177 [warning]
Undefined array key "" ParserAbstract.php:340 [warning]

The undefined key (265 above) varies depending on the input, for example if I invoke the class with a prepended backslash (absolute namespace) then the key cited is 263.

To Reproduce

  1. Download drush-8.4.12
  2. Create test file Foo.php in D7 document root containing namespaced class declaration (see below)
  3. Execute drush.phar -l <my-site-url> php
  4. In php-cli execute: include getcwd().'/Foo.php';
  5. Execute: echo Bar\Foo::BAR;

Test file contents:

<?php
namespace Bar;

class Foo {
    const BAR = 'baz';
}

Expected behavior String baz printed on commandline

Actual behavior Error output as above

Workaround The equivalent expression(s) do not cause an error when the classname is given indirectly using a string:

>>> echo ('Bar\Foo')::BAR;
baz

System Configuration

Q A
Drush version? 8.4.12
Drupal version? 7.x
PHP version 8.1
OS? Linux

Additional information I have extracted drush.phar and located the files/lines cited in the errors but this does not make the cause of the issue any clearer to me personally. In general terms, it appears that the command-line parser does not expect to find a backslash in this (perfectly valid in PHP) lexical position. Calling class_exists('Bar\Foo') does not cause any error, nor does new ReflectionClass('Bar\Foo').

headbank avatar May 05 '24 13:05 headbank