Skip to content

Commit 5b97e89

Browse files
committed
Replace isConstructorPromotion manual scan with FunctionDeclarations::getParameters()
Delegates to PHPCSUtils which already parses all parameter attributes including property_visibility for constructor-promoted parameters. Also picks up PHP 8.4 asymmetric visibility (set_visibility_token) for free. Removes the now-unused isTokenPartOfTypehint() and isTokenPossiblyPartOfTypehint() helpers.
1 parent f4ac147 commit 5b97e89

1 file changed

Lines changed: 5 additions & 99 deletions

File tree

VariableAnalysis/Lib/Helpers.php

Lines changed: 5 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Util\Tokens;
1313
use PHPCSUtils\Utils\Conditions;
1414
use PHPCSUtils\Utils\Context;
15+
use PHPCSUtils\Utils\FunctionDeclarations;
1516
use PHPCSUtils\Utils\Lists;
1617
use PHPCSUtils\Utils\Parentheses;
1718

@@ -1388,31 +1389,15 @@ public static function getForLoopForIncrementVariable($stackPtr, $forLoops)
13881389
*/
13891390
public static function isConstructorPromotion(File $phpcsFile, $stackPtr)
13901391
{
1391-
// If we are not in a function's parameters, this is not promotion.
13921392
$functionIndex = self::getFunctionIndexForFunctionParameter($phpcsFile, $stackPtr);
13931393
if (! $functionIndex) {
13941394
return false;
13951395
}
1396-
1397-
$tokens = $phpcsFile->getTokens();
1398-
1399-
// Move backwards from the token, ignoring whitespace, typehints, and the
1400-
// 'readonly' keyword, and return true if the previous token is a
1401-
// visibility keyword (eg: `public`).
1402-
for ($i = $stackPtr - 1; $i > $functionIndex; $i--) {
1403-
if (in_array($tokens[$i]['code'], Tokens::$scopeModifiers, true)) {
1404-
return true;
1396+
$params = FunctionDeclarations::getParameters($phpcsFile, $functionIndex);
1397+
foreach ($params as $param) {
1398+
if ($param['token'] === $stackPtr) {
1399+
return isset($param['property_visibility']);
14051400
}
1406-
if (in_array($tokens[$i]['code'], Tokens::$emptyTokens, true)) {
1407-
continue;
1408-
}
1409-
if ($tokens[$i]['content'] === 'readonly') {
1410-
continue;
1411-
}
1412-
if (self::isTokenPartOfTypehint($phpcsFile, $i)) {
1413-
continue;
1414-
}
1415-
return false;
14161401
}
14171402
return false;
14181403
}
@@ -1470,85 +1455,6 @@ public static function getFunctionNameWithNamespace(File $phpcsFile, $stackPtr)
14701455
return $functionName;
14711456
}
14721457

1473-
/**
1474-
* Return false if the token is definitely not part of a typehint
1475-
*
1476-
* @param File $phpcsFile
1477-
* @param int $stackPtr
1478-
*
1479-
* @return bool
1480-
*/
1481-
private static function isTokenPossiblyPartOfTypehint(File $phpcsFile, $stackPtr)
1482-
{
1483-
$tokens = $phpcsFile->getTokens();
1484-
$token = $tokens[$stackPtr];
1485-
if ($token['code'] === 'PHPCS_T_NULLABLE') {
1486-
return true;
1487-
}
1488-
if ($token['code'] === T_NAME_QUALIFIED) {
1489-
return true;
1490-
}
1491-
if ($token['code'] === T_NAME_RELATIVE) {
1492-
return true;
1493-
}
1494-
if ($token['code'] === T_NAME_FULLY_QUALIFIED) {
1495-
return true;
1496-
}
1497-
if ($token['code'] === T_NS_SEPARATOR) {
1498-
return true;
1499-
}
1500-
if ($token['code'] === T_STRING) {
1501-
return true;
1502-
}
1503-
if ($token['code'] === T_TRUE) {
1504-
return true;
1505-
}
1506-
if ($token['code'] === T_FALSE) {
1507-
return true;
1508-
}
1509-
if ($token['code'] === T_NULL) {
1510-
return true;
1511-
}
1512-
if ($token['content'] === '|') {
1513-
return true;
1514-
}
1515-
if (in_array($token['code'], Tokens::$emptyTokens)) {
1516-
return true;
1517-
}
1518-
return false;
1519-
}
1520-
1521-
/**
1522-
* Return true if the token is inside a typehint
1523-
*
1524-
* @param File $phpcsFile
1525-
* @param int $stackPtr
1526-
*
1527-
* @return bool
1528-
*/
1529-
public static function isTokenPartOfTypehint(File $phpcsFile, $stackPtr)
1530-
{
1531-
$tokens = $phpcsFile->getTokens();
1532-
1533-
if (! self::isTokenPossiblyPartOfTypehint($phpcsFile, $stackPtr)) {
1534-
return false;
1535-
}
1536-
1537-
// Examine every following token, ignoring everything that might be part of
1538-
// a typehint. If we find a variable at the end, this is part of a
1539-
// typehint.
1540-
$i = $stackPtr;
1541-
while (true) {
1542-
$i += 1;
1543-
if (! isset($tokens[$i])) {
1544-
return false;
1545-
}
1546-
if (! self::isTokenPossiblyPartOfTypehint($phpcsFile, $i)) {
1547-
return ($tokens[$i]['code'] === T_VARIABLE);
1548-
}
1549-
}
1550-
}
1551-
15521458
/**
15531459
* Return true if the token is inside an abstract class.
15541460
*

0 commit comments

Comments
 (0)