Complex classes like SimpleFileResolver 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 SimpleFileResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class SimpleFileResolver extends AbstractFileResolver |
||
| 37 | { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The regular expression used to load the files with. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $regex = '/^.*\/%s\\.%s$/'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The matches for the last processed CSV filename. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $matches = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns the regular expression used to load the files with. |
||
| 55 | * |
||
| 56 | * @return string The regular expression |
||
| 57 | */ |
||
| 58 | 3 | protected function getRegex() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the number of matches found. |
||
| 65 | * |
||
| 66 | * @return integer The number of matches |
||
| 67 | */ |
||
| 68 | 3 | protected function countMatches() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Adds the passed match to the array with the matches. |
||
| 75 | * |
||
| 76 | * @param string $name The name of the match |
||
| 77 | * @param string $match The match itself |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | 3 | protected function addMatch($name, $match) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Returns the match with the passed name. |
||
| 88 | * |
||
| 89 | * @param string $name The name of the match to return |
||
| 90 | * |
||
| 91 | * @return string|null The match itself |
||
| 92 | */ |
||
| 93 | 3 | protected function getMatch($name) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the elements the filenames consists of, converted to lowercase. |
||
| 102 | * |
||
| 103 | * @return array The array with the filename elements |
||
| 104 | */ |
||
| 105 | 3 | protected function getPatternKeys() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Remove's the passed line from the file with the passed name. |
||
| 122 | * |
||
| 123 | * @param string $line The line to be removed |
||
| 124 | * @param string $filename The name of the file the line has to be removed |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 128 | */ |
||
| 129 | protected function removeLineFromFile($line, $filename) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the values to create the regex pattern from. |
||
| 136 | * |
||
| 137 | * @return array The array with the pattern values |
||
| 138 | */ |
||
| 139 | 3 | protected function resolvePatternValues() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Resolves the pattern value for the given element name. |
||
| 159 | * |
||
| 160 | * @param string $element The element name to resolve the pattern value for |
||
| 161 | * |
||
| 162 | * @return string|null The resolved pattern value |
||
| 163 | */ |
||
| 164 | 3 | protected function resolvePatternValue($element) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Prepares and returns the pattern for the regex to load the files from the |
||
| 187 | * source directory for the passed subject. |
||
| 188 | * |
||
| 189 | * @return string The prepared regex pattern |
||
| 190 | */ |
||
| 191 | 3 | protected function preparePattern() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Prepares and returns an OK filename from the passed parts. |
||
| 198 | * |
||
| 199 | * @param array $parts The parts to concatenate the OK filename from |
||
| 200 | * |
||
| 201 | * @return string The OK filename |
||
| 202 | */ |
||
| 203 | 1 | protected function prepareOkFilename(array $parts) |
|
| 204 | { |
||
| 205 | 1 | return sprintf('%s/%s.%s', $this->getSourceDir(), implode($this->getElementSeparator(), $parts), $this->getOkFileSuffix()); |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Query whether or not the basename, without suffix, of the passed filenames are equal. |
||
| 210 | * |
||
| 211 | * @param string $filename1 The first filename to compare |
||
| 212 | * @param string $filename2 The second filename to compare |
||
| 213 | * |
||
| 214 | * @return boolean TRUE if the passed files basename are equal, else FALSE |
||
| 215 | */ |
||
| 216 | 1 | protected function isEqualFilename($filename1, $filename2) |
|
| 217 | { |
||
| 218 | 1 | return $this->stripSuffix($filename1, $this->getSuffix()) === $this->stripSuffix($filename2, $this->getOkFileSuffix()); |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Strips the passed suffix, including the (.), from the filename and returns it. |
||
| 223 | * |
||
| 224 | * @param string $filename The filename to return the suffix from |
||
| 225 | * @param string $suffix The suffix to return |
||
| 226 | * |
||
| 227 | * @return string The filname without the suffix |
||
| 228 | */ |
||
| 229 | 1 | protected function stripSuffix($filename, $suffix) |
|
| 230 | { |
||
| 231 | 1 | return basename($filename, sprintf('.%s', $suffix)); |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 236 | * |
||
| 237 | * @return array The array with the expected OK filenames |
||
| 238 | */ |
||
| 239 | 1 | protected function getOkFilenames() |
|
| 240 | { |
||
| 241 | |||
| 242 | // initialize the array for the available okFilenames |
||
| 243 | 1 | $okFilenames = array(); |
|
| 244 | |||
| 245 | // prepare the OK filenames based on the found CSV file information |
||
| 246 | 1 | for ($i = 1; $i <= sizeof($patternKeys = $this->getPatternKeys()); $i++) { |
|
|
|
|||
| 247 | // intialize the array for the parts of the names (prefix, filename + counter) |
||
| 248 | 1 | $parts = array(); |
|
| 249 | // load the parts from the matches |
||
| 250 | 1 | for ($z = 0; $z < $i; $z++) { |
|
| 251 | // append the part |
||
| 252 | 1 | $parts[] = $this->getMatch($patternKeys[$z]); |
|
| 253 | } |
||
| 254 | |||
| 255 | // query whether or not, the OK file exists, if yes append it |
||
| 256 | 1 | if (file_exists($okFilename = $this->prepareOkFilename($parts))) { |
|
| 257 | 1 | $okFilenames[] = $okFilename; |
|
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | // prepare and return the pattern for the OK file |
||
| 262 | 1 | return $okFilenames; |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Resets the file resolver to parse another source directory for new files. |
||
| 267 | * |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | 1 | public function reset() |
|
| 271 | { |
||
| 272 | 1 | $this->matches = array(); |
|
| 273 | 1 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Returns the matches. |
||
| 277 | * |
||
| 278 | * @return array The array with the matches |
||
| 279 | */ |
||
| 280 | public function getMatches() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Queries whether or not, the passed filename should be handled by the subject. |
||
| 287 | * |
||
| 288 | * @param string $filename The filename to query for |
||
| 289 | * |
||
| 290 | * @return boolean TRUE if the file should be handled, else FALSE |
||
| 291 | */ |
||
| 292 | 3 | public function shouldBeHandled($filename) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 354 | * the OK file will be cleaned-up. |
||
| 355 | * |
||
| 356 | * @param string $filename The filename to be cleaned-up |
||
| 357 | * |
||
| 358 | * @return void |
||
| 359 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or the OK can not be cleaned-up |
||
| 360 | */ |
||
| 361 | public function cleanUpOkFile($filename) |
||
| 418 | } |
||
| 419 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: