Skip to content

Commit

Permalink
avoid passing null to max function
Browse files Browse the repository at this point in the history
  • Loading branch information
Guite committed Jan 16, 2024
1 parent 612413f commit 520f5d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ private function loadMinFromNode(\DOMElement $node, ?int $min): ?int

$minOccurs = (int) $node->getAttribute('minOccurs');

return max($min, $minOccurs);
// NOTE must not pass "null" to max() function, because "max(null, 0)" returns "null" instead of "0"
return max((int) $min, $minOccurs);
}

private function loadMaxFromNode(\DOMElement $node, ?int $max): ?int
Expand Down Expand Up @@ -499,7 +500,8 @@ private function loadSequenceChildNodeLoadElement(
$this->resolveSubstitutionGroup($schema, $node, $childNode, $element);

if (null !== $min) {
$element->setMin($min);
// NOTE must not pass "null" to max() function, because "max(null, 0)" returns "null" instead of "0"
$element->setMin(max((int) $element->getMin(), $min));

Check failure on line 504 in src/SchemaReader.php

View workflow job for this annotation

GitHub Actions / QA - Run psalm (PHP 8.1)

RedundantCast

src/SchemaReader.php:504:34: RedundantCast: Redundant cast to int (see https://psalm.dev/262)

Check failure on line 504 in src/SchemaReader.php

View workflow job for this annotation

GitHub Actions / QA - Run psalm (PHP 8.1)

RedundantCast

src/SchemaReader.php:504:34: RedundantCast: Redundant cast to int (see https://psalm.dev/262)
}

if (null !== $max && 1 < $max) {
Expand Down
7 changes: 4 additions & 3 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ public function testSequenceMinOccursOverride($sequenceMinOccurs, $childMinOccur
public function getMinOccurencesOverride(): array
{
return [
['0', '2', 2],
['3', '2', 3],
['0', '5', 5],
['0', '1', 1],
['1', '1', 1],
['2', '1', 2],
['0', '0', 0],
['3', '0', 3],
['3', '2', 3],
['0', '0', 0],
[null, '0', 0],
];
}

Expand Down

0 comments on commit 520f5d6

Please sign in to comment.