1.2.948. PHP7 Dirname

dirname() has a second argument that represents the number of parent folder to follow. This prevent us from using nested dirname() calls to reach an grand-parent direct.

<?php
$path = '/a/b/c/d/e/f';

// PHP 7 syntax
$threeFoldersUp = dirname($path, 3);

// PHP 5 syntax
$threeFoldersUp = dirname(dirname(dirname($path)));

// long path, with backtracking
$path = __DIR__.'/../../../abc';

// short path, with direct access
$path = dirname(__DIR__, 3).'/abc';

?>

See also dirname.

1.2.948.1. Suggestions

  • Use dirname()’s second argument

1.2.948.2. Specs

Short name

Structures/PHP7Dirname

Rulesets

All, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56, Suggestions, php-cs-fixable

Exakat since

0.8.4

PHP Version

With PHP 7.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Examples

OpenConf, MediaWiki

Available in

Entreprise Edition, Exakat Cloud