Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Remove runtime array validation
Browse files Browse the repository at this point in the history
- it worked on PHP5, but not HHVM. As we now require HHVM, it's pointless
  facebook/hhvm#4961
- iterating over containers for typechecks is likely to be a perf issue
  • Loading branch information
fredemmott committed Mar 5, 2015
1 parent e85f464 commit 03ef74e
Showing 1 changed file with 0 additions and 80 deletions.
80 changes: 0 additions & 80 deletions src/core/ComposableElement.php
Expand Up @@ -565,13 +565,6 @@ final protected function validateAttributeValue<T>(
if (!is_array($val)) {
throw new XHPInvalidAttributeException($this, 'array', $attr, $val);
}
if ($decl[$attr][1]) {
$this->validateArrayAttributeValue(
(array)$decl[$attr][1],
$attr,
$val,
);
}
break;

case self::TYPE_OBJECT:
Expand Down Expand Up @@ -600,79 +593,6 @@ final protected function validateAttributeValue<T>(
return $val;
}

final private function validateArrayAttributeValue(
array<int, mixed> $decl,
string $attr,
array<mixed> $val,
): void {
if ($decl[0]) { // Key declaration
if ($decl[0] == self::TYPE_STRING) {
$type = 'string';
$func = fun('is_string');
} else {
$type = 'int';
$func = fun('is_int');
}
if (count($val) != count(array_filter(array_keys($val), $func))) {
$bad = $type == 'string' ? 'int' : 'string';
throw new XHPInvalidArrayKeyAttributeException(
$this,
(string)$type,
$attr,
$bad
);
}
}
switch ((int)$decl[1]) { // Value declaration
case self::TYPE_STRING:
$type = 'string';
$func = fun('is_string');
break;
case self::TYPE_BOOL:
$type = 'bool';
$func = fun('is_bool');
break;
case self::TYPE_INTEGER:
$type = 'int';
$func = fun('is_int');
break;
case self::TYPE_FLOAT:
$type = 'float';
$func = fun('is_numeric');
break;
case self::TYPE_ARRAY:
$type = 'array';
$func = fun('is_array');
break;
case self::TYPE_OBJECT:
$type = $decl[2];
$func = function($item) use ($type) {
return $item instanceof $type;
};
break;
}
$filtered = array_filter($val, $func);
if (count($val) != count($filtered)) {
$bad = array_diff($val, $filtered);
throw new XHPInvalidArrayAttributeException(
$this,
(string)$type,
$attr,
reset($bad)
);
}

if (isset($decl[2]) && $decl[1] == self::TYPE_ARRAY) {
foreach ($val as $arrayVal) {
$this->validateArrayAttributeValue(
(array)$decl[2],
$attr,
(array)$arrayVal,
);
}
}
}

/**
* Validates that this element's children match its children descriptor, and
* throws an exception if that's not the case.
Expand Down

0 comments on commit 03ef74e

Please sign in to comment.