Skip to content

Commit 9f108b2

Browse files
authored
Support type declarations on class properties (#67)
* Support type declarations on class properties * PHP 7.4 and above do not round to -0
1 parent 54089c0 commit 9f108b2

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

src/Reflection/ReflectionClass.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,13 @@ public function getProperties(): array
311311
// Seek forward, skipping static, if it was found and check if we
312312
// really have a property here, otherwise confine and try to find more
313313
// properties.
314-
315-
// We also skip final, to improve error handling and consistent behaviour,
314+
//
315+
// Skip final, to improve error handling and consistent behaviour,
316316
// otherwise final private $foo would be parsed and private final $bar
317317
// would not be parsed.
318-
$var_loc = $tokens->next($vis_loc, [T_COMMENT, T_WHITESPACE, T_STATIC, T_FINAL]);
318+
//
319+
// Skip T_STRING because if it exists, it must contain a type declaration.
320+
$var_loc = $tokens->next($vis_loc, [T_COMMENT, T_WHITESPACE, T_STATIC, T_FINAL, T_STRING]);
319321
if ($tokens->type($var_loc) !== T_VARIABLE) {
320322
continue;
321323
}

test/Generator/DecimalTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public function setup(): void
1818
{
1919
$this->inflector = InflectorFactory::create()->build();
2020
}
21-
2221
private function getTestValues($scale, $precision): iterable
2322
{
2423
$values = [];
@@ -179,7 +178,21 @@ public function roundProvider(): iterable
179178
{
180179
$huge = str_repeat('1', 35) . '.' . str_repeat('5', 29);
181180

182-
return [
181+
if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
182+
$test_set = [
183+
['decimal_18_20', -1.0E-30, '0.000000000000000000'],
184+
['decimal_19_20', -1.0E-20, '0.0000000000000000000'],
185+
['decimal_30_65', -1.6E-130, '0.000000000000000000000000000000'],
186+
];
187+
} else { // before php 7.4.0
188+
$test_set = [
189+
['decimal_18_20', -1.0E-30, '-0.000000000000000000'],
190+
['decimal_19_20', -1.0E-20, '-0.0000000000000000000'],
191+
['decimal_30_65', -1.6E-130, '-0.000000000000000000000000000000'],
192+
];
193+
}
194+
195+
$test_set = array_merge($test_set, [
183196
['decimal_0_10', .5, '1'],
184197
['decimal_0_10', .499, '0'],
185198
['decimal_0_10', 1, '1'],
@@ -204,22 +217,21 @@ public function roundProvider(): iterable
204217
['decimal_10_10', 1.0E-30, '0.0000000000'],
205218

206219
['decimal_18_20', 1.0E-30, '0.000000000000000000'],
207-
['decimal_18_20', -1.0E-30, '-0.000000000000000000'],
208220
['decimal_18_20', '12.3456789012345678915', '12.345678901234567892'],
209221
['decimal_18_20', '-12.3456789012345678915', '-12.345678901234567892'],
210222

211223
['decimal_19_20', 1.0E-30, '0.0000000000000000000'],
212-
['decimal_19_20', -1.0E-20, '-0.0000000000000000000'],
213224
['decimal_19_20', '1.23456789012345678915', '1.2345678901234567892'],
214225
['decimal_19_20', '-1.23456789012345678915', '-1.2345678901234567892'],
215226

216227
['decimal_30_65', 1.0E-20, '0.000000000000000000010000000000'],
217228
['decimal_30_65', 1.6E-130, '0.000000000000000000000000000000'],
218-
['decimal_30_65', -1.6E-130, '-0.000000000000000000000000000000'],
219229
['decimal_30_65', $huge . '54', $huge . '5'],
220230
['decimal_30_65', $huge . '55', $huge . '6'],
221231
['decimal_30_65', '-' . $huge . '55', '-' . $huge . '6'],
222-
];
232+
]);
233+
234+
return $test_set;
223235
}
224236

225237
/**

test/Reflection/ReflectionClassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function fileProvider(): array
110110
'public $kruin;',
111111
'public $nerf;',
112112
'private $riet;',
113+
'private $touw;',
113114
],
114115
],
115116
[

test/Reflection/fixtures/modifiers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ class Boom {
1818
public $kruin static;
1919
var $nerf;
2020
public static private final protected private $riet;
21+
private string $touw;
2122
}

0 commit comments

Comments
 (0)