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 |
||
| 19 | class Request |
||
| 20 | { |
||
| 21 | |||
| 22 | /** @var Query[] */ |
||
| 23 | private $queries = []; |
||
| 24 | |||
| 25 | /** @var Fragment[] */ |
||
| 26 | private $fragments = []; |
||
| 27 | |||
| 28 | /** @var Mutation[] */ |
||
| 29 | private $mutations = []; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | private $variables = []; |
||
| 33 | |||
| 34 | /** @var VariableReference[] */ |
||
| 35 | private $variableReferences = []; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | private $queryVariables = []; |
||
| 39 | |||
| 40 | /** @var array */ |
||
| 41 | private $fragmentReferences = []; |
||
| 42 | |||
| 43 | 56 | public function __construct($data = [], $variables = []) |
|
| 71 | |||
| 72 | 55 | public function addQueries($queries) |
|
| 78 | |||
| 79 | 55 | public function addMutations($mutations) |
|
| 85 | |||
| 86 | 54 | public function addQueryVariables($queryVariables) |
|
| 92 | |||
| 93 | 54 | public function addVariableReferences($variableReferences) |
|
| 99 | |||
| 100 | 54 | public function addFragmentReferences($fragmentReferences) |
|
| 106 | |||
| 107 | 55 | public function addFragments($fragments) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return Query[] |
||
| 116 | */ |
||
| 117 | 53 | public function getAllOperations() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return Query[] |
||
| 124 | */ |
||
| 125 | 1 | public function getQueries() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @return Fragment[] |
||
| 132 | */ |
||
| 133 | 57 | public function getFragments() |
|
| 137 | |||
| 138 | 5 | public function addFragment(Fragment $fragment) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param $name |
||
| 145 | * |
||
| 146 | * @return null|Fragment |
||
| 147 | */ |
||
| 148 | 9 | public function getFragment($name) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @return Mutation[] |
||
| 161 | */ |
||
| 162 | 1 | public function getMutations() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 1 | public function hasQueries() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 1 | public function hasMutations() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | 1 | public function hasFragments() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | 2 | public function getVariables() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $variables |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | 56 | public function setVariables($variables) |
|
| 225 | |||
| 226 | 6 | public function getVariable($name) |
|
| 230 | |||
| 231 | 6 | public function hasVariable($name) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @return array|Variable[] |
||
| 238 | */ |
||
| 239 | 54 | public function getQueryVariables() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param array $queryVariables |
||
| 246 | */ |
||
| 247 | public function setQueryVariables($queryVariables) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return array|FragmentReference[] |
||
| 254 | */ |
||
| 255 | 59 | public function getFragmentReferences() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param array $fragmentReferences |
||
| 262 | */ |
||
| 263 | public function setFragmentReferences($fragmentReferences) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return array|VariableReference[] |
||
| 270 | */ |
||
| 271 | 55 | public function getVariableReferences() |
|
| 275 | } |
||
| 276 |
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..