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) |
|
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) |
|
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) |
|
179 | |||
180 | /** |
||
181 | * @param ReaderInterface $reader |
||
182 | * @param Result $result |
||
183 | */ |
||
184 | 16 | protected function processReader(ReaderInterface $reader, Result $result) |
|
198 | |||
199 | /** |
||
200 | * @param WriterPipe[] $writers |
||
201 | */ |
||
202 | 5 | protected function prepareWriters($writers) |
|
208 | |||
209 | /** |
||
210 | * @param WriterPipe[] $writers |
||
211 | */ |
||
212 | 5 | protected function finishWriters($writers) |
|
218 | |||
219 | /** |
||
220 | * @param mixed $item |
||
221 | * @param Result $result |
||
222 | */ |
||
223 | 18 | protected function processItem($item, Result $result) |
|
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) |
|
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 |