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

Breadcrumb

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

function ValidVariableNameSniff::processMemberVar

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions\ValidVariableNameSniff::processMemberVar()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php \PHP_CodeSniffer\Standards\Zend\Sniffs\NamingConventions\ValidVariableNameSniff::processMemberVar()
  3. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php \PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidVariableNameSniff::processMemberVar()

Processes class member variables.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

Overrides AbstractVariableSniff::processMemberVar

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidVariableNameSniff.php, line 37

Class

ValidVariableNameSniff
\Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.

Namespace

Drupal\Sniffs\NamingConventions

Code

protected function processMemberVar(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $memberProps = $phpcsFile->getMemberProperties($stackPtr);
    if (empty($memberProps) === true) {
        return;
    }
    // Check if the class extends another class and get the name of the class
    // that is extended.
    if (empty($tokens[$stackPtr]['conditions']) === false) {
        $classPtr = key($tokens[$stackPtr]['conditions']);
        $extendsName = $phpcsFile->findExtendedClassName($classPtr);
        // Special case config entities: those are allowed to have underscores in
        // their class property names. If a class extends something like
        // ConfigEntityBase then we consider it a config entity class and allow
        // underscores.
        if ($extendsName !== false && strpos($extendsName, 'ConfigEntity') !== false) {
            return;
        }
        // Plugin annotations may have underscores in class properties.
        // For example, see \Drupal\Core\Field\Annotation\FieldFormatter.
        // The only class named "Plugin" in Drupal core is
        // \Drupal\Component\Annotation\Plugin while many Views plugins
        // extend \Drupal\views\Annotation\ViewsPluginAnnotationBase.
        if ($extendsName !== false && in_array($extendsName, [
            'Plugin',
            'ViewsPluginAnnotationBase',
        ]) !== false) {
            return;
        }
        $implementsNames = $phpcsFile->findImplementedInterfaceNames($classPtr);
        if ($implementsNames !== false && in_array('AnnotationInterface', $implementsNames) !== false) {
            return;
        }
    }
    
    //end if
    // The name of a property must start with a lowercase letter, properties
    // with underscores are not allowed, except the cases handled above.
    $memberName = ltrim($tokens[$stackPtr]['content'], '$');
    if (preg_match('/^[a-z]/', $memberName) === 1 && strpos($memberName, '_') === false) {
        return;
    }
    $error = 'Class property %s should use lowerCamel naming without underscores';
    $data = [
        $tokens[$stackPtr]['content'],
    ];
    $phpcsFile->addError($error, $stackPtr, 'LowerCamelName', $data);
}

API Navigation

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