Complex classes like Workflow 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Workflow, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Workflow |
||
31 | { |
||
32 | const APPEND = 1; |
||
33 | const PREPEND = 2; |
||
34 | |||
35 | /** |
||
36 | * @var AbstractPipe[] |
||
37 | */ |
||
38 | private $pipeline = []; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $options = [ |
||
44 | 'resumeOnError' => false, |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * @param array $options |
||
49 | * |
||
50 | * @codeCoverageIgnore |
||
51 | */ |
||
52 | public function __construct(array $options = []) |
||
56 | |||
57 | /** |
||
58 | * @param string|null $type |
||
59 | * |
||
60 | * @return AbstractPipe[] |
||
61 | */ |
||
62 | 2 | public function getPipeline($type = null) |
|
63 | { |
||
64 | 2 | $pipeline = []; |
|
65 | |||
66 | 2 | foreach ($this->pipeline as $element) { |
|
67 | 2 | if ($type === null || get_class($element) === $type) { |
|
68 | 2 | $pipeline[] = $element; |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 2 | return $pipeline; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Inserts an element into the pipeline at the given position. |
||
77 | * |
||
78 | * @param AbstractPipe $pipe |
||
79 | * |
||
80 | * @return Workflow |
||
81 | */ |
||
82 | 13 | protected function addPipe(AbstractPipe $pipe) |
|
83 | { |
||
84 | 13 | if ($pipe->getPosition() === self::PREPEND) { |
|
85 | 5 | array_unshift($this->pipeline, $pipe); |
|
86 | } else { |
||
87 | 13 | $this->pipeline[] = $pipe; |
|
88 | } |
||
89 | |||
90 | 13 | return $this; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param array|callable|FilterInterface $element |
||
95 | * |
||
96 | * @return Workflow |
||
97 | * |
||
98 | * @throws InvalidArgumentException |
||
99 | */ |
||
100 | 11 | public function addFilter($element) |
|
106 | |||
107 | /** |
||
108 | * @return FilterPipe[] |
||
109 | */ |
||
110 | 5 | public function getFilters() |
|
114 | |||
115 | /** |
||
116 | * @param ConverterInterface|callable|array $element |
||
117 | * |
||
118 | * @return Workflow $this |
||
119 | */ |
||
120 | 13 | public function addConverter($element) |
|
126 | |||
127 | /** |
||
128 | * @return ConverterPipe[] |
||
129 | */ |
||
130 | 5 | public function getConverters() |
|
134 | |||
135 | /** |
||
136 | * @param WriterInterface|array $element |
||
137 | * |
||
138 | * @return Workflow |
||
139 | */ |
||
140 | 7 | public function addWriter($element) |
|
146 | |||
147 | /** |
||
148 | * @return WriterPipe[] |
||
149 | */ |
||
150 | 3 | public function getWriters() |
|
154 | |||
155 | /** |
||
156 | * @param ReaderInterface[]|ReaderInterface $readers |
||
157 | * |
||
158 | * @return Result |
||
159 | */ |
||
160 | 19 | public function process($readers) |
|
161 | { |
||
162 | 19 | if (!is_array($readers)) { |
|
163 | 18 | $readers = [$readers]; |
|
164 | } |
||
165 | |||
166 | 19 | $result = new Result(); |
|
167 | |||
168 | 19 | $this->prepareWriters($this->getWriters()); |
|
169 | |||
170 | 19 | foreach ($readers as $reader) { |
|
171 | 19 | $this->processReader($reader, $result); |
|
172 | } |
||
173 | |||
174 | 18 | $this->finishWriters($this->getWriters()); |
|
175 | |||
176 | 18 | return $result; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param ReaderInterface $reader |
||
181 | * @param Result $result |
||
182 | */ |
||
183 | 16 | protected function processReader(ReaderInterface $reader, Result $result) |
|
184 | { |
||
185 | 16 | foreach ($reader as $item) { |
|
186 | 15 | $result->incReadCount(); |
|
187 | try { |
||
188 | 15 | $this->processItem($item, $result); |
|
189 | 2 | } catch (\Exception $e) { |
|
190 | 2 | $result->addException($e); |
|
191 | 2 | if (!$this->options['resumeOnError']) { |
|
192 | 15 | throw $e; |
|
193 | } |
||
194 | } |
||
195 | } |
||
196 | 15 | } |
|
197 | |||
198 | /** |
||
199 | * @param WriterPipe[] $writers |
||
200 | */ |
||
201 | 5 | protected function prepareWriters($writers) |
|
202 | { |
||
203 | 5 | foreach ($writers as $element) { |
|
204 | 5 | $element->getWriter()->prepare(); |
|
205 | } |
||
206 | 5 | } |
|
207 | |||
208 | /** |
||
209 | * @param WriterPipe[] $writers |
||
210 | */ |
||
211 | 5 | protected function finishWriters($writers) |
|
212 | { |
||
213 | 5 | foreach ($writers as $element) { |
|
214 | 5 | $element->getWriter()->finish(); |
|
215 | } |
||
216 | 5 | } |
|
217 | |||
218 | /** |
||
219 | * @param mixed $item |
||
220 | * @param Result $result |
||
221 | */ |
||
222 | 18 | protected function processItem($item, Result $result) |
|
223 | { |
||
224 | 18 | $written = false; |
|
225 | |||
226 | 18 | foreach ($this->pipeline as $element) { |
|
227 | 18 | if ($element instanceof FilterPipe && $element->getField()) { |
|
228 | 1 | if ($element->getFilter()->filter(Vale::get($item, $element->getField())) === false) { |
|
229 | 1 | return; |
|
230 | } |
||
231 | } elseif ($element instanceof FilterPipe) { |
||
232 | 1 | if ($element->getFilter()->filter($item) === false) { |
|
233 | 1 | return; |
|
234 | } |
||
235 | 18 | } elseif ($element instanceof ConverterPipe && $element->getField()) { |
|
236 | 4 | $item = $this->convertItemValue($item, $element); |
|
237 | } elseif ($element instanceof ConverterPipe) { |
||
238 | 7 | $item = $this->convertItem($item, $element); |
|
239 | 5 | if ($item === null) { |
|
240 | 5 | return; |
|
241 | } |
||
242 | } elseif ($element instanceof WriterPipe) { |
||
243 | 11 | if ($this->writeItem($item, $element) === true) { |
|
244 | 10 | $result->incWriteCount(); |
|
245 | 16 | $written = true; |
|
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | 16 | if ($written === true) { |
|
251 | 10 | $result->incItemWriteCount(); |
|
252 | } |
||
253 | 16 | } |
|
254 | |||
255 | /** |
||
256 | * Applies the given converter to the given item either if no filter is given or if the filter returns `true`. |
||
257 | * |
||
258 | * @param mixed $item |
||
259 | * @param ConverterPipe $pipe |
||
260 | * |
||
261 | * @return mixed |
||
262 | */ |
||
263 | 5 | protected function convertItem($item, ConverterPipe $pipe) |
|
272 | |||
273 | /** |
||
274 | * Applies the given converter to the given field in the given item if no filter is given or if the filters returns |
||
275 | * `true` for the field. |
||
276 | * |
||
277 | * @param mixed $item |
||
278 | * @param ConverterPipe $pipe |
||
279 | * |
||
280 | * @return mixed |
||
281 | */ |
||
282 | 4 | protected function convertItemValue($item, ConverterPipe $pipe) |
|
283 | { |
||
284 | 4 | $value = Vale::get($item, $pipe->getField()); |
|
285 | 4 | $filterValue = $pipe->getFilterField() ? Vale::get($item, $pipe->getFilterField()) : $item; |
|
286 | |||
287 | 4 | if ($pipe->getFilter() === null || $pipe->getFilter()->filter($filterValue) === true) { |
|
288 | 3 | $item = Vale::set($item, $pipe->getField(), $pipe->getConverter()->convert($value)); |
|
289 | } |
||
290 | |||
291 | 4 | return $item; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Writes the given item to the given writer if the no filter is given or the filter returns `true`. |
||
296 | * |
||
297 | * @param mixed $item |
||
298 | * @param WriterPipe $pipe |
||
299 | * |
||
300 | * @return bool `true` if the item has been written, `false` if not. |
||
301 | */ |
||
302 | 4 | protected function writeItem($item, WriterPipe $pipe) |
|
312 | } |
||
313 |