From 73a614b6fb318c141fe1e528a334ee1c1aef194e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 21 Oct 2025 07:10:36 +0200 Subject: [PATCH] Fix sprintf() types and placeholders --- src/Cache/Cache.php | 4 ++++ src/Cache/CacheStorage.php | 4 ++++ src/Cache/FileCacheStorage.php | 2 ++ src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php | 4 ++-- src/Command/ErrorFormatter/CheckstyleErrorFormatter.php | 2 +- src/Command/ErrorFormatter/RawErrorFormatter.php | 2 +- src/Command/ErrorFormatter/TableErrorFormatter.php | 6 +++--- src/Command/FixerWorkerCommand.php | 2 +- src/Command/WorkerCommand.php | 2 +- src/Reflection/Php/PhpMethodReflection.php | 9 ++++++++- src/Testing/PHPStanTestCase.php | 2 +- src/Testing/RuleTestCase.php | 2 +- 12 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index 8f4deb6f13..9b888cf67a 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -17,6 +17,8 @@ public function __construct( } /** + * @param non-empty-string $key + * * @return mixed|null */ public function load(string $key, string $variableKey) @@ -25,6 +27,8 @@ public function load(string $key, string $variableKey) } /** + * @param non-empty-string $key + * * @param mixed $data */ public function save(string $key, string $variableKey, $data): void diff --git a/src/Cache/CacheStorage.php b/src/Cache/CacheStorage.php index c3a645eb2b..4ac0a3dd87 100644 --- a/src/Cache/CacheStorage.php +++ b/src/Cache/CacheStorage.php @@ -6,11 +6,15 @@ interface CacheStorage { /** + * @param non-empty-string $key + * * @return mixed|null */ public function load(string $key, string $variableKey); /** + * @param non-empty-string $key + * * @param mixed $data */ public function save(string $key, string $variableKey, $data): void; diff --git a/src/Cache/FileCacheStorage.php b/src/Cache/FileCacheStorage.php index 1b66f26e2a..efe681a95f 100644 --- a/src/Cache/FileCacheStorage.php +++ b/src/Cache/FileCacheStorage.php @@ -101,6 +101,8 @@ public function save(string $key, string $variableKey, $data): void } /** + * @param non-empty-string $key + * * @return array{string, string, string} */ private function getFilePaths(string $key): array diff --git a/src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php b/src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php index 892a3e1eb2..ac4c1fc81f 100644 --- a/src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php +++ b/src/Command/ErrorFormatter/BaselinePhpErrorFormatter.php @@ -87,7 +87,7 @@ public function formatErrors( if (count($identifiers) > 0) { foreach ($identifiers as $identifier => $identifierCount) { $php .= sprintf( - "\$ignoreErrors[] = [\n\t%s => %s,\n\t'identifier' => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n", + "\$ignoreErrors[] = [\n\t%s => %s,\n\t'identifier' => %s,\n\t'count' => %s,\n\t'path' => __DIR__ . %s,\n];\n", var_export($messageKey, true), var_export(Helpers::escape($message), true), var_export(Helpers::escape($identifier), true), @@ -97,7 +97,7 @@ public function formatErrors( } } else { $php .= sprintf( - "\$ignoreErrors[] = [\n\t%s => %s,\n\t'count' => %d,\n\t'path' => __DIR__ . %s,\n];\n", + "\$ignoreErrors[] = [\n\t%s => %s,\n\t'count' => %s,\n\t'path' => __DIR__ . %s,\n];\n", var_export($messageKey, true), var_export(Helpers::escape($message), true), var_export($totalCount, true), diff --git a/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php b/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php index 8120dea1ef..6a8c27b122 100644 --- a/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php +++ b/src/Command/ErrorFormatter/CheckstyleErrorFormatter.php @@ -44,7 +44,7 @@ public function formatErrors( foreach ($errors as $error) { $output->writeRaw(sprintf( - ' ', + ' ', $this->escape((string) $error->getLine()), $this->escape($error->getMessage()), $error->getIdentifier() !== null ? sprintf(' source="%s"', $this->escape($error->getIdentifier())) : '', diff --git a/src/Command/ErrorFormatter/RawErrorFormatter.php b/src/Command/ErrorFormatter/RawErrorFormatter.php index a37dc42129..efd770468d 100644 --- a/src/Command/ErrorFormatter/RawErrorFormatter.php +++ b/src/Command/ErrorFormatter/RawErrorFormatter.php @@ -30,7 +30,7 @@ public function formatErrors( $output->writeRaw( sprintf( - '%s:%d:%s%s', + '%s:%s:%s%s', $fileSpecificError->getFile(), $fileSpecificError->getLine() ?? '?', $fileSpecificError->getMessage(), diff --git a/src/Command/ErrorFormatter/TableErrorFormatter.php b/src/Command/ErrorFormatter/TableErrorFormatter.php index bf79a7dc5f..cbea7e6bfa 100644 --- a/src/Command/ErrorFormatter/TableErrorFormatter.php +++ b/src/Command/ErrorFormatter/TableErrorFormatter.php @@ -64,8 +64,8 @@ public function formatErrors( $output->writeLineFormatted('💡 Tip of the Day:'); $output->writeLineFormatted(sprintf( "PHPStan is performing only the most basic checks.\nYou can pass a higher rule level through the --%s option\n(the default and current level is %d) to analyse code more thoroughly.", - AnalyseCommand::OPTION_LEVEL, - AnalyseCommand::DEFAULT_LEVEL, + (int) AnalyseCommand::OPTION_LEVEL, + (int) AnalyseCommand::DEFAULT_LEVEL, )); $output->writeLineFormatted(''); } @@ -111,7 +111,7 @@ public function formatErrors( if (getenv('TERMINAL_EMULATOR') === 'JetBrains-JediTerm') { $title = $this->relativePathHelper->getRelativePath($filePath); - $message .= sprintf("\nat %s:%d", $title, $error->getLine()); + $message .= sprintf("\nat %s:%d", $title, $error->getLine() ?? 0); } elseif (is_string($this->editorUrl)) { $url = str_replace( diff --git a/src/Command/FixerWorkerCommand.php b/src/Command/FixerWorkerCommand.php index aedb4bf858..fbb811917d 100644 --- a/src/Command/FixerWorkerCommand.php +++ b/src/Command/FixerWorkerCommand.php @@ -130,7 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $loop = new StreamSelectLoop(); $tcpConnector = new TcpConnector($loop); - $tcpConnector->connect(sprintf('127.0.0.1:%d', $serverPort))->then(function (ConnectionInterface $connection) use ($container, $inceptionResult, $configuration, $input, $ignoredErrorHelperResult, $loop): void { + $tcpConnector->connect(sprintf('127.0.0.1:%d', (int) $serverPort))->then(function (ConnectionInterface $connection) use ($container, $inceptionResult, $configuration, $input, $ignoredErrorHelperResult, $loop): void { // phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly $jsonInvalidUtf8Ignore = defined('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0; // phpcs:enable diff --git a/src/Command/WorkerCommand.php b/src/Command/WorkerCommand.php index dfef585109..902c9055a2 100644 --- a/src/Command/WorkerCommand.php +++ b/src/Command/WorkerCommand.php @@ -142,7 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $analysedFiles = array_fill_keys($analysedFiles, true); $tcpConnector = new TcpConnector($loop); - $tcpConnector->connect(sprintf('127.0.0.1:%d', $port))->then(function (ConnectionInterface $connection) use ($container, $identifier, $output, $analysedFiles, $tmpFile, $insteadOfFile): void { + $tcpConnector->connect(sprintf('127.0.0.1:%d', (int) $port))->then(function (ConnectionInterface $connection) use ($container, $identifier, $output, $analysedFiles, $tmpFile, $insteadOfFile): void { // phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly $jsonInvalidUtf8Ignore = defined('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0; // phpcs:enable diff --git a/src/Reflection/Php/PhpMethodReflection.php b/src/Reflection/Php/PhpMethodReflection.php index 3c4aafdd60..c6e5140cfb 100644 --- a/src/Reflection/Php/PhpMethodReflection.php +++ b/src/Reflection/Php/PhpMethodReflection.php @@ -255,7 +255,14 @@ private function isVariadic(): bool $className = $declaringClass->getName(); if ($declaringClass->isAnonymous()) { - $className = sprintf('%s:%s:%s', VariadicMethodsVisitor::ANONYMOUS_CLASS_PREFIX, $declaringClass->getNativeReflection()->getStartLine(), $declaringClass->getNativeReflection()->getEndLine()); + $startLine = $declaringClass->getNativeReflection()->getStartLine(); + $endLine = $declaringClass->getNativeReflection()->getEndLine(); + + if ($endLine === false) { + $endLine = 0; + } + + $className = sprintf('%s:%s:%s', VariadicMethodsVisitor::ANONYMOUS_CLASS_PREFIX, $startLine, $endLine); } if (array_key_exists($className, VariadicMethodsVisitor::$cache)) { if (array_key_exists($this->reflection->getName(), VariadicMethodsVisitor::$cache[$className])) { diff --git a/src/Testing/PHPStanTestCase.php b/src/Testing/PHPStanTestCase.php index 5749b34e44..5cdd02383d 100644 --- a/src/Testing/PHPStanTestCase.php +++ b/src/Testing/PHPStanTestCase.php @@ -219,7 +219,7 @@ protected function assertNoErrors(array $errors): void $messages = []; foreach ($errors as $error) { if ($error instanceof Error) { - $messages[] = sprintf("- %s\n in %s on line %d\n", rtrim($error->getMessage(), '.'), $error->getFile(), $error->getLine()); + $messages[] = sprintf("- %s\n in %s on line %d\n", rtrim($error->getMessage(), '.'), $error->getFile(), $error->getLine() ?? 0); } else { $messages[] = $error; } diff --git a/src/Testing/RuleTestCase.php b/src/Testing/RuleTestCase.php index 47c8a997e9..6aebd5e904 100644 --- a/src/Testing/RuleTestCase.php +++ b/src/Testing/RuleTestCase.php @@ -258,7 +258,7 @@ private function gatherAnalyserErrorsWithDelayedErrors(array $files): array if ($this->shouldFailOnPhpErrors() && count($analyserResult->getAllPhpErrors()) > 0) { $this->fail(implode("\n", array_map( - static fn (Error $error): string => sprintf('%s on %s:%d', $error->getMessage(), $error->getFile(), $error->getLine()), + static fn (Error $error): string => sprintf('%s on %s:%d', $error->getMessage(), $error->getFile(), $error->getLine() ?? 0), $analyserResult->getAllPhpErrors(), ))); }