You are here

class ThemeSniff in Coder 8.3

Same name and namespace in other branches
  1. 8.2 coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/ThemeSniff.php \DrupalPractice\Sniffs\FunctionCalls\ThemeSniff
  2. 8.3.x coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/ThemeSniff.php \DrupalPractice\Sniffs\FunctionCalls\ThemeSniff

\DrupalPractice\Sniffs\FunctionCalls\Checks that theme functions are not directly called.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \Drupal\Sniffs\Semantics\FunctionCall implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \DrupalPractice\Sniffs\FunctionCalls\ThemeSniff

Expanded class hierarchy of ThemeSniff

File

coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/ThemeSniff.php, line 22

Namespace

DrupalPractice\Sniffs\FunctionCalls
View source
class ThemeSniff extends FunctionCall {

  /**
   * List of functions starting with "theme_" that don't generate theme output.
   *
   * @var array<string>
   */
  protected $reservedFunctions = [
    'theme_get_registry',
    'theme_get_setting',
    'theme_render_template',
    'theme_enable',
    'theme_disable',
    'theme_get_suggestions',
  ];

  /**
   * Processes this function call.
   *
   * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
   * @param int                         $stackPtr  The position of the function call in
   *                                               the stack.
   *
   * @return void
   */
  public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile
      ->getTokens();
    $functionName = $tokens[$stackPtr]['content'];
    if (strpos($functionName, 'theme_') !== 0 || in_array($functionName, $this->reservedFunctions) === true || $this
      ->isFunctionCall($phpcsFile, $stackPtr) === false) {
      return;
    }
    $themeName = substr($functionName, 6);
    $warning = "Do not call theme functions directly, use theme('%s', ...) instead";
    $phpcsFile
      ->addWarning($warning, $stackPtr, 'ThemeFunctionDirect', [
      $themeName,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FunctionCall::$arguments protected property Internal cache to save the calculated arguments of the function call.
FunctionCall::$closeBracket protected property The token position of the closing bracket of the function call.
FunctionCall::$functionCall protected property The token position of the function call.
FunctionCall::$includeMethodCalls protected property Whether method invocations with the same function name should be processed, too. 1
FunctionCall::$openBracket protected property The token position of the opening bracket of the function call.
FunctionCall::$phpcsFile protected property The currently processed file.
FunctionCall::getArgument public function Returns start and end token for a given argument number.
FunctionCall::isFunctionCall protected function Checks if this is a function call.
FunctionCall::register public function Returns an array of tokens this test wants to listen for.
ThemeSniff::$reservedFunctions protected property List of functions starting with "theme_" that don't generate theme output.
ThemeSniff::process public function Processes this function call. Overrides FunctionCall::process