Conditions | 22 |
Paths | 1190 |
Total Lines | 98 |
Lines | 18 |
Ratio | 18.37 % |
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 |
||
77 | public function analyse($appId) { |
||
78 | $appPath = $this->appManager->getAppPath($appId); |
||
79 | if ($appPath === false) { |
||
80 | throw new \RuntimeException("No app with given id <$appId> known."); |
||
81 | } |
||
82 | |||
83 | $errors = []; |
||
84 | |||
85 | try { |
||
86 | $info = $this->infoParser->parse($appPath . '/appinfo/info.xml'); |
||
87 | } catch (\Exception $e) { |
||
88 | $this->emit('InfoChecker', 'invalidAppInfo', [$appId]); |
||
89 | return [ |
||
90 | [ |
||
91 | 'type' => 'invalidAppInfo', |
||
92 | 'message' => "App <$appId> has invalid XML in appinfo.xml", |
||
93 | ] |
||
94 | ]; |
||
95 | } |
||
96 | |||
97 | View Code Duplication | if (isset($info['dependencies']['owncloud']['@attributes']['min-version']) && ($info['requiremin'] || $info['require'])) { |
|
|
|||
98 | $this->emit('InfoChecker', 'duplicateRequirement', ['min']); |
||
99 | $errors[] = [ |
||
100 | 'type' => 'duplicateRequirement', |
||
101 | 'field' => 'min', |
||
102 | ]; |
||
103 | } elseif (!isset($info['dependencies']['owncloud']['@attributes']['min-version'])) { |
||
104 | $this->emit('InfoChecker', 'missingRequirement', ['min']); |
||
105 | } |
||
106 | |||
107 | View Code Duplication | if (isset($info['dependencies']['owncloud']['@attributes']['max-version']) && $info['requiremax']) { |
|
108 | $this->emit('InfoChecker', 'duplicateRequirement', ['max']); |
||
109 | $errors[] = [ |
||
110 | 'type' => 'duplicateRequirement', |
||
111 | 'field' => 'max', |
||
112 | ]; |
||
113 | } elseif (!isset($info['dependencies']['owncloud']['@attributes']['max-version'])) { |
||
114 | $this->emit('InfoChecker', 'missingRequirement', ['max']); |
||
115 | } |
||
116 | |||
117 | foreach ($info as $key => $value) { |
||
118 | if (\is_array($value)) { |
||
119 | $value = \json_encode($value); |
||
120 | } |
||
121 | if (\in_array($key, $this->mandatoryFields)) { |
||
122 | $this->emit('InfoChecker', 'mandatoryFieldFound', [$key, $value]); |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | if (\in_array($key, $this->optionalFields)) { |
||
127 | $this->emit('InfoChecker', 'optionalFieldFound', [$key, $value]); |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | if (\in_array($key, $this->deprecatedFields)) { |
||
132 | // skip empty arrays - empty arrays for remote and public are always added |
||
133 | if ($value === '[]' && \in_array($key, ['public', 'remote', 'info'])) { |
||
134 | continue; |
||
135 | } |
||
136 | $this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]); |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | $this->emit('InfoChecker', 'unusedFieldFound', [$key, $value]); |
||
141 | } |
||
142 | |||
143 | foreach ($this->mandatoryFields as $key) { |
||
144 | if (!isset($info[$key])) { |
||
145 | $this->emit('InfoChecker', 'mandatoryFieldMissing', [$key]); |
||
146 | $errors[] = [ |
||
147 | 'type' => 'mandatoryFieldMissing', |
||
148 | 'field' => $key, |
||
149 | ]; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $versionFile = $appPath . '/appinfo/version'; |
||
154 | if (\is_file($versionFile)) { |
||
155 | $version = \trim(\file_get_contents($versionFile)); |
||
156 | if (isset($info['version'])) { |
||
157 | if ($info['version'] !== $version) { |
||
158 | $this->emit('InfoChecker', 'differentVersions', |
||
159 | [$version, $info['version']]); |
||
160 | $errors[] = [ |
||
161 | 'type' => 'differentVersions', |
||
162 | 'message' => 'appinfo/version: ' . $version . |
||
163 | ' - appinfo/info.xml: ' . $info['version'], |
||
164 | ]; |
||
165 | } else { |
||
166 | $this->emit('InfoChecker', 'sameVersions', [$versionFile]); |
||
167 | } |
||
168 | } else { |
||
169 | $this->emit('InfoChecker', 'migrateVersion', [$version]); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return $errors; |
||
174 | } |
||
175 | } |
||
176 |
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.