Complex classes like GitInfo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GitInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class GitInfo { |
||
27 | |||
28 | /** |
||
29 | * Singleton for the repo at $IP |
||
30 | */ |
||
31 | protected static $repo = null; |
||
32 | |||
33 | /** |
||
34 | * Location of the .git directory |
||
35 | */ |
||
36 | protected $basedir; |
||
37 | |||
38 | /** |
||
39 | * Path to JSON cache file for pre-computed git information. |
||
40 | */ |
||
41 | protected $cacheFile; |
||
42 | |||
43 | /** |
||
44 | * Cached git information. |
||
45 | */ |
||
46 | protected $cache = []; |
||
47 | |||
48 | /** |
||
49 | * Map of repo URLs to viewer URLs. Access via static method getViewers(). |
||
50 | */ |
||
51 | private static $viewers = false; |
||
52 | |||
53 | /** |
||
54 | * @param string $repoDir The root directory of the repo where .git can be found |
||
55 | * @param bool $usePrecomputed Use precomputed information if available |
||
56 | * @see precomputeValues |
||
57 | */ |
||
58 | public function __construct( $repoDir, $usePrecomputed = true ) { |
||
93 | |||
94 | /** |
||
95 | * Compute the path to the cache file for a given directory. |
||
96 | * |
||
97 | * @param string $repoDir The root directory of the repo where .git can be found |
||
98 | * @return string Path to GitInfo cache file in $wgGitInfoCacheDirectory or |
||
99 | * fallback in the extension directory itself |
||
100 | * @since 1.24 |
||
101 | */ |
||
102 | protected static function getCacheFilePath( $repoDir ) { |
||
130 | |||
131 | /** |
||
132 | * Get the singleton for the repo at $IP |
||
133 | * |
||
134 | * @return GitInfo |
||
135 | */ |
||
136 | public static function repo() { |
||
143 | |||
144 | /** |
||
145 | * Check if a string looks like a hex encoded SHA1 hash |
||
146 | * |
||
147 | * @param string $str The string to check |
||
148 | * @return bool Whether or not the string looks like a SHA1 |
||
149 | */ |
||
150 | public static function isSHA1( $str ) { |
||
153 | |||
154 | /** |
||
155 | * Get the HEAD of the repo (without any opening "ref: ") |
||
156 | * |
||
157 | * @return string|bool The HEAD (git reference or SHA1) or false |
||
158 | */ |
||
159 | public function getHead() { |
||
177 | |||
178 | /** |
||
179 | * Get the SHA1 for the current HEAD of the repo |
||
180 | * |
||
181 | * @return string|bool A SHA1 or false |
||
182 | */ |
||
183 | public function getHeadSHA1() { |
||
202 | |||
203 | /** |
||
204 | * Get the commit date of HEAD entry of the git code repository |
||
205 | * |
||
206 | * @since 1.22 |
||
207 | * @return int|bool Commit date (UNIX timestamp) or false |
||
208 | */ |
||
209 | public function getHeadCommitDate() { |
||
231 | |||
232 | /** |
||
233 | * Get the name of the current branch, or HEAD if not found |
||
234 | * |
||
235 | * @return string|bool The branch name, HEAD, or false |
||
236 | */ |
||
237 | public function getCurrentBranch() { |
||
249 | |||
250 | /** |
||
251 | * Get an URL to a web viewer link to the HEAD revision. |
||
252 | * |
||
253 | * @return string|bool String if a URL is available or false otherwise |
||
254 | */ |
||
255 | public function getHeadViewUrl() { |
||
276 | |||
277 | /** |
||
278 | * Get the URL of the remote origin. |
||
279 | * @return string|bool String if a URL is available or false otherwise. |
||
280 | */ |
||
281 | protected function getRemoteUrl() { |
||
310 | |||
311 | /** |
||
312 | * Check to see if the current cache is fully populated. |
||
313 | * |
||
314 | * Note: This method is public only to make unit testing easier. There's |
||
315 | * really no strong reason that anything other than a test should want to |
||
316 | * call this method. |
||
317 | * |
||
318 | * @return bool True if all expected cache keys exist, false otherwise |
||
319 | */ |
||
320 | public function cacheIsComplete() { |
||
327 | |||
328 | /** |
||
329 | * Precompute and cache git information. |
||
330 | * |
||
331 | * Creates a JSON file in the cache directory associated with this |
||
332 | * GitInfo instance. This cache file will be used by subsequent GitInfo objects referencing |
||
333 | * the same directory to avoid needing to examine the .git directory again. |
||
334 | * |
||
335 | * @since 1.24 |
||
336 | */ |
||
337 | public function precomputeValues() { |
||
363 | |||
364 | /** |
||
365 | * @see self::getHeadSHA1 |
||
366 | * @return string |
||
367 | */ |
||
368 | public static function headSHA1() { |
||
371 | |||
372 | /** |
||
373 | * @see self::getCurrentBranch |
||
374 | * @return string |
||
375 | */ |
||
376 | public static function currentBranch() { |
||
379 | |||
380 | /** |
||
381 | * @see self::getHeadViewUrl() |
||
382 | * @return bool|string |
||
383 | */ |
||
384 | public static function headViewUrl() { |
||
387 | |||
388 | /** |
||
389 | * Gets the list of repository viewers |
||
390 | * @return array |
||
391 | */ |
||
392 | protected static function getViewers() { |
||
402 | } |
||
403 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..