|
12 | 12 | use PHP_CodeSniffer\Util\Tokens; |
13 | 13 | use PHPCSUtils\Utils\Conditions; |
14 | 14 | use PHPCSUtils\Utils\Context; |
| 15 | +use PHPCSUtils\Utils\FunctionDeclarations; |
15 | 16 | use PHPCSUtils\Utils\Lists; |
16 | 17 | use PHPCSUtils\Utils\Parentheses; |
17 | 18 |
|
@@ -1388,31 +1389,15 @@ public static function getForLoopForIncrementVariable($stackPtr, $forLoops) |
1388 | 1389 | */ |
1389 | 1390 | public static function isConstructorPromotion(File $phpcsFile, $stackPtr) |
1390 | 1391 | { |
1391 | | - // If we are not in a function's parameters, this is not promotion. |
1392 | 1392 | $functionIndex = self::getFunctionIndexForFunctionParameter($phpcsFile, $stackPtr); |
1393 | 1393 | if (! $functionIndex) { |
1394 | 1394 | return false; |
1395 | 1395 | } |
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']); |
1405 | 1400 | } |
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; |
1416 | 1401 | } |
1417 | 1402 | return false; |
1418 | 1403 | } |
@@ -1470,85 +1455,6 @@ public static function getFunctionNameWithNamespace(File $phpcsFile, $stackPtr) |
1470 | 1455 | return $functionName; |
1471 | 1456 | } |
1472 | 1457 |
|
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 | | - |
1552 | 1458 | /** |
1553 | 1459 | * Return true if the token is inside an abstract class. |
1554 | 1460 | * |
|
0 commit comments