Skip to content

CascadingDirnameCalls ​

warning on by default quick-fix PHP ≥ 7.0

Group: Language level migration · PhpStorm name: CascadingDirnameCallsInspection

Since PHP 7.0 dirname() accepts a second "levels" argument, so a chain of nested dirname() calls can be collapsed into a single call with the summed level count: fewer calls, easier to read.

Example ​

php
<?php
$base = dirname(dirname(dirname($root)));
$app  = dirname(dirname(dirname(dirname(getcwd()))));
$var  = dirname(dirname($root, $up));
$sum  = dirname(dirname($root, 2), 3);
$two  = dirname(dirname($root, $a), $b);
$fq   = \dirname(\dirname($root));

// not reported
$one  = dirname($root);
$lvl  = dirname($root, 1);
$far  = dirname($root, 4);
$wrap = dirname(getcwd());
$real = dirname(realpath($root), 2);
$zero = dirname(dirname($root, 0));
$bad  = dirname(dirname());
$none = dirname();
php
<?php
$base = dirname($root, 3);
$app  = dirname(getcwd(), 4);
$var  = dirname($root, 1 + $up);
$sum  = dirname($root, 5);
$two  = dirname($root, 0 + $b + $a);
$fq   = dirname($root, 2);

// not reported
$one  = dirname($root);
$lvl  = dirname($root, 1);
$far  = dirname($root, 4);
$wrap = dirname(getcwd());
$real = dirname(realpath($root), 2);
$zero = dirname(dirname($root, 0));
$bad  = dirname(dirname());
$none = dirname();

Reported:

  • line 2: Collapse the nested dirname() calls into 'dirname($root, 3)'.
  • line 3: Collapse the nested dirname() calls into 'dirname(getcwd(), 4)'.
  • line 4: Collapse the nested dirname() calls into 'dirname($root, 1 + $up)'.
  • line 5: Collapse the nested dirname() calls into 'dirname($root, 5)'.
  • line 6: Collapse the nested dirname() calls into 'dirname($root, 0 + $b + $a)'.
  • line 7: Collapse the nested dirname() calls into 'dirname($root, 2)'.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore CascadingDirnameCalls

/**
 * @noinspection CascadingDirnameCallsInspection
 */

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