| Conditions | 12 |
| Paths | 111 |
| Total Lines | 79 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public static function parsePayload(string $payload, OutputInterface $output): array |
||
| 30 | { |
||
| 31 | $p = json_decode($payload, true); |
||
| 32 | if (!$p) { |
||
| 33 | $output->writeln(" ⨯ payload error: invalid payload"); |
||
| 34 | exit(1); |
||
| 35 | } |
||
| 36 | |||
| 37 | $serviceName = $p[Cst::SERVICE_NAME_KEY] ?? ""; |
||
| 38 | if (empty($serviceName)) { |
||
| 39 | $output->writeln(" ⨯ payload error: empty " . Cst::SERVICE_NAME_KEY); |
||
| 40 | exit(1); |
||
| 41 | } |
||
| 42 | |||
| 43 | $service = $p['service'] ?? array(); |
||
| 44 | if (!empty($service)) { |
||
| 45 | $image = $service['image'] ?? ""; |
||
| 46 | $dependsOn = $service['dependsOn'] ?? ""; |
||
| 47 | |||
| 48 | $ports = array(); |
||
| 49 | foreach ($service['ports'] ?? array() as $port) { |
||
| 50 | $str = sprintf("%d:%d", $port['source'], $port['target']); |
||
| 51 | $ports[] = $str; |
||
| 52 | } |
||
| 53 | $labels = array(); |
||
| 54 | foreach ($service['labels'] ?? array() as $label) { |
||
| 55 | $str = $label['value'] ? sprintf("%s=%s", $label['key'], $label['value']) : $label['key']; |
||
| 56 | $labels[] = $str; |
||
| 57 | } |
||
| 58 | $environments = array(); |
||
| 59 | foreach ($service['environments'] ?? array() as $env) { |
||
| 60 | $str = $env['value'] ? sprintf("%s=%s", $env['key'], $env['value']) : $env['key']; |
||
| 61 | $environments[] = $str; |
||
| 62 | } |
||
| 63 | |||
| 64 | $volumes = $p['volumes'] ?? array(); |
||
| 65 | $namedVolumes = array(); |
||
| 66 | foreach ($service['volumes'] ?? array() as $volume) { |
||
| 67 | $type = $volume['type'] ?? ""; |
||
| 68 | $source = $volume['source'] ?? ""; |
||
| 69 | |||
| 70 | $formattedVolume = array( |
||
| 71 | "type" => $type, |
||
| 72 | "source" => $source, |
||
| 73 | "target" => $volume['target'] ?? "", |
||
| 74 | "read_only" => $volume['readOnly'] ?? "", |
||
| 75 | ); |
||
| 76 | $volumes[] = $formattedVolume; |
||
| 77 | |||
| 78 | // case it's a named volume |
||
| 79 | if ($type === "volume") { |
||
| 80 | // for now we just add them without any option |
||
| 81 | $namedVolumes[$source] = null; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $formattedPayload = array( |
||
| 86 | "services" => Utils::arrayFilterRec(array( |
||
| 87 | $serviceName => array( |
||
| 88 | "image" => $image, |
||
| 89 | "depends_on" => $dependsOn, |
||
| 90 | "ports" => $ports, |
||
| 91 | "labels" => $labels, |
||
| 92 | "environments" => $environments, |
||
| 93 | "volumes" => $volumes, |
||
| 94 | ), |
||
| 95 | )), |
||
| 96 | ); |
||
| 97 | if (!empty($namedVolumes)) { |
||
| 98 | $formattedPayload['volumes'] = $namedVolumes; |
||
| 99 | } |
||
| 100 | |||
| 101 | return $formattedPayload; |
||
| 102 | } else { |
||
| 103 | $output->writeln(" ⨯ payload error: empty " . Cst::SERVICE_KEY); |
||
| 104 | exit(1); |
||
| 105 | } |
||
| 106 | |||
| 107 | exit(1); |
||
| 108 | } |
||
| 132 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.