Skip to content

Commit

Permalink
Fix additional places where base_dir was broken due to missing separator
Browse files Browse the repository at this point in the history
Improves upon vimeo#10542 and vimeo#10628
  • Loading branch information
kkmuffme committed Feb 1, 2024
1 parent 38d7d43 commit d4a5909
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -1445,7 +1445,7 @@ private static function fromXmlAndPaths(
if (!$file_path) {
throw new ConfigException(
'Cannot resolve stubfile path '
. rtrim($config->base_dir, DIRECTORY_SEPARATOR)
. $config->base_dir
. DIRECTORY_SEPARATOR
. $stub_file['name'],
);
Expand Down Expand Up @@ -1582,11 +1582,11 @@ public function safeSetCustomErrorLevel(string $issue_key, string $error_level):
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);
Expand All @@ -1596,7 +1596,7 @@ private function loadFileExtensions(SimpleXMLElement $extensions): void
}

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);
Expand Down Expand Up @@ -1817,7 +1817,7 @@ private function getPluginClassForPath(Codebase $codebase, string $path, string
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;
Expand Down
34 changes: 17 additions & 17 deletions tests/Cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testCacheInteractions(

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 {
Expand Down Expand Up @@ -126,7 +126,7 @@ public static function provideCacheInteractions(): iterable
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A {
public function do(B $b): void
Expand All @@ -135,7 +135,7 @@ public function do(B $b): void
}
}
PHP,
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
public function do(): void
Expand All @@ -148,10 +148,10 @@ public function do(): void
],
[
'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',
],
],
Expand All @@ -163,7 +163,7 @@ public function do(): void
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A {
public function foo(B $b): int
Expand All @@ -172,23 +172,23 @@ public function foo(B $b): int
}
}
PHP,
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
public ?int $value = 0;
}
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",
],
],
],
[
'files' => [
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
public int $value = 0;
Expand All @@ -204,7 +204,7 @@ class B {
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
/**
Expand All @@ -219,7 +219,7 @@ public function foo($baz): void
}
}
PHP,
'/src/B.php' => <<<'PHP'
'src/B.php' => <<<'PHP'
<?php
class B {
Expand All @@ -234,7 +234,7 @@ public function foo(): void
],
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
/**
Expand All @@ -251,10 +251,10 @@ public function foo($baz): void
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",
],
],
Expand All @@ -266,7 +266,7 @@ public function foo($baz): void
[
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A {
public function __construct(private string $foo)
Expand All @@ -283,7 +283,7 @@ public function bar(): string
],
[
'files' => [
'/src/A.php' => <<<'PHP'
'src/A.php' => <<<'PHP'
<?php
class A
{
Expand All @@ -298,7 +298,7 @@ public function bar(): string
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",
Expand Down
2 changes: 1 addition & 1 deletion tests/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ function_exists("fooBar");

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(
Expand Down
4 changes: 1 addition & 3 deletions tests/TestConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

use function getcwd;

use const DIRECTORY_SEPARATOR;

class TestConfig extends Config
{
private static ?ProjectFileFilter $cached_project_files = null;
Expand All @@ -28,7 +26,7 @@ public function __construct()
$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(
Expand Down

0 comments on commit d4a5909

Please sign in to comment.