In the Localization Client module:

function l10n_client_save_string() {
  global $user, $language;

gets converted to:

function l10n_client_save_string() {
  $user = \Drupal::currentUser(), $language;

...which is a parse error.

CommentFileSizeAuthor
#3 dmu-fix-globals-grep.patch752 byteswebchick

Comments

webchick’s picture

Assigned: Unassigned » webchick
Issue tags: +Acquia Feb 2015 Hackathon

Taking a look at this one first.

webchick’s picture

Ok, so why this is happening is 'user' is a grep target. From drupalmoduleupgrader.grep.yml:

globals:
  language: '\Drupal::languageManager()->getCurrentLanguage()'
  theme: '\Drupal::theme()->getActiveTheme()->getName()'
  theme_key: '\Drupal::theme()->getActiveTheme()->getName()'
  user: '\Drupal::currentUser()'

So far, that's basically fine. There's even one for language there, too.

These variables get converted in the grep converter like so:

    foreach ($configuration['globals'] as $variable => $replacement) {
      $this->targets['global $' . $variable] = '$' . $variable . ' = ' . $replacement;
      $this->targets['$GLOBALS[\'' . $variable . '\']'] = $replacement;
      $this->targets['$GLOBALS["' . $variable . '"]'] = $replacement;
    }

The "money" line is:

      $this->targets['global $' . $variable] = '$' . $variable . ' = ' . $replacement;

That converts "global $user" to "$user = \Drupal::currentUser()"

And the reason $language doesn't get converted is because the "global" is at the beginning of the line, not just before $language (this is a common pattern, including in core, so l10n_client isn't doing anything weird here).

Looks like we need to both be more explicit in that line to only do that conversion if the line ends in a semi-colon, and in the event that there are multiple globals on the same line, loop through them all and do conversions each on one line.

webchick’s picture

Status: Active » Needs review
StatusFileSize
new752 bytes

Ok, this much at least stops the parse errors from happening. But it doesn't deal with global $foo, $bar; since it skips lines like that. I am actually not immediately sure how to deal with that since I don't think Grep.php handles regular expressions.

  • webchick committed 9d5c769 on 8.x-1.x
    Issue #2425483: Fix parse errors caused by multiple global variable...
webchick’s picture

Title: global $user conversion fails when it's part of multiple global variable declarations » Fix parse errors caused by multiple global variable declarations on the same line.
Status: Needs review » Fixed

Indeed:

      $search = array_keys($this->targets);
      $replace = array_values($this->targets);
      file_put_contents($file->getPathname(), str_replace($search, $replace, $file->getContents()));

So I think this is as good as we can do for now; basically skip those lines that will result in parse errors. Filed #2425541: Convert multiple global variable declarations on one line as a follow-up to make this work For Real™.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.