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

Fix additional places where base_dir was broken due to missing separator

Improves upon https://github.com/vimeo/psalm/pull/10542 and https://github.com/vimeo/psalm/pull/10628
This commit is contained in:
kkmuffme 2024-02-01 08:10:57 +01:00
parent 38d7d435c8
commit d4a5909e1f
4 changed files with 25 additions and 27 deletions

View File

@ -244,7 +244,7 @@ class Config
protected $extra_files;
/**
* The base directory of this config file
* The base directory of this config file without trailing slash
*
* @var string
*/
@ -1445,7 +1445,7 @@ class Config
if (!$file_path) {
throw new ConfigException(
'Cannot resolve stubfile path '
. rtrim($config->base_dir, DIRECTORY_SEPARATOR)
. $config->base_dir
. DIRECTORY_SEPARATOR
. $stub_file['name'],
);
@ -1582,11 +1582,11 @@ class Config
private function loadFileExtensions(SimpleXMLElement $extensions): void
{
foreach ($extensions as $extension) {
$extension_name = preg_replace('/^\.?/', '', (string)$extension['name'], 1);
$extension_name = preg_replace('/^\.?/', '', (string) $extension['name'], 1);
$this->file_extensions[] = $extension_name;
if (isset($extension['scanner'])) {
$path = $this->base_dir . (string)$extension['scanner'];
$path = $this->base_dir . DIRECTORY_SEPARATOR . (string) $extension['scanner'];
if (!file_exists($path)) {
throw new ConfigException('Error parsing config: cannot find file ' . $path);
@ -1596,7 +1596,7 @@ class Config
}
if (isset($extension['checker'])) {
$path = $this->base_dir . (string)$extension['checker'];
$path = $this->base_dir . DIRECTORY_SEPARATOR . (string) $extension['checker'];
if (!file_exists($path)) {
throw new ConfigException('Error parsing config: cannot find file ' . $path);
@ -1817,7 +1817,7 @@ class Config
public function shortenFileName(string $to): string
{
if (!is_file($to)) {
return preg_replace('/^' . preg_quote($this->base_dir, '/') . '/', '', $to, 1);
return preg_replace('/^' . preg_quote($this->base_dir . DIRECTORY_SEPARATOR, '/') . '?/', '', $to, 1);
}
$from = $this->base_dir;

View File

@ -92,7 +92,7 @@ class CacheTest extends TestCase
foreach ($interactions as $interaction) {
foreach ($interaction['files'] as $file_path => $file_contents) {
$file_path = $config->base_dir . str_replace('/', DIRECTORY_SEPARATOR, $file_path);
$file_path = $config->base_dir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file_path);
if ($file_contents === null) {
$file_provider->deleteFile($file_path);
} else {
@ -126,7 +126,7 @@ class CacheTest extends TestCase
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A {
public function do(B $b): void
@ -135,7 +135,7 @@ class CacheTest extends TestCase
}
}
PHP,
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
public function do(): void
@ -148,10 +148,10 @@ class CacheTest extends TestCase
],
[
'files' => [
'/src/B.php' => null,
'src/B.php' => null,
],
'issues' => [
'/src/A.php' => [
'src/A.php' => [
'UndefinedClass: Class, interface or enum named B does not exist',
],
],
@ -163,7 +163,7 @@ class CacheTest extends TestCase
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A {
public function foo(B $b): int
@ -172,7 +172,7 @@ class CacheTest extends TestCase
}
}
PHP,
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
public ?int $value = 0;
@ -180,7 +180,7 @@ class CacheTest extends TestCase
PHP,
],
'issues' => [
'/src/A.php' => [
'src/A.php' => [
"NullableReturnStatement: The declared return type 'int' for A::foo is not nullable, but the function returns 'int|null'",
"InvalidNullableReturnType: The declared return type 'int' for A::foo is not nullable, but 'int|null' contains null",
],
@ -188,7 +188,7 @@ class CacheTest extends TestCase
],
[
'files' => [
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
public int $value = 0;
@ -204,7 +204,7 @@ class CacheTest extends TestCase
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
/**
@ -219,7 +219,7 @@ class CacheTest extends TestCase
}
}
PHP,
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
@ -234,7 +234,7 @@ class CacheTest extends TestCase
],
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
/**
@ -251,10 +251,10 @@ class CacheTest extends TestCase
PHP,
],
'issues' => [
'/src/A.php' => [
'src/A.php' => [
"UndefinedDocblockClass: Docblock-defined class, interface or enum named T does not exist",
],
'/src/B.php' => [
'src/B.php' => [
"InvalidArgument: Argument 1 of A::foo expects T, but 1 provided",
],
],
@ -266,7 +266,7 @@ class CacheTest extends TestCase
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A {
public function __construct(private string $foo)
@ -283,7 +283,7 @@ class CacheTest extends TestCase
],
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A
{
@ -298,7 +298,7 @@ class CacheTest extends TestCase
PHP,
],
'issues' => [
'/src/A.php' => [
'src/A.php' => [
"UndefinedThisPropertyFetch: Instance property A::\$foo is not defined",
"MixedReturnStatement: Could not infer a return type",
"MixedInferredReturnType: Could not verify return type 'string' for A::bar",

View File

@ -864,7 +864,7 @@ class StubTest extends TestCase
public function testNoStubFunction(): void
{
$this->expectExceptionMessage('UndefinedFunction - /src/somefile.php:2:22 - Function barBar does not exist');
$this->expectExceptionMessage('UndefinedFunction');
$this->expectException(CodeException::class);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(

View File

@ -9,8 +9,6 @@ use SimpleXMLElement;
use function getcwd;
use const DIRECTORY_SEPARATOR;
class TestConfig extends Config
{
private static ?ProjectFileFilter $cached_project_files = null;
@ -28,7 +26,7 @@ class TestConfig extends Config
$this->level = 1;
$this->cache_directory = null;
$this->base_dir = getcwd() . DIRECTORY_SEPARATOR;
$this->base_dir = getcwd();
if (!self::$cached_project_files) {
self::$cached_project_files = ProjectFileFilter::loadFromXMLElement(