From f9aa2ebf443ea29a28afeba17a1e023efaf6ee82 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sun, 9 Feb 2025 22:57:51 +0100 Subject: [PATCH 1/2] Allow Psalm 6 and fix newly detected errors --- composer.json | 2 +- src/Module.php | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index f57e901..75cb82e 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require-dev": { "squizlabs/php_codesniffer": "^3.4", - "vimeo/psalm": "^4.17.0 || dev-master || ^5.0.0 || dev-master", + "vimeo/psalm": "^4.17.0 || dev-master || ^5.0.0 || ^6.0.0 || dev-master", "phpunit/phpunit": "^9.5.20", "codeception/codeception": "^4.1.31 || ^5.0.0-rc3" }, diff --git a/src/Module.php b/src/Module.php index ef281af..93f5969 100644 --- a/src/Module.php +++ b/src/Module.php @@ -28,6 +28,7 @@ use function is_numeric; use function is_string; +/** @api */ class Module extends BaseModule { /** @var array */ @@ -144,6 +145,10 @@ public function runPsalmOn(string $filename, array $options = []): void public function runPsalmIn(string $dir, array $options = []): void { $pwd = getcwd(); + if (false === $pwd) { + throw new TestRuntimeException('Failed to get current working directory'); + } + $this->fs()->amInPath($dir); $config = $this->psalmConfig ?: self::DEFAULT_PSALM_CONFIG; @@ -197,7 +202,11 @@ public function seeThisError(string $type, string $message): void private function matches(string $expected, string $actual): bool { $regexpDelimiter = '/'; - if ($expected[0] === $regexpDelimiter && $expected[strlen($expected) - 1] === $regexpDelimiter) { + if ( + $expected !== '' + && $expected[0] === $regexpDelimiter + && $expected[strlen($expected) - 1] === $regexpDelimiter + ) { $regexp = $expected; } else { $regexp = $this->convertToRegexp($expected); @@ -319,6 +328,10 @@ public function runPsalmWithTaintAnalysis(): void public function runPsalmOnASingleFile(string $file): void { $pwd = getcwd(); + if (false === $pwd) { + throw new TestRuntimeException('Failed to get current working directory'); + } + $this->fs()->amInPath($this->getDefaultDirectory()); $config = $this->psalmConfig ?: self::DEFAULT_PSALM_CONFIG; @@ -473,6 +486,7 @@ public function haveADependencySatisfied(string $package, string $versionConstra throw new SkippedTestError("This scenario requires $package to match $versionConstraint"); } + /** @return non-empty-string */ private function convertToRegexp(string $in): string { return '@' . str_replace('%', '.*', preg_quote($in, '@')) . '@'; @@ -552,7 +566,7 @@ private function parseErrors(): void return; } - if (empty($this->output)) { + if ($this->output === null || $this->output === '') { $this->errors = []; return; } From c7c12939ca9c239087d11f653ac9ed9ad04ac7c7 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sun, 9 Feb 2025 23:04:15 +0100 Subject: [PATCH 2/2] Adjust test to account for Psalm changes - param names are reported without `$` - dead code detection is enabled by default --- tests/acceptance/PsalmModule.feature | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/acceptance/PsalmModule.feature b/tests/acceptance/PsalmModule.feature index bfd43d6..f1371cf 100644 --- a/tests/acceptance/PsalmModule.feature +++ b/tests/acceptance/PsalmModule.feature @@ -78,9 +78,9 @@ Feature: Psalm module """ When I run Psalm with dead code detection Then I see these errors - | Type | Message | - | UnusedParam | Param $p is never referenced in this method | - | UnusedClass | Class CD is never used | + | Type | Message | + | UnusedParam | /Param \$?p is never referenced in this method/ | + | UnusedClass | Class CD is never used | And I see no other errors Scenario: Running Psalm with custom config @@ -174,6 +174,7 @@ Feature: Psalm module Scenario: Using regexps to match error messages Given I have the following code """ + /** @api */ class CCC extends PPP {} """ When I run Psalm @@ -185,6 +186,7 @@ Feature: Psalm module Scenario: Escaping pipes in regexps Given I have the following code """ + /** @api */ class CC extends PPP {} """ When I run Psalm @@ -196,6 +198,7 @@ Feature: Psalm module Scenario: Using backslashes in regexps Given I have the following code """ + /** @api */ class C extends PPP {} """ When I run Psalm