Microoptimization for ServerRequest::is() and ServerRequest::isAll() (#14654)

This commit is contained in:
andrii-pukhalevych 2020-05-31 20:13:58 +03:00 committed by GitHub
parent 9a7c94bad0
commit 8addcf1fa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -508,14 +508,18 @@ class CakeRequest implements ArrayAccess {
* defined with CakeRequest::addDetector(). Any detector can be called * defined with CakeRequest::addDetector(). Any detector can be called
* as `is($type)` or `is$Type()`. * as `is($type)` or `is$Type()`.
* *
* @param string|array $type The type of request you want to check. If an array * @param string|string[] $type The type of request you want to check. If an array
* this method will return true if the request matches any type. * this method will return true if the request matches any type.
* @return bool Whether or not the request is the type you are checking. * @return bool Whether or not the request is the type you are checking.
*/ */
public function is($type) { public function is($type) {
if (is_array($type)) { if (is_array($type)) {
$result = array_map(array($this, 'is'), $type); foreach ($type as $_type) {
return count(array_filter($result)) > 0; if ($this->is($_type)) {
return true;
}
}
return false;
} }
$type = strtolower($type); $type = strtolower($type);
if (!isset($this->_detectors[$type])) { if (!isset($this->_detectors[$type])) {
@ -643,8 +647,12 @@ class CakeRequest implements ArrayAccess {
* @see CakeRequest::is() * @see CakeRequest::is()
*/ */
public function isAll(array $types) { public function isAll(array $types) {
$result = array_filter(array_map(array($this, 'is'), $types)); foreach ($types as $type) {
return count($result) === count($types); if (!$this->is($type)) {
return false;
}
}
return true;
} }
/** /**