The method Drupal\Component\PhpStorage\FileStorage does not return anything but the documentation says it does:

  /**
   * Ensures the directory exists, has the right permissions, and a .htaccess.
   *   ... snip ...
   * @return bool
   *   TRUE if the directory exists or has been created, FALSE otherwise.
   */
  protected function ensureDirectory($directory, $mode = 0777) {
    if ($this->createDirectory($directory, $mode)) {
      $htaccess_path = $directory . '/.htaccess';
      if (!file_exists($htaccess_path) && file_put_contents($htaccess_path, static::htaccessLines())) {
        @chmod($htaccess_path, 0444);
      }
    }
  }

The return value of this method is used in the following code:

  public function save($name, $code) {
    $path = $this->getFullPath($name);
    $directory = dirname($path);
    if ($this->ensureDirectory($directory)) {
      $htaccess_path = $directory . '/.htaccess';
      if (!file_exists($htaccess_path) && file_put_contents($htaccess_path, static::htaccessLines())) {
        @chmod($htaccess_path, 0444);
      }
    }
    return (bool) file_put_contents($path, $code);
  }

As the results, the code inside of if () { } statement is unreachable.

Proposed resolution

Fix documentation and remove unreachable code.

Comments

jhodgdon’s picture

Title: Cleanup FileStorage::ensureDirectory » FileStorage::ensureDirectory has no return value, but save() uses it and docs say it does
Component: documentation » file system
Priority: Normal » Major

You're right! Wow, that seems like more than a Documentation bug, since the (missing) return value is (erroneously) being used in another function.

Moving to File subsystem for action, and since this an API function, and a Component even, and the save() function is really never going to work right, it may even be Major.

jhedstrom’s picture

Status: Active » Needs review
StatusFileSize
new2.48 KB

This also removes the duplicate code in the save() method (that was never called previously, but would now simply repeat what happens in ensureDirectory()).

Status: Needs review » Needs work

The last submitted patch, 2: 2505793-02.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

20th’s picture

Priority: Major » Normal
Issue tags: +Needs reroll, +Needs tests

8.3.x still tries to use nonexistent return value from Drupal\Component\PhpStorage\FileStorage::ensureDirectory. Patch need reroll.

Cannot agree, however, that this is a major bug because it existed for so long without causing any obvious troubles.

denutkarsh’s picture

Assigned: Unassigned » denutkarsh

@20th I am rerolling the patch for 8.3.x as it is persisting for it too.

denutkarsh’s picture

Status: Needs work » Needs review
StatusFileSize
new1.13 KB

Here is the patch i rerolled for drupal 8.3.x .

Status: Needs review » Needs work

The last submitted patch, 8: 2505793-8.patch, failed testing.

denutkarsh’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: 2505793-8.patch, failed testing.

denutkarsh’s picture

StatusFileSize
new1.17 KB
new529 bytes
denutkarsh’s picture

Status: Needs work » Needs review
20th’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs reroll, -Needs tests

This patch can be applied both to 8.2.x and 8.3.x branches, fixes documentation of ensureDirectory() method and eliminates unreachable code in save() method.

No functionality is lost because the removed code still exists inside of ensureDirectory() method, so the .htaccess file is still being written. Likewise, no functionality is added, so no new tests are required.

It might be good to add a test for the case when the ensureDirectory() actually fails to create directory. But then the file_put_contents() will fail as well and that will be caught by Drupal\Tests\Component\PhpStorage\FileStorageTest::testCRUD().

alexpott’s picture

Version: 8.2.x-dev » 8.3.x-dev
Category: Bug report » Task

Yes this is dead code. But there is no specific bug here. The what happens at run-time is just the same before and after this patch. Therefore this is a task.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed dad2a50 and pushed to 8.3.x. Thanks!

  • alexpott committed dad2a50 on 8.3.x
    Issue #2505793 by denutkarsh, jhedstrom, 20th: FileStorage::...

Status: Fixed » Closed (fixed)

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

denutkarsh’s picture