php 8 expected expression operand but got T_NAME_FULLY_QUALIFIED

When using dmu_upgrade and PHP 8, I get the error

Indexing...
done.
 [warning] my_module does not implement hook_block_configure.
 [error]  Error at line 26:14 in file /var/www/html/web/modules/custom/my_module/my_module.module: excepted expression operand but got T_NAME_FULLY_QUALIFIED 
 [notice] #0 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(1227): Pharborist\Parser->exprOperand()
#1 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(525): Pharborist\Parser->expr()
#2 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(484): Pharborist\Parser->exprStatement()
#3 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2291): Pharborist\Parser->statement()
#4 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2239): Pharborist\Parser->innerStatement()
#5 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2250): Pharborist\Parser->innerStatementList(Object(Pharborist\StatementBlockNode), '}')
#6 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(461): Pharborist\Parser->innerStatementBlock()
#7 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(575): Pharborist\Parser->statement()
#8 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(424): Pharborist\Parser->_if()
#9 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2291): Pharborist\Parser->statement()
#10 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2239): Pharborist\Parser->innerStatement()
#11 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2250): Pharborist\Parser->innerStatementList(Object(Pharborist\StatementBlockNode), '}')
#12 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2228): Pharborist\Parser->innerStatementBlock()
#13 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(2166): Pharborist\Parser->body(Object(Pharborist\Functions\FunctionDeclarationNode))
#14 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(356): Pharborist\Parser->functionDeclaration()
#15 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(321): Pharborist\Parser->topStatement()
#16 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(202): Pharborist\Parser->topStatementList(Object(Pharborist\RootNode))
#17 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(240): Pharborist\Parser->buildTree(Object(Pharborist\TokenIterator))
#18 /var/www/html/vendor/jcnventura/pharborist/src/Parser.php(219): Pharborist\Parser::parseSource('<?php\n\n/**\n * @...', '/var/www/html/w...')
#19 /var/www/html/web/modules/contrib/drupalmoduleupgrader/src/Target.php(211): Pharborist\Parser::parseFile('/var/www/html/w...')
#20 /var/www/html/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Indexer/Functions.php(111): Drupal\drupalmoduleupgrader\Target->open('/var/www/html/w...')
#21 /var/www/html/web/modules/contrib/drupalmoduleupgrader/src/ConverterBase.php(67): Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer\Functions->get('my_module_block...')
#22 /var/www/html/web/modules/contrib/drupalmoduleupgrader/src/Plugin/DMU/Converter/Blocks.php(40): Drupal\drupalmoduleupgrader\ConverterBase->executeHook(Object(Drupal\drupalmoduleupgrader\Target), 'hook_block_info')
#23 /var/www/html/web/modules/contrib/drupalmoduleupgrader/src/Commands/DrupalmoduleupgraderCommands.php(188): Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Blocks->convert(Object(Drupal\drupalmoduleupgrader\Target))
#24 [internal function]: Drupal\drupalmoduleupgrader\Commands\DrupalmoduleupgraderCommands->upgrade('my_module', Array)
#25 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(257): call_user_func_array(Array, Array)
#26 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
#27 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#28 /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(350): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#29 /var/www/html/vendor/symfony/console/Command/Command.php(255): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#30 /var/www/html/vendor/symfony/console/Application.php(1027): Symfony\Component\Console\Command\Command->run(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#31 /var/www/html/vendor/symfony/console/Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(Consolidation\AnnotatedCommand\AnnotatedCommand), Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#32 /var/www/html/vendor/symfony/console/Application.php(149): Symfony\Component\Console\Application->doRun(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(118): Symfony\Component\Console\Application->run(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(48): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 /var/www/html/vendor/drush/drush/drush.php(72): Drush\Runtime\Runtime->run(Array)
#36 /var/www/html/vendor/drush/drush/drush(4): require('/var/www/html/v...')
#37 /var/www/html/vendor/bin/drush(117): include('/var/www/html/v...')
#38 {main}

The error is thrown because it converts some of the file to Drupal 9 syntax, then it runs again maybe, and fails on line 26 which has become $cache = \Drupal::cache('cache_block')->get($cid);?

line 26:14 is the backslash.

Steps to reproduce

Try to upgrade the attached module when using PHP 8.0

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

solideogloria created an issue. See original summary.

solideogloria’s picture

Priority: Normal » Major

The module is basically unusable with PHP 8 right now.

joseph.olstad’s picture

might be related:
#3287082: Make dmu-upgrade itself run without errors on Drupal 10

I'll push the rector fixes for D10 compatibility, might help.

joseph.olstad’s picture

Title: PHP 8.0 excepted expression operand but got T_NAME_FULLY_QUALIFIED » PHP 7.4 and PHP 8.0 compatibility fixes
Issue summary: View changes
joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

Status: Active » Needs work
gábor hojtsy’s picture

I posted #3301592: Drupal module upgrader tests fail on PHP 7.3 which passes fine :) Once that lands, we can start looking at PHP 7.4 in #3301606: Drupal module upgrader tests fail on PHP 7.3 and PHP 7.4 :)

joseph.olstad’s picture

ok I tagged 8.x-1.9 which is now thanks to Gàbor Hojtsy compatible with PHP 7.4

TODO: fixes for PHP 8.0 and 8.1 compatibility.

gábor hojtsy’s picture

Title: PHP 7.4 and PHP 8.0 compatibility fixes » Drupal module upgrader not compatible with PHP 8.0 and/or PHP 8.1
Status: Needs work » Active

Let's retitle for that. Also no patch yet so marking as active.

gábor hojtsy’s picture

Issue summary: View changes

gábor hojtsy’s picture

So I set up a drupalpod with this mymodule sample provided (thanks!). Although the tests fail a similar way. Strange thing is the dmu-analyse and dmu-upgrade does not fail that bad on this PHP 8.1, so maybe it is not actually running PHP 8.1 (the phpinfo on Drupal also says its PHP 8.1, hm):

$ php -v
Notice: running 'php -v' in ddev
PHP 8.1.8 (cli) (built: Jul 11 2022 08:55:24) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.8, Copyright (c), by Zend Technologies

$ drush dmu-analyze mymodule
Notice: running 'drush dmu-analyze mymodule' in ddev
Indexing...
done.
 [warning] Undefined array key "summary" AnalyzerBase.php:20
 [warning] Undefined array key "summary" AnalyzerBase.php:20
 [warning] Undefined array key "summary" AnalyzerBase.php:20
 [warning] Trying to access array offset on value of type null drupalmoduleupgrader.module:127
 [warning] Trying to access array offset on value of type null drupalmoduleupgrader.module:127
 [warning] Trying to access array offset on value of type null drupalmoduleupgrader.module:127
 [warning] Trying to access array offset on value of type null drupalmoduleupgrader.module:127
 [warning] Trying to access array offset on value of type null drupalmoduleupgrader.module:127

$ drush dmu-upgrade mymodule
Notice: running 'drush dmu-upgrade mymodule' in ddev
Indexing...
done.
gábor hojtsy’s picture

Same drupalpod, I don't have much luck running the phpunit tests, which may be an autoloader problem?!

gitpod /workspace/DrupalPod (main) $ vendor/bin/phpunit web/modules/contrib/drupalmoduleupgrader
Notice: running 'php vendor/bin/phpunit web/modules/contrib/drupalmoduleupgrader' in ddev
PHP Fatal error:  Uncaught Error: Class "Drupal\Tests\drupalmoduleupgrader\Unit\TestBase" not found in /var/www/html/repos/drupalmoduleupgrader/tests/src/Unit/IssueTest.php:11
Stack trace:
#0 /var/www/html/vendor/phpunit/phpunit/src/Util/FileLoader.php(65): include_once()
#1 /var/www/html/vendor/phpunit/phpunit/src/Util/FileLoader.php(49): PHPUnit\Util\FileLoader::load('/var/www/html/r...')
#2 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php(394): PHPUnit\Util\FileLoader::checkAndLoad('/var/www/html/r...')
#3 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php(522): PHPUnit\Framework\TestSuite->addTestFile('/var/www/html/r...')
#4 /var/www/html/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(98): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(120): PHPUnit\Runner\BaseTestRunner->getTest('/var/www/html/r...', Array)
#6 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(96): PHPUnit\TextUI\Command->run(Array, true)
#7 /var/www/html/vendor/phpunit/phpunit/phpunit(98): PHPUnit\TextUI\Command::main()
#8 /var/www/html/vendor/bin/phpunit(123): include('/var/www/html/v...')
#9 {main}
liam morland’s picture

Issue tags: +PHP 8.0
gábor hojtsy’s picture

Issue tags: +PHP 8.1
gábor hojtsy’s picture

Title: Drupal module upgrader not compatible with PHP 8.0 and/or PHP 8.1 » Drupal module upgrader dmu_upgrade not compatible with PHP 8.0 and/or PHP 8.1

So what people are finding is analyzing on PHP 8.0/8.1 runs fine, but not making the automated fixes. So one interim fix we can introduce to make it compatible with Drupal 10 is that we bail in the automated fix drush command when on PHP 8.0+, and point to this issue :) But say people can still run the analyze thing. That would be one step more useful compared to not being compatible at all.

I worked on https://github.com/jcnventura/pharborist/pull/1 and @jcnventura landed it but it did not yet quite resolve all PHP 8 problems.

I think #3287082: Make dmu-upgrade itself run without errors on Drupal 10 would be the best place to do that workaround for now.

solideogloria’s picture

Title: Drupal module upgrader dmu_upgrade not compatible with PHP 8.0 and/or PHP 8.1 » dmu_upgrade not compatible with PHP 8.0 and/or PHP 8.1
solideogloria’s picture

Title: dmu_upgrade not compatible with PHP 8.0 and/or PHP 8.1 » dmu_upgrade not compatible with PHP 8.0+

AndyF made their first commit to this issue’s fork.

andyf’s picture

I've set up GitLab CI. I was hoping to have a matrix where we could test D9 with PHP 8.1, but that didn't work, so we're now just testing:

  • D9/PHP 7.4
  • D10/PHP 8.1

D9/PHP 7.4 is already failing without changes from this branch, see #3465500: Use GitLab CI and fix tests. I'm not sure how best to continue testing D9 when the template variables are adjusted and the current stable becomes 11.

andyf’s picture

Just FYI I've forked pharborist and started working on updating it so the tests pass on higher versions of PHP. I've got it passing on 8.0, and using that the tests for this module also pass on 8.0 except for the existing failure mentioned in #20.

I'm going to look at getting it working with 8.1 now.

andyf’s picture

Tests of pharborist and drupalmoduleupgrader pass with 8.1 using the latest from the fork.

jcnventura’s picture

Status: Active » Needs review
jcnventura’s picture

Please do a pull request on upstream pharborist.

andyf’s picture

Thanks @jcnventura! I've made a PR - I had been hanging on, I haven't actually tested the changes by upgrading a module, I've only ensured the tests pass. I was thinking it might make sense to create a script that would use ddev to fire up a D9 site, install some modules and upgrade them, then do the same again with a D10 site, and compare the output... but I haven't found time yet.

andyf’s picture

I've also opened a ticket to get tests running on pharborist/pharborist, not sure if that issue queue will be checked or who might have the access to set something up.

jcnventura’s picture

Once you accept the invite, I guess you'll have the access for that :)

andyf’s picture

Neato, thanks (: I assume we're ok using GitHub actions, which will use up minutes from the pharborist org IIUC.

jcnventura’s picture

Yes, we don't use those for anything else, and I assume the free tier will be enough...

gábor hojtsy’s picture

Status: Needs review » Fixed

I merged the work from @andyF to Pharborist and then let Claude Code fix the remaining PHP 8 compatibility issues (there was not much). It even added further regression testing for the new tokenization constants. After more fixes to the tests https://www.drupal.org/project/drupalmoduleupgrader/releases/2.0.0-alpha2 is now out! Made in less than a day with Claude Code. I'll credit folks here that contributed a lot to this effort.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

gábor hojtsy’s picture

Status: Fixed » Closed (duplicate)