Yesterday, in IRC I saw lots of frustration and gnashing of teeth over PHP and its handling of _toString() errors.

This made me feel a little guilty because, under the 'Probably bug' section of my lint tool I had seen pile of lint error in this regard and done nothing ... so here goes..

Method __toString is not implemented for the following objects.

\Drupal\Core\Database\Query\SelectInterface ( x4 )
#2344831: Document behavior of Drupal/Core/Database/Query/SelectInterface::__toString()
\Drupal\Core\Url
#2345089: lint error _toString() is not implemented in batch.inc
\Drupal\editor\EditorXssFilter\Standard
#2344823: Drupal\Tests\editor\Unit\EditorXssFilter\StandardTest has trivial lint error

No drama they seem simple to fix.

I have selected the database system as the component only because most of the errors come from there.

here is an example of the code which triggers errors.

Definition of Drupal\Core\Database\Driver\sqlite\Insert

the code in error is that $this->formQuery is converted into a string without an explicit definition in the \Drupal\Core\Database\Query\SelectInterface interface

  public function __toString() {
    // Create a sanitized comment string to prepend to the query.
    $comments = $this->connection->makeComment($this->comments);

    // Produce as many generic placeholders as necessary.
    $placeholders = array_fill(0, count($this->insertFields), '?');

    // If we're selecting from a SelectQuery, finish building the query and
    // pass it back, as any remaining options are irrelevant.
    if (!empty($this->fromQuery)) {
      $insert_fields_string = $this->insertFields ? ' (' . implode(', ', $this->insertFields) . ') ' : ' ';
      return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;  <-- triggers error
    }

    return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
  }

Comments

martin107’s picture

Assigned: Unassigned » martin107
Issue summary: View changes
martin107’s picture

Title: [Meta] Clear _toString is not implemented errors. » [Meta issue] Clear _toString is not implemented errors.
Issue summary: View changes

The lint error regarding \Drupal\editor\EditorXssFilter\Standard is trivial to fix.

martin107’s picture

martin107’s picture

The \Drupal\Core\Database\Query\SelectInterface issue is a API change / beta-target which is maybe too late to slip in :(

martin107’s picture

Assigned: martin107 » Unassigned
Issue summary: View changes

updating summary with url/batch.inc fix.

martin107’s picture

Status: Active » Closed (fixed)

Today the last sub issue was fixed. :)