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 |
||
26 | public static function dockerComposeServiceSerialize(Service $service, string $version = self::VERSION): array |
||
27 | { |
||
28 | $portMap = function (array $port): string { |
||
29 | return $port['source'] . ':' . $port['target']; |
||
30 | }; |
||
31 | $labelMap = function (array $label): string { |
||
32 | return $label['value']; |
||
33 | }; |
||
34 | $envMap = function (EnvVariable $e): string { |
||
35 | return $e->getValue(); |
||
36 | }; |
||
37 | /** |
||
38 | * @param NamedVolume|BindVolume|TmpfsVolume $v |
||
39 | * @return array |
||
40 | */ |
||
41 | $volumeMap = function ($v): array { |
||
42 | $array = [ |
||
43 | 'type' => $v->getType(), |
||
44 | 'source' => $v->getSource(), |
||
45 | ]; |
||
46 | if ($v instanceof NamedVolume || $v instanceof BindVolume) { |
||
47 | $array['target'] = $v->getTarget(); |
||
48 | $array['read_only'] = $v->isReadOnly(); |
||
49 | } |
||
50 | return $array; |
||
51 | }; |
||
52 | $dockerService = [ |
||
53 | 'version' => $version, |
||
54 | 'services' => [ |
||
55 | $service->getServiceName() => array_filter([ |
||
56 | 'image' => $service->getImage(), |
||
57 | 'command' => $service->getCommand(), |
||
58 | 'depends_on' => $service->getDependsOn(), |
||
59 | 'ports' => array_map($portMap, $service->getPorts()), |
||
60 | 'labels' => array_map($labelMap, $service->getLabels()), |
||
61 | 'environment' => array_map($envMap, $service->getEnvironment()), |
||
62 | 'volumes' => array_map($volumeMap, $service->getVolumes()), |
||
63 | ]), |
||
64 | ], |
||
65 | ]; |
||
66 | $namedVolumes = array(); |
||
67 | /** @var Volume $volume */ |
||
68 | foreach ($service->getVolumes() as $volume) { |
||
69 | if ($volume->getType() === VolumeTypeEnum::NAMED_VOLUME) { |
||
70 | // for now we just add them without any option |
||
71 | $namedVolumes[$volume->getSource()] = null; |
||
72 | } |
||
73 | } |
||
74 | if (!empty($namedVolumes)) { |
||
75 | $dockerService['volumes'] = $namedVolumes; |
||
76 | } |
||
77 | return $dockerService; |
||
78 | } |
||
145 |