1.2.969. Pathinfo() Returns May Vary¶
pathinfo() function returns an array whose content may vary. It is recommended to collect the values after check, rather than directly. The same applies to parse_url(), which returns an array with various index.
<?php
$file = '/a/b/.c';
//$extension may be missing, leading to empty $filename and filename in $extension
list( $dirname, $basename, $extension, $filename ) = array_values( pathinfo($file) );
//Use PHP 7.1 list() syntax to assign correctly the values, and skip array_values()
//This emits a warning in case of missing index
['dirname' => $dirname,
'basename' => $basename,
'extension' => $extension,
'filename' => $filename ] = pathinfo($file);
//This works without warning
$details = pathinfo($file);
$dirname = $details['dirname'] ?? getpwd();
$basename = $details['basename'] ?? '';
$extension = $details['extension'] ?? '';
$filename = $details['filename'] ?? '';
?>
1.2.969.1. Connex PHP features¶
1.2.969.1.1. Suggestions¶
Add a check on the return value of pathinfo() before using it.
1.2.969.1.2. Specs¶
Short name |
Php/PathinfoReturns |
Rulesets |
|
Exakat since |
0.12.11 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Examples |
|
Available in |