Skip to content

Commit 9b09a51

Browse files
Kevin Fosterkoenpunt
authored andcommitted
Refactor is_hash()
1 parent 890969d commit 9b09a51

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

lib/Utils.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ function array_flatten(array $array)
6363
/**
6464
* Somewhat naive way to determine if an array is a hash.
6565
*/
66-
function is_hash(&$array)
66+
function is_hash($array)
6767
{
68-
if (!is_array($array))
68+
if (!is_array($array)) {
6969
return false;
70+
}
7071

7172
$keys = array_keys($array);
72-
return @is_string($keys[0]) ? true : false;
73+
74+
return isset($keys[0]) && is_string($keys[0]);
7375
}
7476

7577
/**

test/UtilsTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,19 @@ public function test_wrap_strings_in_arrays()
103103
$x = '1';
104104
$this->assert_equals(array(array('1')),ActiveRecord\wrap_strings_in_arrays($x));
105105
}
106-
}
107106

107+
public function test_is_hash()
108+
{
109+
$hash = array('key' => 'value');
110+
$this->assert_true(ActiveRecord\is_hash($hash));
111+
112+
$notHash = array(0 => 'value');
113+
$this->assert_false(ActiveRecord\is_hash($notHash));
114+
}
115+
116+
public function test_is_hash_empty_array()
117+
{
118+
$notHash = array();
119+
$this->assert_false(ActiveRecord\is_hash($notHash));
120+
}
121+
};

0 commit comments

Comments
 (0)