Complex classes like GherkinParam 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 GherkinParam, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class GherkinParam extends \Codeception\Module |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * @var boolean Flag to enable exception (prioritised over $nullable=true) |
||
29 | * false: no exception thrown if parameter invalid, instead replacement value is parameter {{name}} |
||
30 | * true: exception thrown if parameter invalid |
||
31 | */ |
||
32 | private $throwException = false; |
||
33 | |||
34 | /** |
||
35 | * @var boolean Flag to null invalid parameter (incompatible with $throwException=true) |
||
36 | * true: if parameter invalid then replacement value will be null |
||
37 | * false: default behaviour, ie replacement value is parameter {{name}} |
||
38 | */ |
||
39 | private $nullable = false; |
||
40 | |||
41 | protected $config = ['onErrorThrowException', 'onErrorNull']; |
||
42 | |||
43 | protected $requiredFields = []; |
||
44 | |||
45 | /** |
||
46 | * @var array List events to listen to |
||
47 | */ |
||
48 | public static $events = [ |
||
49 | //run before any suite |
||
50 | 'suite.before' => 'beforeSuite', |
||
51 | //run before any steps |
||
52 | 'step.before' => 'beforeStep' |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @var array Current test suite config |
||
57 | */ |
||
58 | private static $suiteConfig; |
||
59 | |||
60 | /** |
||
61 | * @var array RegExp for parsing steps |
||
62 | */ |
||
63 | private static $regEx = [ |
||
64 | 'match' => '/{{\s?[A-z0-9_:-<>]+\s?}}/', |
||
65 | 'filter' => '/[{}]/', |
||
66 | 'config' => '/(?:^config)?:([A-z0-9_-]+)+(?=:|$)/', |
||
67 | 'array' => '/^(?P<var>[A-z0-9_-]+)(?:\[(?P<key>.+)])$/' |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * Initialize module configuration |
||
72 | */ |
||
73 | final public function _initialize() |
||
83 | |||
84 | /** |
||
85 | * Dynamic module reconfiguration |
||
86 | */ |
||
87 | final public function onReconfigure() |
||
91 | |||
92 | /** |
||
93 | * Parse param and replace {{.*}} by its Fixtures::get() value if exists |
||
94 | * |
||
95 | * @param string $param |
||
96 | * |
||
97 | * @return \mixed|null Returns parameter's value if exists, else parameter's name |
||
98 | */ |
||
99 | final protected function getValueFromParam(string $param) |
||
151 | |||
152 | /** |
||
153 | * Replace parameters' matches by corresponding values |
||
154 | * |
||
155 | * @param array $matches |
||
156 | * @param array $values |
||
157 | * @param string $param |
||
158 | * |
||
159 | * @return \mixed|null Returns parameter's value if exists, else parameter {{name}} |
||
160 | */ |
||
161 | //TODO: pass param ref to function (&) [performance] |
||
162 | final private function mapParametersToValues(array $matches, array $values, string $param) |
||
192 | |||
193 | /** |
||
194 | * Retrieve param value from current suite config |
||
195 | * |
||
196 | * @param string $param |
||
197 | * |
||
198 | * @return \mixed|null Returns parameter's value if exists, else null |
||
199 | */ |
||
200 | //TODO: pass param ref to function (&) [performance] |
||
201 | final protected function getValueFromConfig(string $param) |
||
219 | |||
220 | /** |
||
221 | * Retrieve param value from array in Fixtures |
||
222 | * |
||
223 | * @param string $param |
||
224 | * |
||
225 | * @return \mixed|null Returns parameter's value if exists, else null |
||
226 | */ |
||
227 | //TODO: pass param ref to function (&) [performance] |
||
228 | final protected function getValueFromArray(string $param) |
||
239 | |||
240 | /** |
||
241 | * Capture suite's config before any execution |
||
242 | * |
||
243 | * @param array $settings |
||
244 | * @return void |
||
245 | * |
||
246 | * @codeCoverageIgnore |
||
247 | * @ignore Codeception specific |
||
248 | */ |
||
249 | final public function _beforeSuite($settings = []) |
||
253 | |||
254 | /** |
||
255 | * Parse scenario's step before execution |
||
256 | * |
||
257 | * @param \Codeception\Step $step |
||
258 | * @return void |
||
259 | */ |
||
260 | final public function _beforeStep(Step $step) |
||
301 | |||
302 | } |
||
303 |