Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Helpers.php

function Helpers::isConstructorPromotion

* Return true if the token looks like constructor promotion. * * Call on a parameter variable token only. * *

Parameters

File $phpcsFile: * @param int $stackPtr * * @return bool

1 call to Helpers::isConstructorPromotion()
VariableAnalysisSniff::processVariableAsFunctionParameter in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a parameter definition if it is inside a function definition. * * This does not include variables imported by a "use" statement. * *

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php, line 1629

Class

Helpers

Namespace

VariableAnalysis\Lib

Code

public static function isConstructorPromotion(File $phpcsFile, $stackPtr) {
    // If we are not in a function's parameters, this is not promotion.
    $functionIndex = self::getFunctionIndexForFunctionParameter($phpcsFile, $stackPtr);
    if (!$functionIndex) {
        return false;
    }
    $tokens = $phpcsFile->getTokens();
    // Move backwards from the token, ignoring whitespace, typehints, and the
    // 'readonly' keyword, and return true if the previous token is a
    // visibility keyword (eg: `public`).
    for ($i = $stackPtr - 1; $i > $functionIndex; $i--) {
        if (in_array($tokens[$i]['code'], Tokens::$scopeModifiers, true)) {
            return true;
        }
        if (in_array($tokens[$i]['code'], Tokens::$emptyTokens, true)) {
            continue;
        }
        if ($tokens[$i]['content'] === 'readonly') {
            continue;
        }
        if (self::isTokenPartOfTypehint($phpcsFile, $i)) {
            continue;
        }
        return false;
    }
    return false;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal