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) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Retrieve param value from current suite config |
||
| 187 | * |
||
| 188 | * @param string $param |
||
| 189 | * |
||
| 190 | * @return \mixed|null Returns parameter's value if exists, else null |
||
| 191 | */ |
||
| 192 | //TODO: pass param ref to function (&) [performance] |
||
| 193 | final protected function getValueFromConfig(string $param) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Retrieve param value from array in Fixtures |
||
| 214 | * |
||
| 215 | * @param string $param |
||
| 216 | * |
||
| 217 | * @return \mixed|null Returns parameter's value if exists, else null |
||
| 218 | */ |
||
| 219 | //TODO: pass param ref to function (&) [performance] |
||
| 220 | final protected function getValueFromArray(string $param) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Capture suite's config before any execution |
||
| 234 | * |
||
| 235 | * @param array $settings |
||
| 236 | * @return void |
||
| 237 | * |
||
| 238 | * @codeCoverageIgnore |
||
| 239 | * @ignore Codeception specific |
||
| 240 | */ |
||
| 241 | final public function _beforeSuite($settings = []) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Parse scenario's step before execution |
||
| 248 | * |
||
| 249 | * @param \Codeception\Step $step |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | final public function _beforeStep(Step $step) |
||
| 293 | |||
| 294 | } |
||
| 295 |