Complex classes like Burgomaster 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 Burgomaster, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Burgomaster |
||
|
|||
10 | { |
||
11 | /** @var string Base staging directory of the project */ |
||
12 | public $stageDir; |
||
13 | |||
14 | /** @var string Root directory of the project */ |
||
15 | public $projectRoot; |
||
16 | |||
17 | /** @var array stack of sections */ |
||
18 | private $sections = array(); |
||
19 | |||
20 | /** |
||
21 | * @param string $stageDir Staging base directory where your packaging |
||
22 | * takes place. This folder will be created for |
||
23 | * you if it does not exist. If it exists, it |
||
24 | * will be deleted and recreated to start fresh. |
||
25 | * @param string $projectRoot Root directory of the project. |
||
26 | * |
||
27 | * @throws \InvalidArgumentException |
||
28 | * @throws \RuntimeException |
||
29 | */ |
||
30 | public function __construct($stageDir, $projectRoot = null) |
||
65 | |||
66 | /** |
||
67 | * Cleanup if the last section was not already closed. |
||
68 | */ |
||
69 | public function __destruct() |
||
75 | |||
76 | /** |
||
77 | * Call this method when starting a specific section of the packager. |
||
78 | * |
||
79 | * This makes the debug messages used in your script more meaningful and |
||
80 | * adds context when things go wrong. Be sure to call endSection() when |
||
81 | * you have finished a section of your packaging script. |
||
82 | * |
||
83 | * @param string $section Part of the packager that is running |
||
84 | */ |
||
85 | public function startSection($section) |
||
90 | |||
91 | /** |
||
92 | * Call this method when leaving the last pushed section of the packager. |
||
93 | */ |
||
94 | public function endSection() |
||
101 | |||
102 | /** |
||
103 | * Prints a debug message to STDERR bound to the current section. |
||
104 | * |
||
105 | * @param string $message Message to echo to STDERR |
||
106 | */ |
||
107 | public function debug($message) |
||
117 | |||
118 | /** |
||
119 | * Copies a file and creates the destination directory if needed. |
||
120 | * |
||
121 | * @param string $from File to copy |
||
122 | * @param string $to Destination to copy the file to, relative to the |
||
123 | * base staging directory. |
||
124 | * @throws \InvalidArgumentException if the file cannot be found |
||
125 | * @throws \RuntimeException if the directory cannot be created. |
||
126 | * @throws \RuntimeException if the file cannot be copied. |
||
127 | */ |
||
128 | public function deepCopy($from, $to) |
||
147 | |||
148 | /** |
||
149 | * Recursively copy one folder to another. |
||
150 | * |
||
151 | * Any LICENSE file is automatically copied. |
||
152 | * |
||
153 | * @param string $sourceDir Source directory to copy from |
||
154 | * @param string $destDir Directory to copy the files to that is relative |
||
155 | * to the the stage base directory. |
||
156 | * @param array $extensions File extensions to copy from the $sourceDir. |
||
157 | * Defaults to "php" files only (e.g., ['php']). |
||
158 | * @throws \InvalidArgumentException if the source directory is invalid. |
||
159 | */ |
||
160 | function recursiveCopy( |
||
198 | |||
199 | /** |
||
200 | * Execute a command and throw an exception if the return code is not 0. |
||
201 | * |
||
202 | * @param string $command Command to execute |
||
203 | * |
||
204 | * @return string Returns the output of the command as a string |
||
205 | * @throws \RuntimeException on error. |
||
206 | */ |
||
207 | public function exec($command) |
||
220 | |||
221 | /** |
||
222 | * Creates a class-map autoloader to the staging directory in a file |
||
223 | * named autoloader.php |
||
224 | * |
||
225 | * @param array $files Files to explicitly require in the autoloader. This |
||
226 | * is similar to Composer's "files" "autoload" section. |
||
227 | * @param string $filename Name of the autoloader file. |
||
228 | * @throws \RuntimeException if the file cannot be written |
||
229 | */ |
||
230 | function createAutoloader($files = array(), $filename = 'autoloader.php') { |
||
293 | |||
294 | /** |
||
295 | * Creates a default stub for the phar that includeds the generated |
||
296 | * autoloader. |
||
297 | * |
||
298 | * This phar also registers a constant that can be used to check if you |
||
299 | * are running the phar. The constant is the basename of the $dest variable |
||
300 | * without the extension, with "_PHAR" appended, then converted to all |
||
301 | * caps (e.g., "/foo/guzzle.phar" gets a contant defined as GUZZLE_PHAR. |
||
302 | * |
||
303 | * @param $dest |
||
304 | * @param string $autoloaderFilename Name of the autoloader file. |
||
305 | * @param string $alias The phar alias to use |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | private function createStub($dest, $autoloaderFilename = 'autoloader.php', $alias = null) |
||
325 | |||
326 | /** |
||
327 | * Creates a phar that automatically registers an autoloader. |
||
328 | * |
||
329 | * Call this only after your staging directory is built. |
||
330 | * |
||
331 | * @param string $dest Where to save the file. The basename of the file |
||
332 | * is also used as the alias name in the phar |
||
333 | * (e.g., /path/to/guzzle.phar => guzzle.phar). |
||
334 | * @param string|bool|null $stub The path to the phar stub file. Pass or |
||
335 | * leave null to automatically have one created for you. Pass false |
||
336 | * to no use a stub in the generated phar. |
||
337 | * @param string $autoloaderFilename Name of the autolaoder filename. |
||
338 | */ |
||
339 | public function createPhar( |
||
361 | |||
362 | /** |
||
363 | * Creates a zip file containing the staged files of your project. |
||
364 | * |
||
365 | * Call this only after your staging directory is built. |
||
366 | * |
||
367 | * @param string $dest Where to save the zip file |
||
368 | */ |
||
369 | public function createZip($dest) |
||
380 | |||
381 | private function createDirIfNeeded($dir) |
||
387 | } |
||
388 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.