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