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 |
||
| 23 | class GherkinParam extends \Codeception\Module |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var boolean Flag to enable exception (prioritised over $nullable=true) |
||
| 28 | * false: no exception thrown if parameter invalid, instead replacement value is parameter {{name}} |
||
| 29 | * true: exception thrown if parameter invalid |
||
| 30 | */ |
||
| 31 | private $throwException = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var boolean Flag to null invalid parameter (incompatible with $throwException=true) |
||
| 35 | * true: if parameter invalid then replacement value will be null |
||
| 36 | * false: default behaviour, ie replacement value is parameter {{name}} |
||
| 37 | */ |
||
| 38 | private $nullable = false; |
||
| 39 | |||
| 40 | protected $config = ['onErrorThrowException', 'onErrorNull']; |
||
| 41 | |||
| 42 | protected $requiredFields = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array List events to listen to |
||
| 46 | */ |
||
| 47 | public static $events = [ |
||
| 48 | //run before any suite |
||
| 49 | 'suite.before' => 'beforeSuite', |
||
| 50 | //run before any steps |
||
| 51 | 'step.before' => 'beforeStep' |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array Current test suite config |
||
| 56 | */ |
||
| 57 | private static $suiteConfig; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array RegExp for parsing steps |
||
| 61 | */ |
||
| 62 | private static $regEx = [ |
||
| 63 | 'match' => '/{{\s?[A-z0-9_:-<>]+\s?}}/', |
||
| 64 | 'filter' => '/[{}]/', |
||
| 65 | 'config' => '/(?:^config)?:([A-z0-9_-]+)+(?=:|$)/', |
||
| 66 | 'array' => '/^(?P<var>[A-z0-9_-]+)(?:\[(?P<key>.+)])$/' |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initialize module configuration |
||
| 71 | */ |
||
| 72 | final public function _initialize() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Dynamic module reconfiguration |
||
| 85 | */ |
||
| 86 | final public function onReconfigure() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Parse param and replace {{.*}} by its Fixtures::get() value if exists |
||
| 93 | * |
||
| 94 | * @param string $param |
||
| 95 | * |
||
| 96 | * @return \mixed|null Returns parameter's value if exists, else parameter's name |
||
| 97 | */ |
||
| 98 | final protected function getValueFromParam(string $param) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Replace parameters' matches by corresponding values |
||
| 151 | * |
||
| 152 | * @param array $matches |
||
| 153 | * @param array $values |
||
| 154 | * @param string $param |
||
| 155 | * |
||
| 156 | * @return \mixed|null Returns parameter's value if exists, else parameter {{name}} |
||
| 157 | */ |
||
| 158 | //TODO: pass param ref to function (&) [performance] |
||
| 159 | final private function mapParametersToValues(array $matches, array $values, string $param) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Retrieve param value from current suite config |
||
| 189 | * |
||
| 190 | * @param string $param |
||
| 191 | * |
||
| 192 | * @return \mixed|null Returns parameter's value if exists, else null |
||
| 193 | */ |
||
| 194 | //TODO: pass param ref to function (&) [performance] |
||
| 195 | final protected function getValueFromConfig(string $param) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Retrieve param value from array in Fixtures |
||
| 216 | * |
||
| 217 | * @param string $param |
||
| 218 | * |
||
| 219 | * @return \mixed|null Returns parameter's value if exists, else null |
||
| 220 | */ |
||
| 221 | //TODO: pass param ref to function (&) [performance] |
||
| 222 | final protected function getValueFromArray(string $param) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Capture suite's config before any execution |
||
| 236 | * |
||
| 237 | * @param array $settings |
||
| 238 | * @return void |
||
| 239 | * |
||
| 240 | * @codeCoverageIgnore |
||
| 241 | * @ignore Codeception specific |
||
| 242 | */ |
||
| 243 | final public function _beforeSuite($settings = []) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Parse scenario's step before execution |
||
| 250 | * |
||
| 251 | * @param \Codeception\Step $step |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | final public function _beforeStep(Step $step) |
||
| 295 | |||
| 296 | } |
||
| 297 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: