Skip to content

Commit

Permalink
[BUGFIX] Avoid count() when checking types with "[]" notation (#245)
Browse files Browse the repository at this point in the history
This prevents calling count() on argument to check validity of first item. Cases where first value is "null" now means that the value itself is valid (e.g. container is empty).

Close: #242
  • Loading branch information
NamelessCoder authored and Marc Neuhaus committed Dec 19, 2016
1 parent 617595f commit af69261
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Core/ViewHelper/AbstractViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ protected function isValidType($type, $value)
} elseif ($type === 'array' || substr($type, -2) === '[]') {
if (!is_array($value) && !$value instanceof \ArrayAccess && !$value instanceof \Traversable && !empty($value)) {
return false;
} elseif (count($value) > 0 && substr($type, -2) === '[]') {
} elseif (substr($type, -2) === '[]') {
$firstElement = $this->getFirstElementOfNonEmpty($value);
if ($firstElement == null) {
return false;
if ($firstElement === null) {
return true;
}
return $this->isValidType(substr($type, 0, -2), $firstElement);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Core/ViewHelper/AbstractViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ public function getValidateArgumentsErrorsTestValues()
[new ArgumentDefinition('test', 'DateTime', '', true), 'test'],
[new ArgumentDefinition('test', 'integer', '', true), new \ArrayIterator(['bar'])],
[new ArgumentDefinition('test', 'object', '', true), 'test'],
[new ArgumentDefinition('test', 'string[]', '', true), [new \DateTime('now'),'test']],
[new ArgumentDefinition('test', 'string[]', '', true), [null]],
[new ArgumentDefinition('test', 'string[]', '', true), [new \DateTime('now'),'test']]
];
}

Expand Down

0 comments on commit af69261

Please sign in to comment.