| Total Complexity | 50 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractTarAdapter 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.
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 AbstractTarAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class AbstractTarAdapter extends AbstractBinaryAdapter |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @inheritdoc |
||
| 26 | */ |
||
| 27 | protected function doCreate($path, $files, $recursive) |
||
| 28 | { |
||
| 29 | return $this->doTarCreate($this->getLocalOptions(), $path, $files, $recursive); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @inheritdoc |
||
| 34 | */ |
||
| 35 | protected function doListMembers(ResourceInterface $resource) |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritdoc |
||
| 42 | */ |
||
| 43 | protected function doAdd(ResourceInterface $resource, $files, $recursive) |
||
| 44 | { |
||
| 45 | return $this->doTarAdd($this->getLocalOptions(), $resource, $files, $recursive); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | protected function doRemove(ResourceInterface $resource, $files) |
||
| 52 | { |
||
| 53 | return $this->doTarRemove($this->getLocalOptions(), $resource, $files); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @inheritdoc |
||
| 58 | */ |
||
| 59 | protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false) |
||
| 60 | { |
||
| 61 | return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to, $overwrite); |
||
|
|
|||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @inheritdoc |
||
| 66 | */ |
||
| 67 | protected function doExtract(ResourceInterface $resource, $to) |
||
| 68 | { |
||
| 69 | return $this->doTarExtract($this->getLocalOptions(), $resource, $to); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritdoc |
||
| 74 | */ |
||
| 75 | protected function doGetInflatorVersion() |
||
| 76 | { |
||
| 77 | $process = $this |
||
| 78 | ->inflator |
||
| 79 | ->create() |
||
| 80 | ->add('--version') |
||
| 81 | ->getProcess(); |
||
| 82 | |||
| 83 | $process->run(); |
||
| 84 | |||
| 85 | if (!$process->isSuccessful()) { |
||
| 86 | throw new RuntimeException(sprintf( |
||
| 87 | 'Unable to execute the following command %s {output: %s}', |
||
| 88 | $process->getCommandLine(), $process->getErrorOutput() |
||
| 89 | )); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->parser->parseInflatorVersion($process->getOutput() ?: ''); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | */ |
||
| 98 | protected function doGetDeflatorVersion() |
||
| 99 | { |
||
| 100 | return $this->getInflatorVersion(); |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function doTarCreate($options, $path, $files = null, $recursive = true) |
||
| 104 | { |
||
| 105 | $files = (array) $files; |
||
| 106 | |||
| 107 | $builder = $this |
||
| 108 | ->inflator |
||
| 109 | ->create(); |
||
| 110 | |||
| 111 | if (!$recursive) { |
||
| 112 | $builder->add('--no-recursion'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $builder->add('-c'); |
||
| 116 | |||
| 117 | foreach ((array) $options as $option) { |
||
| 118 | $builder->add((string) $option); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (0 === count($files)) { |
||
| 122 | $nullFile = defined('PHP_WINDOWS_VERSION_BUILD') ? 'NUL' : '/dev/null'; |
||
| 123 | |||
| 124 | $builder->add('-f'); |
||
| 125 | $builder->add($path); |
||
| 126 | $builder->add('-T'); |
||
| 127 | $builder->add($nullFile); |
||
| 128 | |||
| 129 | $process = $builder->getProcess(); |
||
| 130 | $process->run(); |
||
| 131 | |||
| 132 | } else { |
||
| 133 | |||
| 134 | $builder->add(sprintf('--file=%s', $path)); |
||
| 135 | |||
| 136 | if (!$recursive) { |
||
| 137 | $builder->add('--no-recursion'); |
||
| 138 | } |
||
| 139 | |||
| 140 | $collection = $this->manager->handle(getcwd(), $files); |
||
| 141 | |||
| 142 | $builder->setWorkingDirectory($collection->getContext()); |
||
| 143 | |||
| 144 | $collection->forAll(function($i, ZippyResource $resource) use ($builder) { |
||
| 145 | return $builder->add($resource->getTarget()); |
||
| 146 | }); |
||
| 147 | |||
| 148 | $process = $builder->getProcess(); |
||
| 149 | |||
| 150 | try { |
||
| 151 | $process->run(); |
||
| 152 | } catch (ProcessException $e) { |
||
| 153 | $this->manager->cleanup($collection); |
||
| 154 | throw $e; |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->manager->cleanup($collection); |
||
| 158 | } |
||
| 159 | |||
| 160 | if (!$process->isSuccessful()) { |
||
| 161 | throw new RuntimeException(sprintf( |
||
| 162 | 'Unable to execute the following command %s {output: %s}', |
||
| 163 | $process->getCommandLine(), |
||
| 164 | $process->getErrorOutput() |
||
| 165 | )); |
||
| 166 | } |
||
| 167 | |||
| 168 | return new Archive($this->createResource($path), $this, $this->manager); |
||
| 169 | } |
||
| 170 | |||
| 171 | protected function doTarListMembers($options, ResourceInterface $resource) |
||
| 172 | { |
||
| 173 | $builder = $this |
||
| 174 | ->inflator |
||
| 175 | ->create(); |
||
| 176 | |||
| 177 | foreach ($this->getListMembersOptions() as $option) { |
||
| 178 | $builder->add($option); |
||
| 179 | } |
||
| 180 | |||
| 181 | $builder |
||
| 182 | ->add('--list') |
||
| 183 | ->add('-v') |
||
| 184 | ->add(sprintf('--file=%s', $resource->getResource())); |
||
| 185 | |||
| 186 | foreach ((array) $options as $option) { |
||
| 187 | $builder->add((string) $option); |
||
| 188 | } |
||
| 189 | |||
| 190 | $process = $builder->getProcess(); |
||
| 191 | $process->run(); |
||
| 192 | |||
| 193 | if (!$process->isSuccessful()) { |
||
| 194 | throw new RuntimeException(sprintf( |
||
| 195 | 'Unable to execute the following command %s {output: %s}', |
||
| 196 | $process->getCommandLine(), |
||
| 197 | $process->getErrorOutput() |
||
| 198 | )); |
||
| 199 | } |
||
| 200 | |||
| 201 | $members = array(); |
||
| 202 | |||
| 203 | foreach ($this->parser->parseFileListing($process->getOutput() ?: '') as $member) { |
||
| 204 | $members[] = new Member( |
||
| 205 | $resource, |
||
| 206 | $this, |
||
| 207 | $member['location'], |
||
| 208 | $member['size'], |
||
| 209 | $member['mtime'], |
||
| 210 | $member['is_dir'] |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $members; |
||
| 215 | } |
||
| 216 | |||
| 217 | protected function doTarAdd($options, ResourceInterface $resource, $files, $recursive = true) |
||
| 267 | } |
||
| 268 | |||
| 269 | protected function doTarRemove($options, ResourceInterface $resource, $files) |
||
| 302 | } |
||
| 303 | |||
| 304 | protected function doTarExtract($options, ResourceInterface $resource, $to = null) |
||
| 305 | { |
||
| 306 | if (null !== $to && !is_dir($to)) { |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param array $options |
||
| 350 | * @param ResourceInterface $resource |
||
| 351 | * @param array $members |
||
| 352 | * @param string $to |
||
| 353 | * @param bool $overwrite |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | protected function doTarExtractMembers($options, ResourceInterface $resource, $members, $to = null, $overwrite = false) |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Returns an array of option for the listMembers command |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | abstract protected function getListMembersOptions(); |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Returns an array of option for the extract command |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | abstract protected function getExtractOptions(); |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns an array of option for the extractMembers command |
||
| 427 | * |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | abstract protected function getExtractMembersOptions(); |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Gets adapter specific additional options |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | abstract protected function getLocalOptions(); |
||
| 438 | } |
||
| 439 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: