| Total Complexity | 44 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ShellBuilder 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 ShellBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | final class ShellBuilder implements ShellInterface, \JsonSerializable |
||
| 18 | { |
||
| 19 | /** @var array<ShellInterface|CollectionTuple> */ |
||
| 20 | private $commandList = []; |
||
| 21 | /** @var int */ |
||
| 22 | private $groupType; |
||
| 23 | /** |
||
| 24 | * name of the coprocess - empty string means anonymous |
||
| 25 | * @var null|string |
||
| 26 | */ |
||
| 27 | private $asynchronously; |
||
| 28 | /** @var bool */ |
||
| 29 | private $processSubstitution = false; |
||
| 30 | /** @var bool */ |
||
| 31 | private $commandSubstitution = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * This is a shortcut for quicker fluid access to the shell builder |
||
| 35 | * @return static |
||
| 36 | */ |
||
| 37 | public static function new(): self |
||
| 38 | { |
||
| 39 | return new ShellBuilder(); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This is a shortcut for quicker fluid access to the command api |
||
| 44 | * @param string $executable |
||
| 45 | * @return ShellCommand |
||
| 46 | */ |
||
| 47 | public static function command(string $executable): ShellCommand |
||
| 48 | { |
||
| 49 | return new ShellCommand($executable, new self()); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function __construct(int $groupType = GroupType::NO_GROUP) |
||
| 53 | { |
||
| 54 | $this->groupType = $groupType; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function createCommand(string $name, bool $withNewBuilder = false): ShellCommand |
||
| 58 | { |
||
| 59 | return new ShellCommand($name, $withNewBuilder ? new self() : $this); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function runAsynchronously(bool $isAsync = true, string $name = ''): self |
||
| 63 | { |
||
| 64 | $this->asynchronously = $isAsync ? $name : null; |
||
| 65 | return $this; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string|ShellInterface $command |
||
| 70 | * @return $this |
||
| 71 | * @throws ShellBuilderException |
||
| 72 | */ |
||
| 73 | public function add($command): self |
||
| 74 | { |
||
| 75 | $command = $this->parseCommand($command, true); |
||
| 76 | if (empty($this->commandList)) { |
||
| 77 | $this->commandList[] = $command; |
||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | $this->commandList[] = ShellList::add($command); |
||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string|ShellInterface $command |
||
| 86 | * @return $this |
||
| 87 | * @throws ShellBuilderException |
||
| 88 | */ |
||
| 89 | public function and($command): self |
||
| 90 | { |
||
| 91 | $this->commandList[] = ShellList::addAnd($this->parseCommand($command)); |
||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string|ShellInterface $command |
||
| 97 | * @return $this |
||
| 98 | * @throws ShellBuilderException |
||
| 99 | */ |
||
| 100 | public function or($command): self |
||
| 101 | { |
||
| 102 | $this->commandList[] = ShellList::addOr($this->parseCommand($command)); |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string|ShellInterface $command |
||
| 108 | * @return $this |
||
| 109 | * @throws ShellBuilderException |
||
| 110 | */ |
||
| 111 | public function async($command = ''): self |
||
| 112 | { |
||
| 113 | $this->commandList[] = ShellList::async($this->parseCommand($command)); |
||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string|ShellInterface $command |
||
| 119 | * @return $this |
||
| 120 | * @throws ShellBuilderException |
||
| 121 | */ |
||
| 122 | public function pipe($command): self |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string|ShellInterface $command |
||
| 130 | * @return $this |
||
| 131 | * @throws ShellBuilderException |
||
| 132 | */ |
||
| 133 | public function pipeWithForward($command): self |
||
| 134 | { |
||
| 135 | $this->commandList[] = Pipeline::pipeErrorForward($this->parseCommand($command)); |
||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string|ShellInterface $command |
||
| 141 | * @param bool $append |
||
| 142 | * @return $this |
||
| 143 | * @throws ShellBuilderException |
||
| 144 | */ |
||
| 145 | public function redirectOutput($command, bool $append = false): self |
||
| 146 | { |
||
| 147 | $command = $this->parseCommand($command); |
||
| 148 | $this->commandList[] = Redirection::redirectOutput($command, $append); |
||
| 149 | return $this; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string|ShellInterface $command |
||
| 154 | * @return $this |
||
| 155 | * @throws ShellBuilderException |
||
| 156 | */ |
||
| 157 | public function redirectInput($command): self |
||
| 158 | { |
||
| 159 | $command = $this->parseCommand($command); |
||
| 160 | $this->commandList[] = Redirection::redirectInput($command); |
||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string|ShellInterface $command |
||
| 166 | * @return $this |
||
| 167 | * @throws ShellBuilderException |
||
| 168 | */ |
||
| 169 | public function redirectError($command): self |
||
| 170 | { |
||
| 171 | $command = $this->parseCommand($command); |
||
| 172 | $this->commandList[] = Redirection::redirectError($command); |
||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string|ShellInterface $command |
||
| 178 | * @param bool $toLeft |
||
| 179 | * @return $this |
||
| 180 | * @throws ShellBuilderException |
||
| 181 | */ |
||
| 182 | public function redirect($command, bool $toLeft = true): self |
||
| 183 | { |
||
| 184 | $command = $this->parseCommand($command); |
||
| 185 | $this->commandList[] = Redirection::redirectBetweenFiles($command, $toLeft); |
||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param ShellInterface|string $command |
||
| 191 | * @param bool $toLeft |
||
| 192 | * @param null|int $firstDescriptor |
||
| 193 | * @param null|int $secondDescriptor |
||
| 194 | * @return static |
||
| 195 | * @throws ShellBuilderException |
||
| 196 | */ |
||
| 197 | public function redirectDescriptor($command, bool $toLeft, int $firstDescriptor = null, int $secondDescriptor = null): self |
||
| 198 | { |
||
| 199 | $command = $this->parseCommand($command); |
||
| 200 | $this->commandList[] = Redirection::redirectBetweenDescriptors($command, $toLeft, $firstDescriptor, $secondDescriptor); |
||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function redirectErrorToOutput(): self |
||
| 205 | { |
||
| 206 | $this->commandList[] = Redirection::redirectErrorToOutput(); |
||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function addCondition(BasicExpression $condition): self |
||
| 211 | { |
||
| 212 | $this->commandList[] = $condition; |
||
| 213 | return $this; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string|ShellInterface $fileEnding |
||
| 218 | * @return ShellBuilder |
||
| 219 | * @throws ShellBuilderException |
||
| 220 | */ |
||
| 221 | public function addFileEnding($fileEnding): self |
||
| 222 | { |
||
| 223 | $tuple = CollectionTuple::create($fileEnding, '.'); |
||
| 224 | $tuple |
||
| 225 | ->noSpaceAfterJoin(true) |
||
| 226 | ->noSpaceBeforeJoin(true); |
||
| 227 | $this->commandList[] = $tuple; |
||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function createGroup(bool $inSameShell = false): self |
||
| 232 | { |
||
| 233 | return new self($inSameShell ? GroupType::SAMESHELL_GROUP : GroupType::SUBSHELL_GROUP); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function createProcessSubstition(): self |
||
| 237 | { |
||
| 238 | $builder = new self(GroupType::SUBSHELL_GROUP); |
||
| 239 | $builder->processSubstitution = true; |
||
| 240 | return $builder; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function createCommandSubstition(): self |
||
| 244 | { |
||
| 245 | $builder = new self(GroupType::SUBSHELL_GROUP); |
||
| 246 | $builder->commandSubstitution = true; |
||
| 247 | return $builder; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string|ShellInterface $command |
||
| 252 | * @param bool $allowEmpty |
||
| 253 | * @return ShellInterface |
||
| 254 | * @throws ShellBuilderException |
||
| 255 | */ |
||
| 256 | private function parseCommand($command, bool $allowEmpty = false): ShellInterface |
||
| 257 | { |
||
| 258 | if (is_string($command)) { |
||
| 259 | $command = $this->createCommand($command); |
||
| 260 | } |
||
| 261 | try { |
||
| 262 | $this->validateCommand($command, $allowEmpty); |
||
| 263 | } catch (TypeError $typeError) { |
||
| 264 | throw new ShellBuilderException('Provided the wrong type - only ShellCommand and ShellBuilder allowed'); |
||
| 265 | } |
||
| 266 | return $command; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** @noinspection PhpUnusedParameterInspection */ |
||
| 270 | private function validateCommand(ShellInterface $command, bool $allowEmpty): void |
||
|
|
|||
| 271 | { |
||
| 272 | if (!$allowEmpty && empty($this->commandList)) { |
||
| 273 | throw new ShellBuilderException('You have to first add a command before you can combine it'); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | public function jsonSerialize(): array |
||
| 278 | { |
||
| 279 | return $this->__toArray(); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return array<mixed> |
||
| 284 | */ |
||
| 285 | public function __toArray(): array |
||
| 286 | { |
||
| 287 | $commands = []; |
||
| 288 | foreach ($this->commandList as $item) { |
||
| 289 | $commands[] = $item instanceof ShellInterface ? $item->__toArray() : $item; |
||
| 290 | } |
||
| 291 | return $commands; |
||
| 292 | } |
||
| 293 | |||
| 294 | public function __toString(): string |
||
| 328 | } |
||
| 329 | } |
||
| 330 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.