Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
28 | class RootModuleFileConverter extends ModuleFileConverter |
||
29 | { |
||
30 | /** |
||
31 | * The default order of the keys in the written module file. |
||
32 | * |
||
33 | * @var string[] |
||
34 | */ |
||
35 | private static $keyOrder = array( |
||
36 | '$schema', |
||
37 | 'name', |
||
38 | 'resources', |
||
39 | 'bindings', |
||
40 | 'binding-types', |
||
41 | 'depend', |
||
42 | 'order', |
||
43 | 'config', |
||
44 | 'plugins', |
||
45 | 'extra', |
||
46 | 'modules', |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 6 | public function toJson($moduleFile, array $options = array()) |
|
53 | { |
||
54 | 6 | Assert::isInstanceOf($moduleFile, 'Puli\Manager\Api\Module\RootModuleFile'); |
|
55 | |||
56 | 6 | $jsonData = new stdClass(); |
|
57 | 6 | $jsonData->{'$schema'} = sprintf(self::SCHEMA, self::VERSION); |
|
58 | |||
59 | 6 | $this->addModuleFileToJson($moduleFile, $jsonData); |
|
60 | 6 | $this->addRootModuleFileToJson($moduleFile, $jsonData); |
|
61 | |||
62 | // Sort according to key order |
||
63 | 6 | $jsonArray = (array) $jsonData; |
|
64 | 6 | $orderedKeys = array_intersect_key(array_flip(self::$keyOrder), $jsonArray); |
|
65 | |||
66 | 6 | return (object) array_replace($orderedKeys, $jsonArray); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 30 | public function fromJson($jsonData, array $options = array()) |
|
89 | |||
90 | 6 | protected function addRootModuleFileToJson(RootModuleFile $moduleFile, stdClass $jsonData) |
|
91 | { |
||
92 | 6 | $moduleOrder = $moduleFile->getModuleOrder(); |
|
93 | 6 | $installInfos = $moduleFile->getInstallInfos(); |
|
94 | |||
95 | // Pass false to exclude base configuration values |
||
96 | 6 | $configValues = $moduleFile->getConfig()->toRawArray(false); |
|
97 | |||
98 | 6 | if (count($moduleOrder) > 0) { |
|
99 | 1 | $jsonData->order = $moduleOrder; |
|
100 | } |
||
101 | |||
102 | 6 | if (count($configValues) > 0) { |
|
103 | 1 | $jsonData->config = (object) $configValues; |
|
104 | } |
||
105 | |||
106 | 6 | if (array() !== $moduleFile->getPluginClasses()) { |
|
107 | 2 | $jsonData->plugins = $moduleFile->getPluginClasses(); |
|
108 | |||
109 | 2 | sort($jsonData->plugins); |
|
110 | } |
||
111 | |||
112 | 6 | if (count($installInfos) > 0) { |
|
113 | 3 | $modulesData = array(); |
|
114 | |||
115 | 3 | foreach ($installInfos as $installInfo) { |
|
116 | 3 | $installData = new stdClass(); |
|
117 | 3 | $installData->{'install-path'} = $installInfo->getInstallPath(); |
|
118 | |||
119 | 3 | if (InstallInfo::DEFAULT_INSTALLER_NAME !== $installInfo->getInstallerName()) { |
|
120 | 1 | $installData->installer = $installInfo->getInstallerName(); |
|
121 | } |
||
122 | |||
123 | 3 | if ($installInfo->hasDisabledBindingUuids()) { |
|
124 | 2 | $installData->{'disabled-bindings'} = array(); |
|
125 | |||
126 | 2 | foreach ($installInfo->getDisabledBindingUuids() as $uuid) { |
|
127 | 2 | $installData->{'disabled-bindings'}[] = $uuid->toString(); |
|
128 | } |
||
129 | |||
130 | 2 | sort($installData->{'disabled-bindings'}); |
|
131 | } |
||
132 | |||
133 | 3 | if (Environment::PROD !== $installInfo->getEnvironment()) { |
|
134 | 1 | $installData->env = $installInfo->getEnvironment(); |
|
135 | } |
||
136 | |||
137 | 3 | $modulesData[$installInfo->getModuleName()] = $installData; |
|
138 | } |
||
139 | |||
140 | 3 | ksort($modulesData); |
|
141 | |||
142 | 3 | $jsonData->modules = (object) $modulesData; |
|
143 | } |
||
144 | 6 | } |
|
145 | |||
146 | 30 | protected function addJsonToRootModuleFile(stdClass $jsonData, RootModuleFile $moduleFile) |
|
147 | { |
||
148 | 30 | if (isset($jsonData->order)) { |
|
149 | 1 | $moduleFile->setModuleOrder((array) $jsonData->order); |
|
150 | } |
||
151 | |||
152 | 30 | if (isset($jsonData->plugins)) { |
|
153 | 27 | $moduleFile->setPluginClasses($jsonData->plugins); |
|
154 | } |
||
155 | |||
156 | 30 | if (isset($jsonData->config)) { |
|
157 | 2 | $config = $moduleFile->getConfig(); |
|
158 | |||
159 | 2 | foreach ($this->objectsToArrays($jsonData->config) as $key => $value) { |
|
160 | 2 | $config->set($key, $value); |
|
161 | } |
||
162 | } |
||
163 | |||
164 | 30 | if (isset($jsonData->modules)) { |
|
165 | 27 | foreach ($jsonData->modules as $moduleName => $moduleData) { |
|
166 | 27 | $installInfo = new InstallInfo($moduleName, $moduleData->{'install-path'}); |
|
167 | |||
168 | 27 | if (isset($moduleData->env)) { |
|
169 | 1 | $installInfo->setEnvironment($moduleData->env); |
|
170 | } |
||
171 | |||
172 | 27 | if (isset($moduleData->installer)) { |
|
173 | 1 | $installInfo->setInstallerName($moduleData->installer); |
|
174 | } |
||
175 | |||
176 | 27 | if (isset($moduleData->{'disabled-bindings'})) { |
|
177 | 1 | foreach ($moduleData->{'disabled-bindings'} as $uuid) { |
|
178 | 1 | $installInfo->addDisabledBindingUuid(Uuid::fromString($uuid)); |
|
179 | } |
||
180 | } |
||
181 | |||
182 | 27 | $moduleFile->addInstallInfo($installInfo); |
|
183 | } |
||
184 | } |
||
185 | 30 | } |
|
186 | |||
187 | 2 | View Code Duplication | private function objectsToArrays($data) |
197 | } |
||
198 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.