1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Do all normalisation on / before conversion to \

This commit is contained in:
Matthew Brown 2018-11-18 17:18:05 -05:00
parent fa2e7f1f70
commit 0f90309819
2 changed files with 28 additions and 23 deletions

View File

@ -65,20 +65,7 @@ class IncludeAnalyzer
}
if ($path_to_file) {
$path_to_file = preg_replace('/\/[\/]+/', '/', $path_to_file);
$path_to_file = str_replace('/./', '/', $path_to_file);
$slash = preg_quote(DIRECTORY_SEPARATOR, '/');
$reduce_pattern = '/' . $slash . '[^' . $slash . ']+' . $slash . '\.\.' . $slash . '/';
while (preg_match($reduce_pattern, $path_to_file)) {
$path_to_file = preg_replace($reduce_pattern, DIRECTORY_SEPARATOR, $path_to_file);
}
$path_to_file = str_replace(
DIRECTORY_SEPARATOR . '.' . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
$path_to_file
);
$path_to_file = self::normalizeFilePath($path_to_file);
// if the file is already included, we can't check much more
if (in_array(realpath($path_to_file), get_included_files(), true)) {
@ -309,4 +296,30 @@ class IncludeAnalyzer
return null;
}
public static function normalizeFilePath(string $path_to_file) : string
{
// replace all \ with / for normalization
$path_to_file = str_replace('\\', '/', $path_to_file);
$path_to_file = str_replace('/./', '/', $path_to_file);
// first remove unnecessary / duplicates
$path_to_file = preg_replace('/\/[\/]+/', '/', $path_to_file);
$path_to_file = preg_replace('/\/[\/]+/', '/', $path_to_file);
$reduce_pattern = '/\/[^\/]+\/\.\.\//';
while (preg_match($reduce_pattern, $path_to_file)) {
$path_to_file = preg_replace($reduce_pattern, DIRECTORY_SEPARATOR, $path_to_file);
}
$path_to_file = str_replace('/./', '/', $path_to_file);
if (DIRECTORY_SEPARATOR !== '/') {
$path_to_file = str_replace('/', DIRECTORY_SEPARATOR, $path_to_file);
}
return $path_to_file;
}
}

View File

@ -1908,15 +1908,7 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
}
if ($path_to_file) {
$slash = preg_quote(DIRECTORY_SEPARATOR, '/');
$reduce_pattern = '/' . $slash . '[^' . $slash . ']+' . $slash . '\.\.' . $slash . '/';
while (preg_match($reduce_pattern, $path_to_file)) {
$path_to_file = preg_replace($reduce_pattern, DIRECTORY_SEPARATOR, $path_to_file);
}
$path_to_file = preg_replace('/\/[\/]+/', DIRECTORY_SEPARATOR, $path_to_file);
$path_to_file = str_replace('/./', DIRECTORY_SEPARATOR, $path_to_file);
$path_to_file = IncludeAnalyzer::normalizeFilePath($path_to_file);
if ($this->file_path === $path_to_file) {
return;