Skip to content

RealpathInStreamContext ​

warning on by default quick-fix

Group: Probable bugs · PhpStorm name: RealpathInStreamContextInspection

realpath() returns false for paths inside stream wrappers such as phar://, so code that works on disk breaks once packaged. Climbing directories with realpath(__DIR__ . '/../') or wrapping include paths in realpath() should use dirname() / the plain path instead.

Example ​

php
<?php
$root = realpath(dirname(__DIR__, 1) . '/../var');
$cfg  = realpath($app . "/../../etc");
$odd  = realpath($app . '/lib' . '/..');
$rel  = realpath('../shared');
require realpath('/opt/app/boot.php');
include_once (realpath($entry));
$ok   = realpath($app . '/cache');
$two  = realpath('/tmp', 'x');
php
<?php
$root = dirname(dirname(__DIR__, 1)) . '/var';
$cfg  = dirname(dirname($app)) . "/etc";
$odd  = realpath($app . '/lib' . '/..');
$rel  = realpath('../shared');
require '/opt/app/boot.php';
include_once (realpath($entry));
$ok   = realpath($app . '/cache');
$two  = realpath('/tmp', 'x');

Reported:

  • line 2: Use 'dirname(dirname(__DIR__, 1)) . '/var'' instead: realpath() fails inside stream wrappers.
  • line 3: Use 'dirname(dirname($app)) . "/etc"' instead: realpath() fails inside stream wrappers.
  • line 4: realpath() fails inside stream wrappers such as phar://; prefer dirname().
  • line 5: realpath() fails inside stream wrappers such as phar://; prefer dirname().
  • line 6: Use ''/opt/app/boot.php'' instead: realpath() fails inside stream wrappers.
  • line 7: realpath() fails inside stream wrappers such as phar://; prefer dirname().

Configure ​

In custos.json:

json
{
  "rules": {
    "RealpathInStreamContext": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore RealpathInStreamContext

/**
 * @noinspection RealpathInStreamContextInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.