Skip to content

Commit 14b7dcc

Browse files
committed
CallFinder: Add meta info for when new without parens is used
1 parent dc1c820 commit 14b7dcc

File tree

6 files changed

+287
-9
lines changed

6 files changed

+287
-9
lines changed

build/kint.phar

1.59 KB
Binary file not shown.

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<code><![CDATA[KINT_PHP80]]></code>
1414
<code><![CDATA[KINT_PHP81]]></code>
1515
<code><![CDATA[KINT_PHP82]]></code>
16+
<code><![CDATA[KINT_PHP84]]></code>
1617
</RedundantCondition>
1718
<TypeDoesNotContainType>
1819
<code><![CDATA[!KINT_PHP82]]></code>

src/CallFinder.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* path: string,
3636
* expression: bool,
3737
* literal: bool,
38+
* new_without_parens: bool,
3839
* }
3940
*/
4041
class CallFinder
@@ -194,7 +195,7 @@ public static function getFunctionCalls(string $source, int $line, $function): a
194195
}
195196

196197
if (!KINT_PHP84) {
197-
self::$operator[T_NEW] = true;
198+
self::$operator[T_NEW] = true; // @codeCoverageIgnore
198199
}
199200

200201
/** @psalm-var list<PhpToken> */
@@ -397,6 +398,7 @@ public static function getFunctionCalls(string $source, int $line, $function): a
397398
$path = self::tokensToString(self::tokensTrim($param['full']));
398399
$expression = false;
399400
$literal = false;
401+
$new_without_parens = false;
400402

401403
foreach ($name as $token) {
402404
if (self::tokenIsOperator($token)) {
@@ -405,6 +407,38 @@ public static function getFunctionCalls(string $source, int $line, $function): a
405407
}
406408
}
407409

410+
// As of 8.4 new is only an expression when parentheses are
411+
// omitted. In that case we can cheat and add them ourselves.
412+
//
413+
// > PHP interprets the first expression after new as a class name
414+
// per https://wiki.php.net/rfc/new_without_parentheses
415+
if (KINT_PHP84 && !$expression && T_NEW === $name[0][0]) {
416+
$had_name_token = false;
417+
$new_without_parens = true;
418+
419+
foreach ($name as $token) {
420+
if (T_NEW === $token[0]) {
421+
continue;
422+
}
423+
424+
if (isset(self::$ignore[$token[0]])) {
425+
continue;
426+
}
427+
428+
if (T_CLASS === $token[0]) {
429+
$new_without_parens = false;
430+
break;
431+
}
432+
433+
if ('(' === $token && $had_name_token) {
434+
$new_without_parens = false;
435+
break;
436+
}
437+
438+
$had_name_token = true;
439+
}
440+
}
441+
408442
if (!$expression && 1 === \count($name)) {
409443
switch ($name[0][0]) {
410444
case T_CONSTANT_ENCAPSED_STRING:
@@ -440,6 +474,7 @@ public static function getFunctionCalls(string $source, int $line, $function): a
440474
'path' => $path,
441475
'expression' => $expression,
442476
'literal' => $literal,
477+
'new_without_parens' => $new_without_parens,
443478
];
444479
}
445480

@@ -605,7 +640,7 @@ private static function tokensFormatted(array $tokens): array
605640
continue;
606641
}
607642

608-
$token = ' ';
643+
$token[1] = ' ';
609644
$space = true;
610645
} else {
611646
if (KINT_PHP80 && null !== $last && T_ATTRIBUTE === $last[0]) {

src/Kint.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,10 @@ public static function getBasesFromParamInfo(array $params, int $argc): array
370370
if (isset($param['path'])) {
371371
$access_path = $param['path'];
372372

373-
if (!empty($param['expression'])) {
373+
if ($param['expression']) {
374374
$access_path = '('.$access_path.')';
375+
} elseif ($param['new_without_parens']) {
376+
$access_path .= '()';
375377
}
376378
} else {
377379
$access_path = '$'.$i;
@@ -624,6 +626,7 @@ protected static function getSingleCall(array $frame, array $args): ?array
624626
'path' => \substr($param['path'], 3).'['.\var_export($key, true).']',
625627
'expression' => false,
626628
'literal' => false,
629+
'new_without_parens' => false,
627630
];
628631
}
629632
} else {
@@ -635,6 +638,7 @@ protected static function getSingleCall(array $frame, array $args): ?array
635638
'path' => 'array_values('.\substr($param['path'], 3).')['.$j.']',
636639
'expression' => false,
637640
'literal' => false,
641+
'new_without_parens' => false,
638642
];
639643
}
640644
}

0 commit comments

Comments
 (0)