Skip to content

Commit 68a3a2d

Browse files
committed
FIX Parse negative numbers in class spec
Before negative numbers was interpreted as possitive numbers.
1 parent c61c5f1 commit 68a3a2d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Core/ClassInfo.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ public static function parse_class_spec($classSpec)
423423
$bucketStack = [];
424424
$lastTokenWasNSSeparator = false;
425425
$currentKey = null;
426+
$factor = 1;
426427

427428
foreach ($tokens as $token) {
428429
// $forceResult used to allow null result to be detected
@@ -444,6 +445,8 @@ public static function parse_class_spec($classSpec)
444445
// Treat service name separator as NS separator
445446
$class .= '.';
446447
$lastTokenWasNSSeparator = true;
448+
} elseif ($token === '-') {
449+
$factor = -1;
447450
} elseif ($lastTokenWasNSSeparator && is_array($token) && $token[0] === T_STRING) {
448451
// Found another section of a namespaced class
449452
$class .= $token[1];
@@ -471,11 +474,11 @@ public static function parse_class_spec($classSpec)
471474
break;
472475

473476
case T_DNUMBER:
474-
$result = (double)$token[1];
477+
$result = ((double)$token[1]) * $factor;
475478
break;
476479

477480
case T_LNUMBER:
478-
$result = (int)$token[1];
481+
$result = ((int)$token[1]) * $factor;
479482
break;
480483

481484
case T_DOUBLE_ARROW:
@@ -550,6 +553,8 @@ public static function parse_class_spec($classSpec)
550553
// Set the active bucket to be our newly-pushed, empty array
551554
$bucket = &$bucket[$key];
552555
}
556+
557+
$factor = 1;
553558
}
554559
}
555560

tests/php/Core/ClassInfoTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,26 @@ public static function provideClassSpecCases()
352352
'Foo\\Bar\\NamespacedClass.withmodifier(["and-arg" => true])',
353353
['Foo\\Bar\\NamespacedClass.withmodifier', [["and-arg" => true]]],
354354
],
355+
'Number' => [
356+
'Foo(1)',
357+
['Foo', [1]]
358+
],
359+
'Negative integer' => [
360+
'Foo(-1)',
361+
['Foo', [-1]]
362+
],
363+
'Negative integer in array' => [
364+
'Foo([-1])',
365+
['Foo', [[-1]]]
366+
],
367+
'Multiple integers' => [
368+
'Foo(-1, 5, 4, -2)',
369+
['Foo', [-1, 5, 4, -2]]
370+
],
371+
'Negative double' => [
372+
'Foo(-1.5)',
373+
['Foo', [-1.5]]
374+
],
355375
];
356376
}
357377
}

0 commit comments

Comments
 (0)