Complex classes like Transform 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 Transform, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | final class Transform implements TransformInterface |
||
| 12 | { |
||
| 13 | private $trans; |
||
| 14 | |||
| 15 | private $data; |
||
| 16 | |||
| 17 | private $sequence; |
||
| 18 | |||
| 19 | private $argDelimiter = TransformInterface::ARG_DELIM_SPACE; |
||
| 20 | |||
| 21 | const ROTATE_PATTERN = "/rotate\s*\(\s*(?<a>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<x>[+-]?\d+(?:\.\d+)?)(?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\s*\)/"; |
||
| 22 | |||
| 23 | const TRANSLATE_PATTERN = "/translate\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\)/"; |
||
| 24 | |||
| 25 | const SCALE_PATTERN = "/scale\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\)/"; |
||
| 26 | |||
| 27 | const SKEW_PATTERN = "/skew([XY])\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)?\)/"; |
||
| 28 | |||
| 29 | const MATRIX_PATTERN = "/matrix\s*\(\s*((([+-]?\d+(?:\.\d+)?)(?:\s+,?\s*|,\s*)){5}([+-]?\d+(?:\.\d+)?)\s*)\)/"; |
||
| 30 | |||
| 31 | private function __construct() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Use this method to instantiate Transform class. |
||
| 37 | * |
||
| 38 | * @param $transformString |
||
| 39 | * |
||
| 40 | * @uses matchRotate() |
||
| 41 | * @uses matchSkewX() |
||
| 42 | * @uses matchSkewY() |
||
| 43 | * @uses matchTranslate() |
||
| 44 | * @uses matchScale() |
||
| 45 | * @uses matchMatrix() |
||
| 46 | * |
||
| 47 | * @return Transform |
||
| 48 | */ |
||
| 49 | public static function newInstance($transformString = null) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | */ |
||
| 66 | public function result() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @inheritdoc |
||
| 73 | */ |
||
| 74 | public function setArgumentDelimiter($delim) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritdoc |
||
| 88 | */ |
||
| 89 | public function rotate($angle, $cx = null, $cy = null) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @inheritdoc |
||
| 103 | */ |
||
| 104 | public function translate($x, $y = null) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @inheritdoc |
||
| 111 | */ |
||
| 112 | public function scale($x, $y = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @inheritdoc |
||
| 119 | */ |
||
| 120 | public function skewX($x) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | public function skewY($y) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | public function matrix(array $matrix) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | private function matchRotate() |
||
| 152 | |||
| 153 | private function matchSkewX() |
||
| 157 | |||
| 158 | private function matchSkewY() |
||
| 162 | |||
| 163 | private function matchSkew() |
||
| 167 | |||
| 168 | private function matchScale() |
||
| 172 | |||
| 173 | private function matchMatrix() |
||
| 184 | |||
| 185 | private function matchTranslate() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getData() |
||
| 197 | |||
| 198 | private function hasTransform($transform) |
||
| 202 | |||
| 203 | private function getTransform($transform) |
||
| 211 | |||
| 212 | private function makeSequence() |
||
| 218 | |||
| 219 | private function addTransformSequence($transform) |
||
| 223 | |||
| 224 | private function buildTransformString() |
||
| 234 | |||
| 235 | private function setTransformData($transform, $data) |
||
| 247 | |||
| 248 | private function addTransformIfNeeded($transform) |
||
| 254 | |||
| 255 | private function matchPattern($pattern, $named) |
||
| 265 | |||
| 266 | private function shortcutBuild($transform, $data) |
||
| 274 | } |