Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * DrupalPractice_Sniffs_Objects_GlobalDrupalSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Checks that \Drupal::service() and friends is not used in forms, controllers, services.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @category PHP
|
Chris@0
|
14 * @package PHP_CodeSniffer
|
Chris@0
|
15 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
16 */
|
Chris@0
|
17 class DrupalPractice_Sniffs_Objects_GlobalDrupalSniff implements PHP_CodeSniffer_Sniff
|
Chris@0
|
18 {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * List of base classes where \Drupal should not be used in an extending class.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var string[]
|
Chris@0
|
24 */
|
Chris@0
|
25 public static $baseClasses = array(
|
Chris@0
|
26 'BlockBase',
|
Chris@0
|
27 'ConfigFormBase',
|
Chris@0
|
28 'ContentEntityForm',
|
Chris@0
|
29 'ControllerBase',
|
Chris@0
|
30 'EntityForm',
|
Chris@0
|
31 'EntityReferenceFormatterBase',
|
Chris@0
|
32 'FileFormatterBase',
|
Chris@0
|
33 'FormatterBase',
|
Chris@0
|
34 'FormBase',
|
Chris@0
|
35 'ImageFormatter',
|
Chris@0
|
36 'ImageFormatterBase',
|
Chris@0
|
37 'WidgetBase',
|
Chris@0
|
38 );
|
Chris@0
|
39
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @return array
|
Chris@0
|
45 */
|
Chris@0
|
46 public function register()
|
Chris@0
|
47 {
|
Chris@0
|
48 return array(T_STRING);
|
Chris@0
|
49
|
Chris@0
|
50 }//end register()
|
Chris@0
|
51
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
57 * @param int $stackPtr The position of the current token
|
Chris@0
|
58 * in the stack passed in $tokens.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return void
|
Chris@0
|
61 */
|
Chris@0
|
62 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
63 {
|
Chris@0
|
64 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
65
|
Chris@0
|
66 // We are only interested in Drupal:: static method calls, not in the global
|
Chris@0
|
67 // scope.
|
Chris@0
|
68 if ($tokens[$stackPtr]['content'] !== 'Drupal'
|
Chris@0
|
69 || $tokens[($stackPtr + 1)]['code'] !== T_DOUBLE_COLON
|
Chris@0
|
70 || isset($tokens[($stackPtr + 2)]) === false
|
Chris@0
|
71 || $tokens[($stackPtr + 2)]['code'] !== T_STRING
|
Chris@0
|
72 || isset($tokens[($stackPtr + 3)]) === false
|
Chris@0
|
73 || $tokens[($stackPtr + 3)]['code'] !== T_OPEN_PARENTHESIS
|
Chris@0
|
74 || empty($tokens[$stackPtr]['conditions']) === true
|
Chris@0
|
75 ) {
|
Chris@0
|
76 return;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 // Check that this statement is not in a static function.
|
Chris@0
|
80 foreach ($tokens[$stackPtr]['conditions'] as $conditionPtr => $conditionCode) {
|
Chris@0
|
81 if ($conditionCode === T_FUNCTION && $phpcsFile->getMethodProperties($conditionPtr)['is_static'] === true) {
|
Chris@0
|
82 return;
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 // Check if the class extends another class and get the name of the class
|
Chris@0
|
87 // that is extended.
|
Chris@0
|
88 $classPtr = key($tokens[$stackPtr]['conditions']);
|
Chris@0
|
89 $extendsName = $phpcsFile->findExtendedClassName($classPtr);
|
Chris@0
|
90
|
Chris@0
|
91 if (($extendsName === false || in_array($extendsName, static::$baseClasses) === false)
|
Chris@0
|
92 && DrupalPractice_Project::isServiceClass($phpcsFile, $classPtr) === false
|
Chris@0
|
93 ) {
|
Chris@0
|
94 return;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 $warning = '\Drupal calls should be avoided in classes, use dependency injection instead';
|
Chris@0
|
98 $phpcsFile->addWarning($warning, $stackPtr, 'GlobalDrupal');
|
Chris@0
|
99
|
Chris@0
|
100 }//end process()
|
Chris@0
|
101
|
Chris@0
|
102
|
Chris@0
|
103 }//end class
|