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