From 9f5d861281e33faa372afdecd8a06f159f219de5 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Thu, 16 Nov 2023 13:59:38 +0100 Subject: [PATCH] phpstan lvl 8 --- lib/php/Converter.php | 25 ++++++++++++++++++------- lib/php/FilesToJSON.php | 12 +++++++++++- phpstan.neon | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/php/Converter.php b/lib/php/Converter.php index d8cbf1f..e70b570 100644 --- a/lib/php/Converter.php +++ b/lib/php/Converter.php @@ -134,7 +134,7 @@ public function __construct($target_version = null) */ public function getTargetVersion(): float { - return $this->target_version; + return $this->target_version ?? self::LAST_VERSION; } /** @@ -167,7 +167,11 @@ public function isDebug(): bool */ public function getSchemaPath(): string { - return realpath(__DIR__ . '/../../inventory.schema.json'); + $schema_path = realpath(__DIR__ . '/../../inventory.schema.json'); + if ($schema_path === false) { + throw new RuntimeException('Schema file not found!'); + } + return $schema_path; } /** @@ -196,14 +200,18 @@ public function setExtraSubProperties(array $properties): self */ public function buildSchema() { - $schema = json_decode(file_get_contents($this->getSchemaPath())); + $string = file_get_contents($this->getSchemaPath()); + if ($string === false) { + throw new RuntimeException('Unable to read schema file'); + } + $schema = json_decode($string); $properties = $schema->properties->content->properties; if ($this->extra_properties != null) { foreach ($this->extra_properties as $extra_property => $extra_config) { if (!property_exists($properties, $extra_property)) { - $properties->$extra_property = json_decode(json_encode($extra_config)); + $properties->$extra_property = json_decode((string)json_encode($extra_config)); } else { trigger_error( sprintf('Property %1$s already exists in schema.', $extra_property), @@ -222,7 +230,7 @@ public function buildSchema() case 'array': if (!property_exists($properties->$extra_sub_property->items->properties, $subprop)) { $properties->$extra_sub_property->items->properties->$subprop = - json_decode(json_encode($subconfig)); + json_decode((string)json_encode($subconfig)); } else { trigger_error( sprintf('Property %1$s already exists in schema.', $subprop), @@ -233,7 +241,7 @@ public function buildSchema() case 'object': if (!property_exists($properties->$extra_sub_property->properties, $subprop)) { $properties->$extra_sub_property->properties->$subprop = - json_decode(json_encode($subconfig)); + json_decode((string)json_encode($subconfig)); } else { trigger_error( sprintf( @@ -312,7 +320,7 @@ public function convert(string $xml) } //convert SimpleXML object to array, recursively. $data = json_decode( - json_encode((array)$sxml), + (string)json_encode((array)$sxml), true ); $this->loadSchemaPatterns(); @@ -1228,6 +1236,9 @@ public function convertDate(string $value, string $format = 'Y-m-d'): ?string public function loadSchemaPatterns(): void { $string = file_get_contents($this->getSchemaPath()); + if ($string === false) { + throw new RuntimeException('Unable to read schema file'); + } $json = json_decode($string, true); $this->schema_patterns['networks_types'] = explode( diff --git a/lib/php/FilesToJSON.php b/lib/php/FilesToJSON.php index 91ce0e3..e99ffc9 100644 --- a/lib/php/FilesToJSON.php +++ b/lib/php/FilesToJSON.php @@ -345,6 +345,15 @@ private function callCurl(string $url): string { $ch = curl_init($url); + if ($ch === false) { + throw new RuntimeException( + sprintf( + 'Unable to initialize curl for %s', + $url + ) + ); + } + $opts = [ CURLOPT_URL => $url, CURLOPT_USERAGENT => "GLPI/Inventory format 1.0", @@ -378,6 +387,7 @@ private function callCurl(string $url): string throw new RuntimeException($msgerr); } - return $content; + //force cast to made phpstan happy, but return is always string here + return (string)$content; } } diff --git a/phpstan.neon b/phpstan.neon index 55ed8e8..be1b56c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,6 @@ parameters: parallel: maximumNumberOfProcesses: 2 - level: 6 + level: 8 paths: - lib/php/