Skip to content

Commit

Permalink
phpstan lvl 8
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Nov 16, 2023
1 parent de9d657 commit 9f5d861
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
25 changes: 18 additions & 7 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 11 additions & 1 deletion lib/php/FilesToJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
parallel:
maximumNumberOfProcesses: 2
level: 6
level: 8
paths:
- lib/php/

0 comments on commit 9f5d861

Please sign in to comment.