Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Request |
||
| 19 | { |
||
| 20 | |||
| 21 | /** @var Query[] */ |
||
| 22 | private $queries = []; |
||
| 23 | |||
| 24 | /** @var Fragment[] */ |
||
| 25 | private $fragments = []; |
||
| 26 | |||
| 27 | /** @var Mutation[] */ |
||
| 28 | private $mutations = []; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | private $variables = []; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $variableReferences = []; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | private $queryVariables = []; |
||
| 38 | |||
| 39 | /** @var array */ |
||
| 40 | private $fragmentReferences = []; |
||
| 41 | |||
| 42 | 32 | public function __construct($data = [], $variables = []) |
|
| 70 | |||
| 71 | 30 | public function addQueries($queries) |
|
| 77 | |||
| 78 | 30 | public function addMutations($mutations) |
|
| 84 | |||
| 85 | 29 | public function addQueryVariables($queryVariables) |
|
| 91 | |||
| 92 | 29 | public function addVariableReferences($variableReferences) |
|
| 98 | |||
| 99 | 29 | public function addFragmentReferences($fragmentReferences) |
|
| 105 | |||
| 106 | 30 | public function addFragments($fragments) |
|
| 112 | |||
| 113 | 29 | public function getOperationsInOrder() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return Query[] |
||
| 120 | */ |
||
| 121 | 1 | public function getQueries() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return Fragment[] |
||
| 128 | */ |
||
| 129 | 33 | public function getFragments() |
|
| 133 | |||
| 134 | 3 | public function addFragment(Fragment $fragment) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param $name |
||
| 141 | * |
||
| 142 | * @return null|Fragment |
||
| 143 | */ |
||
| 144 | 6 | public function getFragment($name) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @return Mutation[] |
||
| 157 | */ |
||
| 158 | 1 | public function getMutations() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | 1 | public function hasQueries() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | 1 | public function hasMutations() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 1 | public function hasFragments() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 2 | public function getVariables() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param array $variables |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | 32 | public function setVariables($variables) |
|
| 210 | |||
| 211 | 4 | public function getVariable($name) |
|
| 215 | |||
| 216 | 4 | public function hasVariable($name) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return array|Variable[] |
||
| 223 | */ |
||
| 224 | 30 | public function getQueryVariables() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param array $queryVariables |
||
| 231 | */ |
||
| 232 | public function setQueryVariables($queryVariables) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return array|FragmentReference[] |
||
| 239 | */ |
||
| 240 | 34 | public function getFragmentReferences() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param array $fragmentReferences |
||
| 247 | */ |
||
| 248 | public function setFragmentReferences($fragmentReferences) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return array|VariableReference[] |
||
| 255 | */ |
||
| 256 | 31 | public function getVariableReferences() |
|
| 260 | } |
||
| 261 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..