Complex classes like Segment 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 Segment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Segment implements ParserInterface |
||
| 15 | { |
||
| 16 | /**#@+ |
||
| 17 | * Descriptive part elements. |
||
| 18 | */ |
||
| 19 | const TYPE = 0; |
||
| 20 | const NAME = 1; |
||
| 21 | const LITERAL = 1; |
||
| 22 | const DELIMITERS = 2; |
||
| 23 | /**#@-*/ |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $delimiter; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $pattern; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $constraints; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $tokens; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $regex; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $paramMap = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $delimiter |
||
| 57 | * @param string $pattern |
||
| 58 | * @param array $constraints |
||
| 59 | */ |
||
| 60 | public function __construct($delimiter, $pattern, array $constraints) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function parse($input, $offset) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function compile(array $params, array $defaults) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Gets regex for matching. |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | protected function getRegex() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Gets parsed tokens. |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | protected function getTokens() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Parses a pattern. |
||
| 131 | * |
||
| 132 | * @param string $pattern |
||
| 133 | * @return array |
||
| 134 | * @throws Exception\RuntimeException |
||
| 135 | */ |
||
| 136 | protected function parsePattern($pattern) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Builds the matching regex from parsed tokens. |
||
| 195 | * |
||
| 196 | * @param array $tokens |
||
| 197 | * @param array $constraints |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | protected function buildRegex(array $tokens, array $constraints) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Builds a string from parts. |
||
| 242 | * |
||
| 243 | * @param array $parts |
||
| 244 | * @param array $mergedParams |
||
| 245 | * @param array $defaults |
||
| 246 | * @return string |
||
| 247 | * @throws Exception\InvalidArgumentException |
||
| 248 | */ |
||
| 249 | protected function buildString(array $parts, array $mergedParams, array $defaults) |
||
| 316 | } |
||
| 317 |