Skip to content

Commit

Permalink
CallFinder: Add meta info for when new without parens is used
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Nov 16, 2024
1 parent dc1c820 commit 14b7dcc
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 9 deletions.
Binary file modified build/kint.phar
Binary file not shown.
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<code><![CDATA[KINT_PHP80]]></code>
<code><![CDATA[KINT_PHP81]]></code>
<code><![CDATA[KINT_PHP82]]></code>
<code><![CDATA[KINT_PHP84]]></code>
</RedundantCondition>
<TypeDoesNotContainType>
<code><![CDATA[!KINT_PHP82]]></code>
Expand Down
39 changes: 37 additions & 2 deletions src/CallFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* path: string,
* expression: bool,
* literal: bool,
* new_without_parens: bool,
* }
*/
class CallFinder
Expand Down Expand Up @@ -194,7 +195,7 @@ public static function getFunctionCalls(string $source, int $line, $function): a
}

if (!KINT_PHP84) {
self::$operator[T_NEW] = true;
self::$operator[T_NEW] = true; // @codeCoverageIgnore
}

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

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

// As of 8.4 new is only an expression when parentheses are
// omitted. In that case we can cheat and add them ourselves.
//
// > PHP interprets the first expression after new as a class name
// per https://wiki.php.net/rfc/new_without_parentheses
if (KINT_PHP84 && !$expression && T_NEW === $name[0][0]) {
$had_name_token = false;
$new_without_parens = true;

foreach ($name as $token) {
if (T_NEW === $token[0]) {
continue;
}

if (isset(self::$ignore[$token[0]])) {
continue;
}

if (T_CLASS === $token[0]) {
$new_without_parens = false;
break;
}

if ('(' === $token && $had_name_token) {
$new_without_parens = false;
break;
}

$had_name_token = true;
}
}

if (!$expression && 1 === \count($name)) {
switch ($name[0][0]) {
case T_CONSTANT_ENCAPSED_STRING:
Expand Down Expand Up @@ -440,6 +474,7 @@ public static function getFunctionCalls(string $source, int $line, $function): a
'path' => $path,
'expression' => $expression,
'literal' => $literal,
'new_without_parens' => $new_without_parens,
];
}

Expand Down Expand Up @@ -605,7 +640,7 @@ private static function tokensFormatted(array $tokens): array
continue;
}

$token = ' ';
$token[1] = ' ';
$space = true;
} else {
if (KINT_PHP80 && null !== $last && T_ATTRIBUTE === $last[0]) {
Expand Down
6 changes: 5 additions & 1 deletion src/Kint.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,10 @@ public static function getBasesFromParamInfo(array $params, int $argc): array
if (isset($param['path'])) {
$access_path = $param['path'];

if (!empty($param['expression'])) {
if ($param['expression']) {
$access_path = '('.$access_path.')';
} elseif ($param['new_without_parens']) {
$access_path .= '()';
}
} else {
$access_path = '$'.$i;
Expand Down Expand Up @@ -624,6 +626,7 @@ protected static function getSingleCall(array $frame, array $args): ?array
'path' => \substr($param['path'], 3).'['.\var_export($key, true).']',
'expression' => false,
'literal' => false,
'new_without_parens' => false,
];
}
} else {
Expand All @@ -635,6 +638,7 @@ protected static function getSingleCall(array $frame, array $args): ?array
'path' => 'array_values('.\substr($param['path'], 3).')['.$j.']',
'expression' => false,
'literal' => false,
'new_without_parens' => false,
];
}
}
Expand Down
Loading

0 comments on commit 14b7dcc

Please sign in to comment.