Conditions | 38 |
Paths | > 20000 |
Total Lines | 214 |
Code Lines | 122 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
43 | public static function load(Ruleset $ruleset, Config $config) |
||
44 | { |
||
45 | // Look at every loaded sniff class so far and use their file contents |
||
46 | // to generate a hash for the code used during the run. |
||
47 | // At this point, the loaded class list contains the core PHPCS code |
||
48 | // and all sniffs that have been loaded as part of the run. |
||
49 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
50 | echo PHP_EOL."\tGenerating loaded file list for code hash".PHP_EOL; |
||
51 | } |
||
52 | |||
53 | $codeHash = ''; |
||
54 | $classes = array_keys(Autoload::getLoadedClasses()); |
||
55 | sort($classes); |
||
56 | |||
57 | $installDir = dirname(__DIR__); |
||
58 | $installDirLen = strlen($installDir); |
||
59 | $standardDir = $installDir.DIRECTORY_SEPARATOR.'Standards'; |
||
60 | $standardDirLen = strlen($standardDir); |
||
61 | foreach ($classes as $file) { |
||
62 | if (substr($file, 0, $standardDirLen) !== $standardDir) { |
||
63 | if (substr($file, 0, $installDirLen) === $installDir) { |
||
64 | // We are only interested in sniffs here. |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
69 | echo "\t\t=> external file: $file".PHP_EOL; |
||
70 | } |
||
71 | } else if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
72 | echo "\t\t=> internal sniff: $file".PHP_EOL; |
||
73 | } |
||
74 | |||
75 | $codeHash .= md5_file($file); |
||
76 | } |
||
77 | |||
78 | // Add the content of the used rulesets to the hash so that sniff setting |
||
79 | // changes in the ruleset invalidate the cache. |
||
80 | $rulesets = $ruleset->paths; |
||
81 | sort($rulesets); |
||
82 | foreach ($rulesets as $file) { |
||
83 | if (substr($file, 0, $standardDirLen) !== $standardDir) { |
||
84 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
85 | echo "\t\t=> external ruleset: $file".PHP_EOL; |
||
86 | } |
||
87 | } else if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
88 | echo "\t\t=> internal ruleset: $file".PHP_EOL; |
||
89 | } |
||
90 | |||
91 | $codeHash .= md5_file($file); |
||
92 | } |
||
93 | |||
94 | // Go through the core PHPCS code and add those files to the file |
||
95 | // hash. This ensures that core PHPCS changes will also invalidate the cache. |
||
96 | // Note that we ignore sniffs here, and any files that don't affect |
||
97 | // the outcome of the run. |
||
98 | $di = new \RecursiveIteratorIterator( |
||
|
|||
99 | new \RecursiveDirectoryIterator($installDir), |
||
100 | 0, |
||
101 | \RecursiveIteratorIterator::CATCH_GET_CHILD |
||
102 | ); |
||
103 | |||
104 | $di = new \RecursiveDirectoryIterator($installDir); |
||
105 | $filter = new \RecursiveCallbackFilterIterator( |
||
106 | $di, |
||
107 | function ($file, $key, $iterator) { |
||
108 | // Skip hidden files. |
||
109 | $filename = $file->getFilename(); |
||
110 | if (substr($filename, 0, 1) === '.') { |
||
111 | return false; |
||
112 | } |
||
113 | |||
114 | $filePath = Common::realpath($file->getPathname()); |
||
115 | if ($filePath === false) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | if (is_dir($filePath) === true |
||
120 | && ($filename === 'Standards' |
||
121 | || $filename === 'Exceptions' |
||
122 | || $filename === 'Reports' |
||
123 | || $filename === 'Generators') |
||
124 | ) { |
||
125 | return false; |
||
126 | } |
||
127 | |||
128 | return true; |
||
129 | } |
||
130 | ); |
||
131 | |||
132 | $iterator = new \RecursiveIteratorIterator($filter); |
||
133 | foreach ($iterator as $file) { |
||
134 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
135 | echo "\t\t=> core file: $file".PHP_EOL; |
||
136 | } |
||
137 | |||
138 | $codeHash .= md5_file($file); |
||
139 | } |
||
140 | |||
141 | $codeHash = md5($codeHash); |
||
142 | |||
143 | // Along with the code hash, use various settings that can affect |
||
144 | // the results of a run to create a new hash. This hash will be used |
||
145 | // in the cache file name. |
||
146 | $rulesetHash = md5(var_export($ruleset->ignorePatterns, true).var_export($ruleset->includePatterns, true)); |
||
147 | $configData = array( |
||
148 | 'tabWidth' => $config->tabWidth, |
||
149 | 'encoding' => $config->encoding, |
||
150 | 'recordErrors' => $config->recordErrors, |
||
151 | 'codeHash' => $codeHash, |
||
152 | 'rulesetHash' => $rulesetHash, |
||
153 | ); |
||
154 | |||
155 | $configString = implode(',', $configData); |
||
156 | $cacheHash = substr(sha1($configString), 0, 12); |
||
157 | |||
158 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
159 | echo "\tGenerating cache key data".PHP_EOL; |
||
160 | echo "\t\t=> tabWidth: ".$configData['tabWidth'].PHP_EOL; |
||
161 | echo "\t\t=> encoding: ".$configData['encoding'].PHP_EOL; |
||
162 | echo "\t\t=> recordErrors: ".(int) $configData['recordErrors'].PHP_EOL; |
||
163 | echo "\t\t=> codeHash: ".$configData['codeHash'].PHP_EOL; |
||
164 | echo "\t\t=> rulesetHash: ".$configData['rulesetHash'].PHP_EOL; |
||
165 | echo "\t\t=> cacheHash: $cacheHash".PHP_EOL; |
||
166 | } |
||
167 | |||
168 | if ($config->cacheFile !== null) { |
||
169 | $cacheFile = $config->cacheFile; |
||
170 | } else { |
||
171 | // Determine the common paths for all files being checked. |
||
172 | // We can use this to locate an existing cache file, or to |
||
173 | // determine where to create a new one. |
||
174 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
175 | echo "\tChecking possible cache file paths".PHP_EOL; |
||
176 | } |
||
177 | |||
178 | $paths = array(); |
||
179 | foreach ($config->files as $file) { |
||
180 | $file = Common::realpath($file); |
||
181 | while ($file !== DIRECTORY_SEPARATOR) { |
||
182 | if (isset($paths[$file]) === false) { |
||
183 | $paths[$file] = 1; |
||
184 | } else { |
||
185 | $paths[$file]++; |
||
186 | } |
||
187 | |||
188 | $lastFile = $file; |
||
189 | $file = dirname($file); |
||
190 | if ($file === $lastFile) { |
||
191 | // Just in case something went wrong, |
||
192 | // we don't want to end up in an infinite loop. |
||
193 | break; |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | ksort($paths); |
||
199 | $paths = array_reverse($paths); |
||
200 | |||
201 | $numFiles = count($config->files); |
||
202 | $tmpDir = sys_get_temp_dir(); |
||
203 | $cacheFile = null; |
||
204 | foreach ($paths as $file => $count) { |
||
205 | if ($count !== $numFiles) { |
||
206 | unset($paths[$file]); |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | $fileHash = substr(sha1($file), 0, 12); |
||
211 | $testFile = $tmpDir.DIRECTORY_SEPARATOR."phpcs.$fileHash.$cacheHash.cache"; |
||
212 | if ($cacheFile === null) { |
||
213 | // This will be our default location if we can't find |
||
214 | // an existing file. |
||
215 | $cacheFile = $testFile; |
||
216 | } |
||
217 | |||
218 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
219 | echo "\t\t=> $testFile".PHP_EOL; |
||
220 | echo "\t\t\t * based on shared location: $file *".PHP_EOL; |
||
221 | } |
||
222 | |||
223 | if (file_exists($testFile) === true) { |
||
224 | $cacheFile = $testFile; |
||
225 | break; |
||
226 | } |
||
227 | }//end foreach |
||
228 | |||
229 | if ($cacheFile === null) { |
||
230 | // Unlikely, but just in case $paths is empty for some reason. |
||
231 | $cacheFile = $tmpDir.DIRECTORY_SEPARATOR."phpcs.$cacheHash.cache"; |
||
232 | } |
||
233 | }//end if |
||
234 | |||
235 | self::$path = $cacheFile; |
||
236 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
237 | echo "\t=> Using cache file: ".self::$path.PHP_EOL; |
||
238 | } |
||
239 | |||
240 | if (file_exists(self::$path) === true) { |
||
241 | self::$cache = json_decode(file_get_contents(self::$path), true); |
||
242 | |||
243 | // Verify the contents of the cache file. |
||
244 | if (self::$cache['config'] !== $configData) { |
||
245 | self::$cache = array(); |
||
246 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
247 | echo "\t* cache was invalid and has been cleared *".PHP_EOL; |
||
248 | } |
||
249 | } |
||
250 | } else if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
251 | echo "\t* cache file does not exist *".PHP_EOL; |
||
252 | } |
||
253 | |||
254 | self::$cache['config'] = $configData; |
||
255 | |||
256 | }//end load() |
||
257 | |||
327 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.