Skip to content

Commit

Permalink
Fix: Return early when json_decode()'ed data is not an object
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Oct 9, 2018
1 parent a2cfbf6 commit b3c12a3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
ignoreErrors:
- '#Access to an undefined property object::\$bin.#'
5 changes: 4 additions & 1 deletion src/Normalizer/BinNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public function normalize(string $json): string
));
}

if (!\property_exists($decoded, 'bin') || !\is_array($decoded->bin)) {
if (!\is_object($decoded)
|| !\property_exists($decoded, 'bin')
|| !\is_array($decoded->bin)
) {
return $json;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Normalizer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ public function __construct(string $schemaUri = 'https://getcomposer.org/schema.

public function normalize(string $json): string
{
if (null === \json_decode($json) && \JSON_ERROR_NONE !== \json_last_error()) {
$decoded = \json_decode($json);

if (null === $decoded && \JSON_ERROR_NONE !== \json_last_error()) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not valid JSON.',
$json
));
}

if (!\is_object($decoded)) {
return $json;
}

return $this->normalizer->normalize($json);
}
}
4 changes: 4 additions & 0 deletions src/Normalizer/ConfigHashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function normalize(string $json): string
));
}

if (!\is_object($decoded)) {
return $json;
}

$objectProperties = \array_intersect_key(
\get_object_vars($decoded),
\array_flip(self::$properties)
Expand Down
4 changes: 4 additions & 0 deletions src/Normalizer/PackageHashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function normalize(string $json): string
));
}

if (!\is_object($decoded)) {
return $json;
}

$objectProperties = \array_intersect_key(
\get_object_vars($decoded),
\array_flip(self::$properties)
Expand Down
4 changes: 4 additions & 0 deletions src/Normalizer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function normalize(string $json): string
));
}

if (!\is_object($decoded)) {
return $json;
}

$objectProperties = \array_intersect_key(
\get_object_vars($decoded),
\array_flip(self::$properties)
Expand Down

0 comments on commit b3c12a3

Please sign in to comment.