Skip to content

DirectoryConstantCanBeUsed ​

warning on by default quick-fix

Group: Language level migration · PhpStorm name: DirectoryConstantCanBeUsedInspection

dirname(__FILE__) computes at runtime what the magic constant __DIR__ (PHP 5.3+) already provides. Use the constant.

Example ​

php
<?php
function bootstrap()
{
    $root = dirname(__FILE__);
    require \dirname(__FILE__) . '/config.php';

    $up    = dirname(__FILE__, 2);
    $other = dirname(__FILE__ . '/../lib');
    $paren = dirname((__FILE__));
    $none  = dirname();
    $dyn   = $resolver->dirname(__FILE__);
}
php
<?php
function bootstrap()
{
    $root = __DIR__;
    require __DIR__ . '/config.php';

    $up    = dirname(__FILE__, 2);
    $other = dirname(__FILE__ . '/../lib');
    $paren = dirname((__FILE__));
    $none  = dirname();
    $dyn   = $resolver->dirname(__FILE__);
}

Reported:

  • line 4: Replace dirname(__FILE__) with __DIR__.
  • line 5: Replace dirname(__FILE__) with __DIR__.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore DirectoryConstantCanBeUsed

/**
 * @noinspection DirectoryConstantCanBeUsedInspection
 */

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