Skip to content

Commit 28bacf7

Browse files
committed
Minor perf improvements
1 parent e680231 commit 28bacf7

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/FnDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private function validateSeq($from, array $types, $a, $b)
324324
$ta = Utils::type($a);
325325
$tb = Utils::type($b);
326326

327-
if ($ta != $tb) {
327+
if ($ta !== $tb) {
328328
$msg = "encountered a type mismatch in sequence: {$ta}, {$tb}";
329329
$this->typeError($from, $msg);
330330
}

src/Utils.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
class Utils
55
{
6+
static $typeMap = [
7+
'boolean' => 'boolean',
8+
'string' => 'string',
9+
'NULL' => 'null',
10+
'double' => 'number',
11+
'float' => 'number',
12+
'integer' => 'number'
13+
];
14+
615
/**
716
* Gets the JMESPath type equivalent of a PHP variable.
817
*
@@ -12,39 +21,32 @@ class Utils
1221
*/
1322
public static function type($arg)
1423
{
15-
static $map = [
16-
'boolean' => 'boolean',
17-
'string' => 'string',
18-
'NULL' => 'null',
19-
'double' => 'number',
20-
'float' => 'number',
21-
'integer' => 'number'
22-
];
23-
2424
$type = gettype($arg);
25-
if (isset($map[$type])) {
26-
return $map[$type];
27-
}
28-
29-
if ($type == 'object') {
30-
if ($arg instanceof \stdClass) {
31-
return 'object';
32-
} elseif ($arg instanceof \Closure) {
33-
return 'expression';
34-
} elseif ($arg instanceof \ArrayAccess
35-
&& $arg instanceof \Countable
36-
) {
37-
return count($arg) == 0 || $arg->offsetExists(0) ? 'array' : 'object';
38-
} elseif (method_exists($arg, '__toString')) {
39-
return 'string';
40-
} else {
41-
throw new \InvalidArgumentException(
42-
'Unable to determine JMESPath type from ' . get_class($arg)
43-
);
25+
if (isset(self::$typeMap[$type])) {
26+
return self::$typeMap[$type];
27+
} elseif ($type === 'array') {
28+
if (empty($arg)) {
29+
return 'array';
4430
}
31+
reset($arg);
32+
return key($arg) === 0 ? 'array' : 'object';
33+
} elseif ($arg instanceof \stdClass) {
34+
return 'object';
35+
} elseif ($arg instanceof \Closure) {
36+
return 'expression';
37+
} elseif ($arg instanceof \ArrayAccess
38+
&& $arg instanceof \Countable
39+
) {
40+
return count($arg) == 0 || $arg->offsetExists(0)
41+
? 'array'
42+
: 'object';
43+
} elseif (method_exists($arg, '__toString')) {
44+
return 'string';
4545
}
4646

47-
return !$arg || array_keys($arg)[0] === 0 ? 'array' : 'object';
47+
throw new \InvalidArgumentException(
48+
'Unable to determine JMESPath type from ' . get_class($arg)
49+
);
4850
}
4951

5052
/**

0 commit comments

Comments
 (0)