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

Breadcrumb

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

class AttributeHelper

Same name in this branch
  1. 11.1.x core/lib/Drupal/Core/Template/AttributeHelper.php \Drupal\Core\Template\AttributeHelper

@internal

Hierarchy

  • class \SlevomatCodingStandard\Helpers\AttributeHelper

Expanded class hierarchy of AttributeHelper

6 files declare their use of AttributeHelper
AttributeAndTargetSpacingSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/AttributeAndTargetSpacingSniff.php
AttributesOrderSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/AttributesOrderSniff.php
DisallowAttributesJoiningSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/DisallowAttributesJoiningSniff.php
DisallowMultipleAttributesPerLineSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/DisallowMultipleAttributesPerLineSniff.php
NewWithParenthesesSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/NewWithParenthesesSniff.php

... See full list

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/AttributeHelper.php, line 26

Namespace

SlevomatCodingStandard\Helpers
View source
class AttributeHelper {
    private const ATTRIBUTE_TARGETS = [
        T_ANON_CLASS,
        T_CLASS,
        T_CLOSURE,
        T_CONST,
        T_ENUM,
        T_ENUM_CASE,
        T_FN,
        T_FUNCTION,
        T_INTERFACE,
        T_TRAIT,
        T_VARIABLE,
    ];
    
    /**
     * @return list<Attribute>
     */
    public static function getAttributes(File $phpcsFile, int $attributeOpenerPointer) : array {
        $tokens = $phpcsFile->getTokens();
        if ($tokens[$attributeOpenerPointer]['code'] !== T_ATTRIBUTE) {
            throw new InvalidArgumentException(sprintf('Token %d must be attribute, %s given.', $attributeOpenerPointer, $tokens[$attributeOpenerPointer]['type']));
        }
        $attributeCloserPointer = $tokens[$attributeOpenerPointer]['attribute_closer'];
        $actualPointer = $attributeOpenerPointer;
        $attributes = [];
        do {
            $attributeNameStartPointer = TokenHelper::findNextEffective($phpcsFile, $actualPointer + 1, $attributeCloserPointer);
            if ($attributeNameStartPointer === null) {
                break;
            }
            $attributeNameEndPointer = TokenHelper::findNextExcluding($phpcsFile, TokenHelper::getNameTokenCodes(), $attributeNameStartPointer + 1) - 1;
            $attributeName = TokenHelper::getContent($phpcsFile, $attributeNameStartPointer, $attributeNameEndPointer);
            $pointerAfterAttributeName = TokenHelper::findNextEffective($phpcsFile, $attributeNameEndPointer + 1, $attributeCloserPointer);
            if ($pointerAfterAttributeName === null) {
                $attributes[] = new Attribute($attributeOpenerPointer, $attributeName, $attributeNameStartPointer, $attributeNameEndPointer);
                break;
            }
            if ($tokens[$pointerAfterAttributeName]['code'] === T_COMMA) {
                $attributes[] = new Attribute($attributeOpenerPointer, $attributeName, $attributeNameStartPointer, $attributeNameEndPointer);
                $actualPointer = $pointerAfterAttributeName;
            }
            if ($tokens[$pointerAfterAttributeName]['code'] === T_OPEN_PARENTHESIS) {
                $attributes[] = new Attribute($attributeOpenerPointer, $attributeName, $attributeNameStartPointer, $tokens[$pointerAfterAttributeName]['parenthesis_closer'], TokenHelper::getContent($phpcsFile, $pointerAfterAttributeName, $tokens[$pointerAfterAttributeName]['parenthesis_closer']));
                $actualPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$pointerAfterAttributeName]['parenthesis_closer'] + 1, $attributeCloserPointer);
                continue;
            }
        } while ($actualPointer !== null);
        return $attributes;
    }
    
    /**
     * Attributes have syntax that when defined incorrectly or in older PHP version, they are treated as comments.
     * An example of incorrect declaration is variables that are not properties.
     */
    public static function isValidAttribute(File $phpcsFile, int $attributeOpenerPointer) : bool {
        return self::getAttributeTarget($phpcsFile, $attributeOpenerPointer) !== null;
    }
    public static function getAttributeTarget(File $phpcsFile, int $attributeOpenerPointer) : ?int {
        $attributeTargetPointer = TokenHelper::findNext($phpcsFile, self::ATTRIBUTE_TARGETS, $attributeOpenerPointer);
        if ($attributeTargetPointer === null) {
            return null;
        }
        if ($phpcsFile->getTokens()[$attributeTargetPointer]['code'] === T_VARIABLE && !PropertyHelper::isProperty($phpcsFile, $attributeTargetPointer) && !ParameterHelper::isParameter($phpcsFile, $attributeTargetPointer)) {
            return null;
        }
        return $attributeTargetPointer;
    }

}

Members

Title Sort descending Modifiers Object type Summary
AttributeHelper::ATTRIBUTE_TARGETS private constant
AttributeHelper::getAttributes public static function *
AttributeHelper::getAttributeTarget public static function
AttributeHelper::isValidAttribute public static function * Attributes have syntax that when defined incorrectly or in older PHP version, they are treated as comments.
* An example of incorrect declaration is variables that are not properties.

API Navigation

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