Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use array_key_exists instead of isset()

This allows contains() to work with null values.

Fixes #4083
  • Loading branch information...
commit a30f861f2c53b888f41fe9242138f0e1e3147466 1 parent 68aefe7
@markstory markstory authored
View
6 lib/Cake/Test/Case/Utility/HashTest.php
@@ -593,6 +593,12 @@ public function testContains() {
);
$this->assertTrue(Hash::contains($b, $a));
$this->assertFalse(Hash::contains($a, $b));
+
+ $a = array(0 => 'test', 'string' => null);
+ $this->assertTrue(Hash::contains($a, array('string' => null)));
+
+ $a = array(0 => 'test', 'string' => null);
+ $this->assertTrue(Hash::contains($a, array('test')));
}
/**
View
4 lib/Cake/Utility/Hash.php
@@ -442,14 +442,14 @@ public static function contains(array $data, array $needle) {
$val = $needle[$key];
unset($needle[$key]);
- if (isset($data[$key]) && is_array($val)) {
+ if (array_key_exists($key, $data) && is_array($val)) {
$next = $data[$key];
unset($data[$key]);
if (!empty($val)) {
$stack[] = array($val, $next);
}
- } elseif (!isset($data[$key]) || $data[$key] != $val) {
+ } elseif (!array_key_exists($key, $data) || $data[$key] != $val) {
return false;
}

7 comments on commit a30f861

@EliuFlorez

In theory redimiento improving the result of php code faster with less time evaluating the array is like this: if (isset ($ data [$ key]))

@Phally

Did you read the commit message and the ticket?

@dereuromark
Collaborator

You should :)

@josegonzalez
Collaborator

lol.

@markstory
Owner

@ElluFlorez I'm well aware of the performance differences between isset() and array_key_exists(). This change intentionally switched to array_key_exists() to solve specific bugs.

@EliuFlorez

ok I understand the reason. : D @ markstory regards

Please sign in to comment.
Something went wrong with that request. Please try again.