1.2.1355. Use pathinfo() Arguments

pathinfo() has a second argument to select only useful data.

It is twice faster to get only one element from pathinfo() than get the four of them, and use only one.

This analysis reports pathinfo() usage, without second argument, where only one or two indices are used, after the call. Depending on the situation, the functions dirname() and basename() may also be used. They are even faster, when only fetching those data.

<?php

// This could use only PATHINFO_BASENAME
function foo_db() {
    $a = pathinfo($file2);
    return $a['basename'];
}

// This could be 2 calls, with PATHINFO_BASENAME and PATHINFO_DIRNAME.
function foo_de() {
    $a = pathinfo($file3);
    return $a['dirname'].'/'.$a['basename'];
}

// This is OK : 3 calls to pathinfo() is slower than array access.
function foo_deb() {
    $a = pathinfo($file4);
    return  $a['dirname'].'/'.$a['filename'].'.'.$a['extension'];
}

?>

See also list.

1.2.1355.1. Suggestions

  • Use PHP native function pathinfo() and its arguments

1.2.1355.2. Specs

Short name

Php/UsePathinfoArgs

Rulesets

All, Performances

Exakat since

0.12.12

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

path

Examples

Zend-Config, ThinkPHP

Available in

Entreprise Edition, Exakat Cloud