From b5ee6e0d8b2a94e1feca845e2c75d5870394eb49 Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Wed, 6 Dec 2023 11:20:18 -0600 Subject: [PATCH] Replace remaining POSIX only absolute path detection These were missed in #10441. Fixes "Could not resolve config path" error on Windows (#10418). --- src/Psalm/Config.php | 2 +- src/Psalm/Config/FileFilter.php | 2 +- .../Statements/Expression/IncludeAnalyzer.php | 17 +++-------------- .../PhpVisitor/Reflector/ExpressionScanner.php | 10 ++-------- 4 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index d8ba8e8f416..0cd65f165f4 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -1315,7 +1315,7 @@ private static function fromXmlAndPaths( } // we need an absolute path for checks - if ($path[0] !== '/' && DIRECTORY_SEPARATOR === '/') { + if (Path::isRelative($path)) { $prospective_path = $base_dir . DIRECTORY_SEPARATOR . $path; } else { $prospective_path = $path; diff --git a/src/Psalm/Config/FileFilter.php b/src/Psalm/Config/FileFilter.php index dd8fb31186a..8626deca4ee 100644 --- a/src/Psalm/Config/FileFilter.php +++ b/src/Psalm/Config/FileFilter.php @@ -247,7 +247,7 @@ public static function loadFromArray( foreach ($config['file'] as $file) { $file_path = (string) ($file['name'] ?? ''); - if ($file_path[0] === '/' && DIRECTORY_SEPARATOR === '/') { + if (Path::isAbsolute($file_path)) { /** @var non-empty-string */ $prospective_file_path = $file_path; } else { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php index 24db2b6d92e..6c4a36ab2bf 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php @@ -20,6 +20,7 @@ use Psalm\IssueBuffer; use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent; use Psalm\Type\TaintKind; +use Symfony\Component\Filesystem\Path; use function constant; use function defined; @@ -93,13 +94,7 @@ public static function analyze( $include_path = self::resolveIncludePath($path_to_file, dirname($statements_analyzer->getFilePath())); $path_to_file = $include_path ?: $path_to_file; - if (DIRECTORY_SEPARATOR === '/') { - $is_path_relative = $path_to_file[0] !== DIRECTORY_SEPARATOR; - } else { - $is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $path_to_file); - } - - if ($is_path_relative) { + if (Path::isRelative($path_to_file)) { $path_to_file = $config->base_dir . DIRECTORY_SEPARATOR . $path_to_file; } } else { @@ -285,13 +280,7 @@ public static function getPathTo( string $file_name, Config $config ): ?string { - if (DIRECTORY_SEPARATOR === '/') { - $is_path_relative = $file_name[0] !== DIRECTORY_SEPARATOR; - } else { - $is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $file_name); - } - - if ($is_path_relative) { + if (Path::isRelative($file_name)) { $file_name = $config->base_dir . DIRECTORY_SEPARATOR . $file_name; } diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionScanner.php index 6afd142bfa5..6e7887ff158 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionScanner.php @@ -20,13 +20,13 @@ use Psalm\Storage\FileStorage; use Psalm\Storage\FunctionLikeStorage; use Psalm\Type; +use Symfony\Component\Filesystem\Path; use function assert; use function defined; use function dirname; use function explode; use function in_array; -use function preg_match; use function strpos; use function strtolower; use function substr; @@ -316,13 +316,7 @@ public static function visitInclude( $include_path = IncludeAnalyzer::resolveIncludePath($path_to_file, dirname($file_storage->file_path)); $path_to_file = $include_path ?: $path_to_file; - if (DIRECTORY_SEPARATOR === '/') { - $is_path_relative = $path_to_file[0] !== DIRECTORY_SEPARATOR; - } else { - $is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $path_to_file); - } - - if ($is_path_relative) { + if (Path::isRelative($path_to_file)) { $path_to_file = $config->base_dir . DIRECTORY_SEPARATOR . $path_to_file; } } else {