comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/InfoFiles/ClassFilesSniff.php @ 0:4c8ae668cc8c

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