| Conditions | 6 |
| Paths | 6 |
| Total Lines | 52 |
| Code Lines | 32 |
| 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 |
||
| 105 | public static function dockerComposeServiceSerialize(Service $service, string $version = self::VERSION): array |
||
| 106 | { |
||
| 107 | $portMap = function (array $port): string { |
||
| 108 | return $port['source'] . ':' . $port['target']; |
||
| 109 | }; |
||
| 110 | $labelMap = function (array $label): string { |
||
| 111 | return $label['value']; |
||
| 112 | }; |
||
| 113 | $envMap = function (EnvVariable $e): string { |
||
| 114 | return $e->getValue(); |
||
| 115 | }; |
||
| 116 | /** |
||
| 117 | * @param NamedVolume|BindVolume|TmpfsVolume $v |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | $volumeMap = function ($v): array { |
||
| 121 | $array = [ |
||
| 122 | 'type' => $v->getType(), |
||
| 123 | 'source' => $v->getSource(), |
||
| 124 | ]; |
||
| 125 | if ($v instanceof NamedVolume || $v instanceof BindVolume) { |
||
| 126 | $array['target'] = $v->getTarget(); |
||
| 127 | $array['read_only'] = $v->isReadOnly(); |
||
| 128 | } |
||
| 129 | return $array; |
||
| 130 | }; |
||
| 131 | $dockerService = [ |
||
| 132 | 'version' => $version, |
||
| 133 | 'services' => [ |
||
| 134 | $service->getServiceName() => array_filter([ |
||
| 135 | 'image' => $service->getImage(), |
||
| 136 | 'command' => $service->getCommand(), |
||
| 137 | 'depends_on' => $service->getDependsOn(), |
||
| 138 | 'ports' => array_map($portMap, $service->getPorts()), |
||
| 139 | 'labels' => array_map($labelMap, $service->getLabels()), |
||
| 140 | 'environment' => array_map($envMap, $service->getEnvironment()), |
||
| 141 | 'volumes' => array_map($volumeMap, $service->getVolumes()), |
||
| 142 | ]), |
||
| 143 | ], |
||
| 144 | ]; |
||
| 145 | $namedVolumes = array(); |
||
| 146 | /** @var Volume $volume */ |
||
| 147 | foreach ($service->getVolumes() as $volume) { |
||
| 148 | if ($volume->getType() === VolumeTypeEnum::NAMED_VOLUME) { |
||
| 149 | // for now we just add them without any option |
||
| 150 | $namedVolumes[$volume->getSource()] = null; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | if (!empty($namedVolumes)) { |
||
| 154 | $dockerService['volumes'] = $namedVolumes; |
||
| 155 | } |
||
| 156 | return $dockerService; |
||
| 157 | } |
||
| 171 |