Skip to content

Commit e410f68

Browse files
committed
FIX Parse negative numbers in class spec
Before negative numbers was interpreted as possitive numbers.
1 parent 29c4bd7 commit e410f68

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,30 @@ public function provideClassSpecCases()
366366
'Foo\\Bar\\NamespacedClass.withmodifier(["and-arg" => true])',
367367
['Foo\\Bar\\NamespacedClass.withmodifier', [["and-arg" => true]]],
368368
],
369+
'Number' => [
370+
'Foo(1)',
371+
['Foo', [1]]
372+
],
373+
'Negative integer' => [
374+
'Foo(-1)',
375+
['Foo', [-1]]
376+
],
377+
'Negative integer in array' => [
378+
'Foo([-1])',
379+
['Foo', [[-1]]]
380+
],
381+
'Multiple integers' => [
382+
'Foo(-1, 5, 4, -2)',
383+
['Foo', [-1, 5, 4, -2]]
384+
],
385+
'Negative double' => [
386+
'Foo(-1.5)',
387+
['Foo', [-1.5]]
388+
],
389+
'Minus in strings' => [
390+
'Foo("-")',
391+
['Foo', ['-']]
392+
],
369393
];
370394
}
371395
}

0 commit comments

Comments
 (0)