Conditions | 29 |
Paths | 2004 |
Total Lines | 156 |
Code Lines | 102 |
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 |
||
99 | public function init($tag, $attrs) |
||
100 | { |
||
101 | $name = null; |
||
102 | $depends = ''; |
||
103 | $extensionPoint = null; //'fail'; |
||
104 | $extensionPointMissing = null; |
||
105 | $ifCond = null; |
||
106 | $unlessCond = null; |
||
107 | $id = null; |
||
108 | $description = null; |
||
109 | $isHidden = false; |
||
110 | $logskipped = false; |
||
111 | |||
112 | foreach ($attrs as $key => $value) { |
||
113 | switch ($key) { |
||
114 | case 'name': |
||
115 | $name = (string) $value; |
||
116 | |||
117 | break; |
||
118 | |||
119 | case 'depends': |
||
120 | $depends = (string) $value; |
||
121 | |||
122 | break; |
||
123 | |||
124 | case 'if': |
||
125 | $ifCond = (string) $value; |
||
126 | |||
127 | break; |
||
128 | |||
129 | case 'unless': |
||
130 | $unlessCond = (string) $value; |
||
131 | |||
132 | break; |
||
133 | |||
134 | case 'id': |
||
135 | $id = (string) $value; |
||
136 | |||
137 | break; |
||
138 | |||
139 | case 'hidden': |
||
140 | $isHidden = StringHelper::booleanValue($value); |
||
141 | |||
142 | break; |
||
143 | |||
144 | case 'description': |
||
145 | $description = (string) $value; |
||
146 | |||
147 | break; |
||
148 | |||
149 | case 'logskipped': |
||
150 | $logskipped = $value; |
||
151 | |||
152 | break; |
||
153 | |||
154 | case 'extensionof': |
||
155 | $extensionPoint = $value; |
||
156 | |||
157 | break; |
||
158 | |||
159 | case 'onmissingextensionpoint': |
||
160 | if (!in_array($value, ['fail', 'warn', 'ignore'], true)) { |
||
161 | throw new BuildException('Invalid onMissingExtensionPoint ' . $value); |
||
162 | } |
||
163 | $extensionPointMissing = $value; |
||
164 | |||
165 | break; |
||
166 | |||
167 | default: |
||
168 | throw new ExpatParseException("Unexpected attribute '{$key}'", $this->parser->getLocation()); |
||
|
|||
169 | } |
||
170 | } |
||
171 | |||
172 | if (null === $name) { |
||
173 | throw new ExpatParseException( |
||
174 | 'target element appears without a name attribute', |
||
175 | $this->parser->getLocation() |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | // shorthand |
||
180 | $project = $this->configurator->project; |
||
181 | |||
182 | // check to see if this target is a dup within the same file |
||
183 | if (isset($this->context->getCurrentTargets()[$name])) { |
||
184 | throw new BuildException( |
||
185 | "Duplicate target: {$name}", |
||
186 | $this->parser->getLocation() |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | $this->target = 'target' === $tag ? new Target() : new ExtensionPoint(); |
||
191 | $this->target->setProject($project); |
||
192 | $this->target->setLocation($this->parser->getLocation()); |
||
193 | $this->target->setHidden($isHidden); |
||
194 | $this->target->setIf($ifCond); |
||
195 | $this->target->setUnless($unlessCond); |
||
196 | $this->target->setDescription($description); |
||
197 | $this->target->setLogSkipped(StringHelper::booleanValue($logskipped)); |
||
198 | // take care of dependencies |
||
199 | if ('' !== $depends) { |
||
200 | $this->target->setDepends($depends); |
||
201 | } |
||
202 | |||
203 | // check to see if target with same name is already defined |
||
204 | $projectTargets = $project->getTargets(); |
||
205 | if (isset($projectTargets[$name])) { |
||
206 | if ( |
||
207 | $this->configurator->isIgnoringProjectTag() |
||
208 | && null != $this->configurator->getCurrentProjectName() |
||
209 | && 0 != strlen($this->configurator->getCurrentProjectName()) |
||
210 | ) { |
||
211 | // In an impored file (and not completely |
||
212 | // ignoring the project tag) |
||
213 | $newName = $this->configurator->getCurrentProjectName() . '.' . $name; |
||
214 | $project->log( |
||
215 | 'Already defined in main or a previous import, ' . |
||
216 | "define {$name} as {$newName}", |
||
217 | Project::MSG_VERBOSE |
||
218 | ); |
||
219 | $name = $newName; |
||
220 | } else { |
||
221 | $project->log( |
||
222 | 'Already defined in main or a previous import, ' . |
||
223 | "ignore {$name}", |
||
224 | Project::MSG_VERBOSE |
||
225 | ); |
||
226 | $name = null; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | if (null !== $name) { |
||
231 | $this->target->setName($name); |
||
232 | $project->addOrReplaceTarget($name, $this->target); |
||
233 | if (null !== $id && '' !== $id) { |
||
234 | $project->addReference($id, $this->target); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | if (null !== $extensionPointMissing && null === $extensionPoint) { |
||
239 | throw new BuildException( |
||
240 | 'onMissingExtensionPoint attribute cannot ' . |
||
241 | 'be specified unless extensionOf is specified', |
||
242 | $this->target->getLocation() |
||
243 | ); |
||
244 | } |
||
245 | if (null !== $extensionPoint) { |
||
246 | foreach (Target::parseDepends($extensionPoint, $name, 'extensionof') as $extPointName) { |
||
247 | if (null === $extensionPointMissing) { |
||
248 | $extensionPointMissing = 'fail'; |
||
249 | } |
||
250 | $this->context->addExtensionPoint([ |
||
251 | $extPointName, |
||
252 | $this->target->getName(), |
||
253 | $extensionPointMissing, |
||
254 | null, |
||
255 | ]); |
||
288 |