Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_InfoFiles_ClassFilesSniff.
|
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 files[] entries in info files. Only files containing classes/interfaces
|
Chris@0
|
12 * should be listed.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @category PHP
|
Chris@0
|
15 * @package PHP_CodeSniffer
|
Chris@0
|
16 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
17 */
|
Chris@0
|
18 class Drupal_Sniffs_InfoFiles_ClassFilesSniff implements PHP_CodeSniffer_Sniff
|
Chris@0
|
19 {
|
Chris@0
|
20
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @return array
|
Chris@0
|
26 */
|
Chris@0
|
27 public function register()
|
Chris@0
|
28 {
|
Chris@0
|
29 return array(T_INLINE_HTML);
|
Chris@0
|
30
|
Chris@0
|
31 }//end register()
|
Chris@0
|
32
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
38 * @param int $stackPtr The position of the current token in the
|
Chris@0
|
39 * stack passed in $tokens.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @return int
|
Chris@0
|
42 */
|
Chris@0
|
43 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
44 {
|
Chris@0
|
45 // Only run this sniff once per info file.
|
Chris@0
|
46 $fileExtension = strtolower(substr($phpcsFile->getFilename(), -4));
|
Chris@0
|
47 if ($fileExtension !== 'info') {
|
Chris@0
|
48 return ($phpcsFile->numTokens + 1);
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 $contents = file_get_contents($phpcsFile->getFilename());
|
Chris@0
|
52 $info = self::drupalParseInfoFormat($contents);
|
Chris@0
|
53 if (isset($info['files']) === true && is_array($info['files']) === true) {
|
Chris@0
|
54 foreach ($info['files'] as $file) {
|
Chris@0
|
55 $fileName = dirname($phpcsFile->getFilename()).'/'.$file;
|
Chris@0
|
56 if (file_exists($fileName) === false) {
|
Chris@0
|
57 // We need to find the position of the offending line in the
|
Chris@0
|
58 // info file.
|
Chris@0
|
59 $ptr = self::getPtr('files[]', $file, $phpcsFile);
|
Chris@0
|
60 $error = 'Declared file was not found';
|
Chris@0
|
61 $phpcsFile->addError($error, $ptr, 'DeclaredFileNotFound');
|
Chris@0
|
62 continue;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 // Read the file, parse its tokens and check if it actually contains
|
Chris@0
|
66 // a class or interface definition.
|
Chris@0
|
67 $searchTokens = token_get_all(file_get_contents($fileName));
|
Chris@0
|
68 foreach ($searchTokens as $token) {
|
Chris@0
|
69 if (is_array($token) === true
|
Chris@0
|
70 && in_array($token[0], array(T_CLASS, T_INTERFACE, T_TRAIT)) === true
|
Chris@0
|
71 ) {
|
Chris@0
|
72 continue 2;
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 $ptr = self::getPtr('files[]', $file, $phpcsFile);
|
Chris@0
|
77 $error = "It's only necessary to declare files[] if they declare a class or interface.";
|
Chris@0
|
78 $phpcsFile->addError($error, $ptr, 'UnecessaryFileDeclaration');
|
Chris@0
|
79 }//end foreach
|
Chris@0
|
80 }//end if
|
Chris@0
|
81
|
Chris@0
|
82 return ($phpcsFile->numTokens + 1);
|
Chris@0
|
83
|
Chris@0
|
84 }//end process()
|
Chris@0
|
85
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Helper function that returns the position of the key in the info file.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param string $key Key name to search for.
|
Chris@0
|
91 * @param string $value Corresponding value to search for.
|
Chris@0
|
92 * @param PHP_CodeSniffer_File $infoFile Info file to search in.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return int|false Returns the stack position if the file name is found, false
|
Chris@0
|
95 * otherwise.
|
Chris@0
|
96 */
|
Chris@0
|
97 public static function getPtr($key, $value, PHP_CodeSniffer_File $infoFile)
|
Chris@0
|
98 {
|
Chris@0
|
99 foreach ($infoFile->getTokens() as $ptr => $tokenInfo) {
|
Chris@0
|
100 if (preg_match('@^[\s]*'.preg_quote($key).'[\s]*=[\s]*["\']?'.preg_quote($value).'["\']?@', $tokenInfo['content']) === 1) {
|
Chris@0
|
101 return $ptr;
|
Chris@0
|
102 }
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 return false;
|
Chris@0
|
106
|
Chris@0
|
107 }//end getPtr()
|
Chris@0
|
108
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Parses a Drupal info file. Copied from Drupal core drupal_parse_info_format().
|
Chris@0
|
112 *
|
Chris@0
|
113 * @param string $data The contents of the info file to parse
|
Chris@0
|
114 *
|
Chris@0
|
115 * @return array The info array.
|
Chris@0
|
116 */
|
Chris@0
|
117 public static function drupalParseInfoFormat($data)
|
Chris@0
|
118 {
|
Chris@0
|
119 $info = array();
|
Chris@0
|
120 $constants = get_defined_constants();
|
Chris@0
|
121
|
Chris@0
|
122 if (preg_match_all(
|
Chris@0
|
123 '
|
Chris@0
|
124 @^\s* # Start at the beginning of a line, ignoring leading whitespace
|
Chris@0
|
125 ((?:
|
Chris@0
|
126 [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
|
Chris@0
|
127 \[[^\[\]]*\] # unless they are balanced and not nested
|
Chris@0
|
128 )+?)
|
Chris@0
|
129 \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
|
Chris@0
|
130 (?:
|
Chris@0
|
131 ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
|
Chris@0
|
132 (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
|
Chris@0
|
133 ([^\r\n]*?) # Non-quoted string
|
Chris@0
|
134 )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
|
Chris@0
|
135 @msx',
|
Chris@0
|
136 $data,
|
Chris@0
|
137 $matches,
|
Chris@0
|
138 PREG_SET_ORDER
|
Chris@0
|
139 ) !== false
|
Chris@0
|
140 ) {
|
Chris@0
|
141 foreach ($matches as $match) {
|
Chris@0
|
142 // Fetch the key and value string.
|
Chris@0
|
143 $i = 0;
|
Chris@0
|
144 foreach (array('key', 'value1', 'value2', 'value3') as $var) {
|
Chris@0
|
145 if (isset($match[++$i]) === true) {
|
Chris@0
|
146 $$var = $match[$i];
|
Chris@0
|
147 } else {
|
Chris@0
|
148 $$var = '';
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 $value = stripslashes(substr($value1, 1, -1)).stripslashes(substr($value2, 1, -1)).$value3;
|
Chris@0
|
153
|
Chris@0
|
154 // Parse array syntax.
|
Chris@0
|
155 $keys = preg_split('/\]?\[/', rtrim($key, ']'));
|
Chris@0
|
156 $last = array_pop($keys);
|
Chris@0
|
157 $parent = &$info;
|
Chris@0
|
158
|
Chris@0
|
159 // Create nested arrays.
|
Chris@0
|
160 foreach ($keys as $key) {
|
Chris@0
|
161 if ($key === '') {
|
Chris@0
|
162 $key = count($parent);
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 if (isset($parent[$key]) === false || is_array($parent[$key]) === false) {
|
Chris@0
|
166 $parent[$key] = array();
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 $parent = &$parent[$key];
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 // Handle PHP constants.
|
Chris@0
|
173 if (isset($constants[$value]) === true) {
|
Chris@0
|
174 $value = $constants[$value];
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 // Insert actual value.
|
Chris@0
|
178 if ($last === '') {
|
Chris@0
|
179 $last = count($parent);
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 $parent[$last] = $value;
|
Chris@0
|
183 }//end foreach
|
Chris@0
|
184 }//end if
|
Chris@0
|
185
|
Chris@0
|
186 return $info;
|
Chris@0
|
187
|
Chris@0
|
188 }//end drupalParseInfoFormat()
|
Chris@0
|
189
|
Chris@0
|
190
|
Chris@0
|
191 }//end class
|