Total Complexity | 56 |
Total Lines | 423 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ForeachTask often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ForeachTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class ForeachTask extends Task |
||
58 | { |
||
59 | use ResourceAware; |
||
60 | |||
61 | /** |
||
62 | * Delimter-separated list of values to process. |
||
63 | */ |
||
64 | private $list; |
||
65 | |||
66 | /** |
||
67 | * Name of parameter to pass to callee. |
||
68 | */ |
||
69 | private $param; |
||
70 | |||
71 | /** |
||
72 | * @var PropertyTask[] |
||
73 | */ |
||
74 | private $params = []; |
||
75 | |||
76 | /** |
||
77 | * Name of absolute path parameter to pass to callee. |
||
78 | */ |
||
79 | private $absparam; |
||
80 | |||
81 | /** |
||
82 | * Delimiter that separates items in $list. |
||
83 | */ |
||
84 | private $delimiter = ','; |
||
85 | |||
86 | /** |
||
87 | * PhingCallTask that will be invoked w/ calleeTarget. |
||
88 | * |
||
89 | * @var PhingCallTask |
||
90 | */ |
||
91 | private $callee; |
||
92 | |||
93 | /** |
||
94 | * @var Mapper |
||
95 | */ |
||
96 | private $mapperElement; |
||
97 | |||
98 | /** |
||
99 | * Target to execute. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | private $calleeTarget; |
||
104 | |||
105 | /** |
||
106 | * Total number of files processed. |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | private $total_files = 0; |
||
111 | |||
112 | /** |
||
113 | * Total number of directories processed. |
||
114 | * |
||
115 | * @var int |
||
116 | */ |
||
117 | private $total_dirs = 0; |
||
118 | |||
119 | /** |
||
120 | * @var bool |
||
121 | */ |
||
122 | private $trim = false; |
||
123 | |||
124 | /** |
||
125 | * @var bool |
||
126 | */ |
||
127 | private $inheritAll = false; |
||
128 | |||
129 | /** |
||
130 | * @var bool |
||
131 | */ |
||
132 | private $inheritRefs = false; |
||
133 | |||
134 | /** |
||
135 | * @var Path |
||
136 | */ |
||
137 | private $currPath; |
||
138 | |||
139 | /** |
||
140 | * @var PhingReference[] |
||
141 | */ |
||
142 | private $references = []; |
||
143 | |||
144 | /** |
||
145 | * @var string |
||
146 | */ |
||
147 | private $index = 'index'; |
||
148 | |||
149 | /** |
||
150 | * This method does the work. |
||
151 | * |
||
152 | * @throws BuildException |
||
153 | */ |
||
154 | public function main() |
||
155 | { |
||
156 | if ( |
||
157 | null === $this->list |
||
158 | && null === $this->currPath |
||
159 | && 0 === count($this->dirsets) |
||
160 | && 0 === count($this->filesets) |
||
161 | && 0 === count($this->filelists) |
||
162 | ) { |
||
163 | throw new BuildException( |
||
164 | 'Need either list, path, nested dirset, nested fileset or nested filelist to iterate through' |
||
165 | ); |
||
166 | } |
||
167 | if (null === $this->param) { |
||
168 | throw new BuildException('You must supply a property name to set on each iteration in param'); |
||
169 | } |
||
170 | if (null === $this->calleeTarget) { |
||
171 | throw new BuildException('You must supply a target to perform'); |
||
172 | } |
||
173 | |||
174 | $callee = $this->createCallTarget(); |
||
175 | $mapper = null; |
||
176 | $total_entries = 0; |
||
177 | |||
178 | if (null !== $this->mapperElement) { |
||
179 | $mapper = $this->mapperElement->getImplementation(); |
||
180 | } |
||
181 | |||
182 | if (null !== $this->list) { |
||
183 | $arr = explode($this->delimiter, $this->list); |
||
184 | |||
185 | foreach ($arr as $index => $value) { |
||
186 | if ($this->trim) { |
||
187 | $value = trim($value); |
||
188 | } |
||
189 | $premapped = ''; |
||
190 | if (null !== $mapper) { |
||
191 | $premapped = $value; |
||
192 | $value = $mapper->main($value); |
||
193 | if (null === $value) { |
||
194 | continue; |
||
195 | } |
||
196 | $value = array_shift($value); |
||
197 | } |
||
198 | $this->log( |
||
199 | "Setting param '{$this->param}' to value '{$value}'" . ($premapped ? " (mapped from '{$premapped}')" : ''), |
||
200 | Project::MSG_VERBOSE |
||
201 | ); |
||
202 | $prop = $callee->createProperty(); |
||
203 | $prop->setName($this->param); |
||
204 | $prop->setValue($value); |
||
205 | $prop = $callee->createProperty(); |
||
206 | $prop->setName($this->index); |
||
207 | $prop->setValue($index); |
||
208 | $callee->main(); |
||
209 | ++$total_entries; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (null !== $this->currPath) { |
||
214 | $pathElements = $this->currPath->listPaths(); |
||
215 | foreach ($pathElements as $pathElement) { |
||
216 | $ds = new DirectoryScanner(); |
||
217 | $ds->setBasedir($pathElement); |
||
218 | $ds->scan(); |
||
219 | $this->process($callee, new File($pathElement), $ds->getIncludedFiles(), []); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | // filelists |
||
224 | foreach ($this->filelists as $fl) { |
||
225 | $srcFiles = $fl->getFiles($this->project); |
||
226 | |||
227 | $this->process($callee, $fl->getDir($this->project), $srcFiles, []); |
||
228 | } |
||
229 | |||
230 | // filesets |
||
231 | foreach ($this->filesets as $fs) { |
||
232 | $ds = $fs->getDirectoryScanner($this->project); |
||
233 | $srcFiles = $ds->getIncludedFiles(); |
||
234 | $srcDirs = $ds->getIncludedDirectories(); |
||
235 | |||
236 | $this->process($callee, $fs->getDir($this->project), $srcFiles, $srcDirs); |
||
237 | } |
||
238 | |||
239 | foreach ($this->dirsets as $dirset) { |
||
240 | $ds = $dirset->getDirectoryScanner($this->project); |
||
241 | $srcDirs = $ds->getIncludedDirectories(); |
||
242 | |||
243 | $this->process($callee, $dirset->getDir($this->project), [], $srcDirs); |
||
244 | } |
||
245 | |||
246 | if (null === $this->list) { |
||
247 | $this->log( |
||
248 | "Processed {$this->total_dirs} directories and {$this->total_files} files", |
||
249 | Project::MSG_VERBOSE |
||
250 | ); |
||
251 | } else { |
||
252 | $this->log( |
||
253 | "Processed {$total_entries} entr" . ($total_entries > 1 ? 'ies' : 'y') . ' in list', |
||
254 | Project::MSG_VERBOSE |
||
255 | ); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | public function setTrim(string $trim) |
||
260 | { |
||
261 | $this->trim = $trim; |
||
|
|||
262 | } |
||
263 | |||
264 | public function setList(string $list) |
||
265 | { |
||
266 | $this->list = $list; |
||
267 | } |
||
268 | |||
269 | public function setTarget(string $target) |
||
272 | } |
||
273 | |||
274 | public function addParam(PropertyTask $param) |
||
275 | { |
||
276 | $this->params[] = $param; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Corresponds to <code><phingcall></code>'s nested |
||
281 | * <code><reference></code> element. |
||
282 | */ |
||
283 | public function addReference(PhingReference $r) |
||
284 | { |
||
285 | $this->references[] = $r; |
||
286 | } |
||
287 | |||
288 | public function setAbsparam(string $absparam) |
||
289 | { |
||
290 | $this->absparam = $absparam; |
||
291 | } |
||
292 | |||
293 | public function setDelimiter(string $delimiter) |
||
294 | { |
||
295 | $this->delimiter = $delimiter; |
||
296 | } |
||
297 | |||
298 | public function setIndex($index) |
||
299 | { |
||
300 | $this->index = $index; |
||
301 | } |
||
302 | |||
303 | public function createPath() |
||
304 | { |
||
305 | if (null === $this->currPath) { |
||
306 | $this->currPath = new Path($this->getProject()); |
||
307 | } |
||
308 | |||
309 | return $this->currPath; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Nested creator, creates one Mapper for this task. |
||
314 | * |
||
315 | * @throws BuildException |
||
316 | * |
||
317 | * @return object The created Mapper type object |
||
318 | */ |
||
319 | public function createMapper() |
||
320 | { |
||
321 | if (null !== $this->mapperElement) { |
||
322 | throw new BuildException('Cannot define more than one mapper', $this->getLocation()); |
||
323 | } |
||
324 | $this->mapperElement = new Mapper($this->project); |
||
325 | |||
326 | return $this->mapperElement; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return PropertyTask |
||
331 | */ |
||
332 | public function createProperty() |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return PropertyTask |
||
339 | */ |
||
340 | public function createParam() |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param string $param |
||
347 | */ |
||
348 | public function setParam($param) |
||
349 | { |
||
350 | $this->param = $param; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Corresponds to <code><antcall></code>'s <code>inheritall</code> |
||
355 | * attribute. |
||
356 | * |
||
357 | * @param mixed $b |
||
358 | */ |
||
359 | public function setInheritall($b) |
||
360 | { |
||
361 | $this->inheritAll = $b; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Corresponds to <code><antcall></code>'s <code>inheritrefs</code> |
||
366 | * attribute. |
||
367 | * |
||
368 | * @param mixed $b |
||
369 | */ |
||
370 | public function setInheritrefs($b) |
||
371 | { |
||
372 | $this->inheritRefs = $b; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Processes a list of files & directories. |
||
377 | * |
||
378 | * @param array $srcFiles |
||
379 | * @param array $srcDirs |
||
380 | */ |
||
381 | protected function process(PhingCallTask $callee, File $fromDir, $srcFiles, $srcDirs) |
||
382 | { |
||
383 | $mapper = null; |
||
384 | |||
385 | if (null !== $this->mapperElement) { |
||
386 | $mapper = $this->mapperElement->getImplementation(); |
||
387 | } |
||
388 | |||
389 | $filecount = count($srcFiles); |
||
390 | $this->total_files += $filecount; |
||
391 | |||
392 | $this->processResources($filecount, $srcFiles, $callee, $fromDir, $mapper); |
||
393 | |||
394 | $dircount = count($srcDirs); |
||
395 | $this->total_dirs += $dircount; |
||
396 | |||
397 | $this->processResources($dircount, $srcDirs, $callee, $fromDir, $mapper); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @param string $fromDir |
||
402 | * @param FileNameMapper $mapper |
||
403 | * |
||
404 | * @throws IOException |
||
405 | */ |
||
406 | private function processResources(int $rescount, array $srcRes, PhingCallTask $callee, $fromDir, $mapper) |
||
438 | } |
||
439 | } |
||
440 | |||
441 | private function createCallTarget() |
||
442 | { |
||
443 | /** |
||
480 | } |
||
481 | } |
||
482 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.