| Conditions | 8 |
| Paths | 12 |
| Total Lines | 56 |
| Code Lines | 35 |
| 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 |
||
| 32 | public static function dockerComposeServiceSerialize(Service $service, array $envFileNames = [], string $version = self::VERSION): array |
||
| 33 | { |
||
| 34 | $portMap = function (array $port): string { |
||
| 35 | return $port['source'] . ':' . $port['target']; |
||
| 36 | }; |
||
| 37 | $envMapDockerCompose = self::getEnvironmentVariablesForDockerCompose($service); |
||
| 38 | $envMap = function (EnvVariable $e) { |
||
| 39 | if ($e->getComment() !== null) { |
||
| 40 | return new CommentedItem($e->getValue(), $e->getComment()); |
||
| 41 | } |
||
| 42 | return $e->getValue(); |
||
| 43 | }; |
||
| 44 | /** |
||
| 45 | * @param NamedVolume|BindVolume|TmpfsVolume $v |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | $volumeMap = function ($v): array { |
||
| 49 | $array = [ |
||
| 50 | 'type' => $v->getType(), |
||
| 51 | 'source' => $v->getSource(), |
||
| 52 | ]; |
||
| 53 | if ($v instanceof NamedVolume || $v instanceof BindVolume) { |
||
| 54 | $array['target'] = $v->getTarget(); |
||
| 55 | $array['read_only'] = $v->isReadOnly(); |
||
| 56 | } |
||
| 57 | return $array; |
||
| 58 | }; |
||
| 59 | $dockerService = [ |
||
| 60 | 'version' => $version, |
||
| 61 | 'services' => [ |
||
| 62 | $service->getServiceName() => array_filter([ |
||
| 63 | 'image' => $service->getImage(), |
||
| 64 | 'command' => $service->getCommand(), |
||
| 65 | 'depends_on' => $service->getDependsOn(), |
||
| 66 | 'ports' => \array_map($portMap, $service->getPorts()), |
||
| 67 | 'labels' => $service->getLabels(), |
||
| 68 | 'environment' => \array_map($envMap, $envMapDockerCompose), |
||
| 69 | 'volumes' => \array_map($volumeMap, $service->getVolumes()), |
||
| 70 | ]), |
||
| 71 | ], |
||
| 72 | ]; |
||
| 73 | if ($envFileNames) { |
||
| 74 | $dockerService['services'][$service->getServiceName()]['env_file'] = $envFileNames; |
||
| 75 | } |
||
| 76 | $namedVolumes = array(); |
||
| 77 | /** @var Volume $volume */ |
||
| 78 | foreach ($service->getVolumes() as $volume) { |
||
| 79 | if ($volume->getType() === VolumeTypeEnum::NAMED_VOLUME) { |
||
| 80 | // for now we just add them without any option |
||
| 81 | $namedVolumes[$volume->getSource()] = null; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | if (!empty($namedVolumes)) { |
||
| 85 | $dockerService['volumes'] = $namedVolumes; |
||
| 86 | } |
||
| 87 | return $dockerService; |
||
| 88 | } |
||
| 188 |