From 42b439ba648bc96a8a9d46570f7699d28ed01685 Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Tue, 14 May 2024 11:22:30 +0200 Subject: [PATCH] Fixed PHPStan errors --- src/ComposerFile.php | 4 +++- src/Extensions/JanpechaActionsExtension.php | 12 ++++++++++-- src/Utils/Yaml.php | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/ComposerFile.php b/src/ComposerFile.php index e4e59f8..4143255 100644 --- a/src/ComposerFile.php +++ b/src/ComposerFile.php @@ -35,7 +35,9 @@ public function getPath(): string public function getType(): ?string { - return Arrays::get($this->data, 'type', NULL); + $type = Arrays::get($this->data, 'type', NULL); + assert(is_string($type) || is_null($type)); + return $type; } diff --git a/src/Extensions/JanpechaActionsExtension.php b/src/Extensions/JanpechaActionsExtension.php index 7665bf8..334a1e3 100644 --- a/src/Extensions/JanpechaActionsExtension.php +++ b/src/Extensions/JanpechaActionsExtension.php @@ -102,6 +102,7 @@ private function processBuildFile(CodeChecker\Engine $engine): void $created = FALSE; $oldContent = $engine->readFile(self::BuildPath); $yaml = Yaml::decode($oldContent); + assert(is_array($yaml)); } if (!isset($yaml['name'])) { @@ -129,6 +130,7 @@ private function processBuildFile(CodeChecker\Engine $engine): void $yaml = $this->createTestsJob($yaml, $engine); $yaml = $this->createCodingStyleJob($yaml, $engine); $yaml = $this->createStaticAnalysisJob($yaml, $engine); + assert(isset($yaml['jobs']) && is_array($yaml['jobs'])); if (count($yaml['jobs']) > 0) { $newContent = Yaml::encode($yaml); @@ -152,6 +154,8 @@ private function createCodingStyleJob(array $yaml, CodeChecker\Engine $engine): return $yaml; } + assert(isset($yaml['jobs']) && is_array($yaml['jobs'])); + if (!isset($yaml['jobs']['coding-style'])) { $engine->reportFixInFile("Created job 'coding-style'", self::BuildPath); $yaml['jobs']['coding-style'] = [ @@ -174,6 +178,8 @@ private function createCodingStyleJob(array $yaml, CodeChecker\Engine $engine): */ private function createStaticAnalysisJob(array $yaml, CodeChecker\Engine $engine): array { + assert(isset($yaml['jobs']) && is_array($yaml['jobs'])); + if (!isset($yaml['jobs']['static-analysis']) && ($phpstanConfigPath = $this->findPhpStanConfig($engine)) !== NULL) { $engine->reportFixInFile("Created job 'static-analysis'", self::BuildPath); $yaml['jobs']['static-analysis'] = [ @@ -217,6 +223,8 @@ private function createTestsJob(array $yaml, CodeChecker\Engine $engine): array return $yaml; } + assert(isset($yaml['jobs']) && is_array($yaml['jobs'])); + if (!isset($yaml['jobs']['tests'])) { $engine->reportFixInFile("Created job 'tests'", self::BuildPath); $yaml['jobs']['tests'] = [ @@ -238,7 +246,7 @@ private function createTestsJob(array $yaml, CodeChecker\Engine $engine): array $yaml['jobs']['tests']['with']['workingDirectory'] = $this->workingDirectory; } - if (count($yaml['jobs']['tests']['with']) === 0) { + if (count($yaml['jobs']['tests']['with']) === 0) { // @phpstan-ignore identical.alwaysFalse unset($yaml['jobs']['tests']['with']); } @@ -268,7 +276,7 @@ public static function configure(CodeChecker\CheckerConfig $config): void ); $config->addExtension(new self( - $config->getComposerFile()->getType(), + $config->getComposerFile()->getType() ?? 'library', $config->getPhpVersion(), $config->getMaxPhpVersion(), $config->getConfigFile() !== NULL, diff --git a/src/Utils/Yaml.php b/src/Utils/Yaml.php index e2fc0ae..7ee9708 100644 --- a/src/Utils/Yaml.php +++ b/src/Utils/Yaml.php @@ -45,7 +45,7 @@ private static function addEmptyLinesBetweenItems(string $result): string { $i = 0; - return preg_replace_callback('#^([\s]{4})?[a-zA-Z_\\\'-]+:#m', function ($match) use (&$i) { + return (string) preg_replace_callback('#^([\s]{4})?[a-zA-Z_\\\'-]+:#m', function ($match) use (&$i) { $i++; if ($i === 1) {