GlobalDrupalSniff.php in Coder 8.2
File
coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalDrupalSniff.php
View source
<?php
namespace DrupalPractice\Sniffs\Objects;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use DrupalPractice\Project;
class GlobalDrupalSniff implements Sniff {
public static $baseClasses = array(
'BlockBase',
'ConfigFormBase',
'ContentEntityForm',
'ControllerBase',
'EntityForm',
'EntityReferenceFormatterBase',
'FileFormatterBase',
'FormatterBase',
'FormBase',
'ImageFormatter',
'ImageFormatterBase',
'WidgetBase',
);
public function register() {
return array(
T_STRING,
);
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
if ($tokens[$stackPtr]['content'] !== 'Drupal' || $tokens[$stackPtr + 1]['code'] !== T_DOUBLE_COLON || isset($tokens[$stackPtr + 2]) === false || $tokens[$stackPtr + 2]['code'] !== T_STRING || isset($tokens[$stackPtr + 3]) === false || $tokens[$stackPtr + 3]['code'] !== T_OPEN_PARENTHESIS || empty($tokens[$stackPtr]['conditions']) === true) {
return;
}
foreach ($tokens[$stackPtr]['conditions'] as $conditionPtr => $conditionCode) {
if ($conditionCode === T_FUNCTION && $phpcsFile
->getMethodProperties($conditionPtr)['is_static'] === true) {
return;
}
}
$classPtr = key($tokens[$stackPtr]['conditions']);
$extendsName = $phpcsFile
->findExtendedClassName($classPtr);
if (($extendsName === false || in_array($extendsName, static::$baseClasses) === false) && Project::isServiceClass($phpcsFile, $classPtr) === false) {
return;
}
$warning = '\\Drupal calls should be avoided in classes, use dependency injection instead';
$phpcsFile
->addWarning($warning, $stackPtr, 'GlobalDrupal');
}
}