Complex classes like LockedCommandDecorator 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 LockedCommandDecorator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class LockedCommandDecorator extends Command |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var Command |
||
| 17 | */ |
||
| 18 | private $decoratedCommand; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string|LockHandler |
||
| 22 | */ |
||
| 23 | private $lockName; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $lockPath; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Constructor. |
||
| 32 | * |
||
| 33 | * @param Command $command |
||
| 34 | * @param string|LockHandler $lockName |
||
| 35 | * @param string $lockPath |
||
| 36 | */ |
||
| 37 | 30 | public function __construct(Command $command, $lockName = null, $lockPath = null) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 3 | public function ignoreValidationErrors() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | 3 | public function setApplication(Application $application = null) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 3 | public function setHelperSet(HelperSet $helperSet) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | 3 | public function getHelperSet() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | 3 | public function getApplication() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 3 | public function isEnabled() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param string|LockHandler|null $lockName |
||
| 94 | */ |
||
| 95 | 30 | public function setLockName($lockName) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Runs the command. |
||
| 102 | * |
||
| 103 | * Before the decorated command is run, a lock is requested. |
||
| 104 | * When failed to acquire the lock, the command exits. |
||
| 105 | * |
||
| 106 | * @param InputInterface $input An InputInterface instance |
||
| 107 | * @param OutputInterface $output An OutputInterface instance |
||
| 108 | * |
||
| 109 | * @return int The command exit code |
||
| 110 | */ |
||
| 111 | 12 | public function run(InputInterface $input, OutputInterface $output) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | 3 | public function setCode(callable $code) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | 15 | public function mergeApplicationDefinition($mergeArgs = true) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | 3 | public function setDefinition($definition) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | 15 | public function getDefinition() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | 3 | public function getNativeDefinition() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 3 | public function addArgument($name, $mode = null, $description = '', $default = null) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | 3 | public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | 3 | public function setName($name) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | 3 | public function setProcessTitle($title) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | 3 | public function getName() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | 3 | public function setDescription($description) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | 3 | public function getDescription() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | 3 | public function setHelp($help) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | 3 | public function getHelp() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | 3 | public function getProcessedHelp() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | 3 | public function setAliases($aliases) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | 3 | public function getAliases() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | 3 | public function getSynopsis($short = false) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | 3 | public function getHelper($name) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get the locking helper. |
||
| 302 | * |
||
| 303 | * @param InputInterface $input |
||
| 304 | * |
||
| 305 | * @return LockHandler |
||
| 306 | */ |
||
| 307 | 12 | private function getLockHandler(InputInterface $input) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get the name for the lock. |
||
| 321 | * |
||
| 322 | * @param InputInterface $input |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 15 | public function getLockName(InputInterface $input) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Get the lock path. |
||
| 345 | * |
||
| 346 | * @param InputInterface $input |
||
| 347 | * |
||
| 348 | * @return null|string |
||
| 349 | */ |
||
| 350 | 12 | public function getLockPath(InputInterface $input) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Write the "is locked" message. |
||
| 369 | * |
||
| 370 | * @param InputInterface $input |
||
| 371 | * @param OutputInterface $output |
||
| 372 | */ |
||
| 373 | 6 | private function writeLockedMessage(InputInterface $input, OutputInterface $output) |
|
| 393 | } |
||
| 394 |