Complex classes like Proxy 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 Proxy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Proxy implements ObjectManagerAwareInterface |
||
| 32 | { |
||
| 33 | use ArrayOrTraversableGuardTrait; |
||
| 34 | |||
| 35 | /** @var mixed[]|Traversable */ |
||
| 36 | protected $objects; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | protected $targetClass; |
||
| 40 | |||
| 41 | /** @var mixed[] */ |
||
| 42 | protected $valueOptions = []; |
||
| 43 | |||
| 44 | /** @var mixed[] */ |
||
| 45 | protected $findMethod = []; |
||
| 46 | |||
| 47 | /** @var mixed */ |
||
| 48 | protected $property; |
||
| 49 | |||
| 50 | /** @var mixed[] */ |
||
| 51 | protected $optionAttributes = []; |
||
| 52 | |||
| 53 | /** @var callable $labelGenerator A callable used to create a label based on an item in the collection an Entity */ |
||
| 54 | protected $labelGenerator; |
||
| 55 | |||
| 56 | /** @var bool|null */ |
||
| 57 | protected $isMethod; |
||
| 58 | |||
| 59 | /** @var ObjectManager */ |
||
| 60 | protected $objectManager; |
||
| 61 | |||
| 62 | /** @var bool */ |
||
| 63 | protected $displayEmptyItem = false; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | protected $emptyItemLabel = ''; |
||
| 67 | |||
| 68 | /** @var string|null */ |
||
| 69 | protected $optgroupIdentifier; |
||
| 70 | |||
| 71 | /** @var string|null */ |
||
| 72 | protected $optgroupDefault; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param mixed[] $options |
||
| 76 | */ |
||
| 77 | 23 | public function setOptions(array $options) : void |
|
| 78 | { |
||
| 79 | 23 | if (isset($options['object_manager'])) { |
|
| 80 | 22 | $this->setObjectManager($options['object_manager']); |
|
| 81 | } |
||
| 82 | |||
| 83 | 23 | if (isset($options['target_class'])) { |
|
| 84 | 22 | $this->setTargetClass($options['target_class']); |
|
| 85 | } |
||
| 86 | |||
| 87 | 23 | if (isset($options['property'])) { |
|
| 88 | 3 | $this->setProperty($options['property']); |
|
| 89 | } |
||
| 90 | |||
| 91 | 23 | if (isset($options['label_generator'])) { |
|
| 92 | 2 | $this->setLabelGenerator($options['label_generator']); |
|
|
|
|||
| 93 | } |
||
| 94 | |||
| 95 | 23 | if (isset($options['find_method'])) { |
|
| 96 | 4 | $this->setFindMethod($options['find_method']); |
|
| 97 | } |
||
| 98 | |||
| 99 | 23 | if (isset($options['is_method'])) { |
|
| 100 | 1 | $this->setIsMethod($options['is_method']); |
|
| 101 | } |
||
| 102 | |||
| 103 | 23 | if (isset($options['display_empty_item'])) { |
|
| 104 | 1 | $this->setDisplayEmptyItem($options['display_empty_item']); |
|
| 105 | } |
||
| 106 | |||
| 107 | 23 | if (isset($options['empty_item_label'])) { |
|
| 108 | 1 | $this->setEmptyItemLabel($options['empty_item_label']); |
|
| 109 | } |
||
| 110 | |||
| 111 | 23 | if (isset($options['option_attributes'])) { |
|
| 112 | 3 | $this->setOptionAttributes($options['option_attributes']); |
|
| 113 | } |
||
| 114 | |||
| 115 | 23 | if (isset($options['optgroup_identifier'])) { |
|
| 116 | 4 | $this->setOptgroupIdentifier($options['optgroup_identifier']); |
|
| 117 | } |
||
| 118 | |||
| 119 | 23 | if (! isset($options['optgroup_default'])) { |
|
| 120 | 23 | return; |
|
| 121 | } |
||
| 122 | |||
| 123 | 1 | $this->setOptgroupDefault($options['optgroup_default']); |
|
| 124 | 1 | } |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | 22 | public function getValueOptions() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | 20 | public function getObjects() |
|
| 142 | { |
||
| 143 | 20 | $this->loadObjects(); |
|
| 144 | |||
| 145 | 16 | return $this->objects; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set the label for the empty option |
||
| 150 | */ |
||
| 151 | 1 | public function setEmptyItemLabel(string $emptyItemLabel) : Proxy |
|
| 152 | { |
||
| 153 | 1 | $this->emptyItemLabel = $emptyItemLabel; |
|
| 154 | |||
| 155 | 1 | return $this; |
|
| 156 | } |
||
| 157 | |||
| 158 | 1 | public function getEmptyItemLabel() : string |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return mixed[] |
||
| 165 | */ |
||
| 166 | 14 | public function getOptionAttributes() : array |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param mixed[] $optionAttributes |
||
| 173 | */ |
||
| 174 | 3 | public function setOptionAttributes(array $optionAttributes) : void |
|
| 175 | { |
||
| 176 | 3 | $this->optionAttributes = $optionAttributes; |
|
| 177 | 3 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Set a flag, whether to include the empty option at the beginning or not |
||
| 181 | */ |
||
| 182 | 1 | public function setDisplayEmptyItem(bool $displayEmptyItem) : Proxy |
|
| 188 | |||
| 189 | public function getDisplayEmptyItem() : bool |
||
| 190 | { |
||
| 191 | return $this->displayEmptyItem; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set the object manager |
||
| 196 | */ |
||
| 197 | 22 | public function setObjectManager(ObjectManager $objectManager) : void |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get the object manager |
||
| 204 | */ |
||
| 205 | 20 | public function getObjectManager() : ObjectManager |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Set the FQCN of the target object |
||
| 212 | */ |
||
| 213 | 22 | public function setTargetClass(string $targetClass) : Proxy |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get the target class |
||
| 222 | */ |
||
| 223 | 20 | public function getTargetClass() : string |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Set the property to use as the label in the options |
||
| 230 | */ |
||
| 231 | 3 | public function setProperty(string $property) : Proxy |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | public function getProperty() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Set the label generator callable that is responsible for generating labels for the items in the collection |
||
| 248 | * |
||
| 249 | * @param callable $callable A callable used to create a label based off of an Entity |
||
| 250 | */ |
||
| 251 | 1 | public function setLabelGenerator(callable $callable) : void |
|
| 255 | |||
| 256 | 14 | public function getLabelGenerator() : ?callable |
|
| 260 | |||
| 261 | 13 | public function getOptgroupIdentifier() : ?string |
|
| 265 | |||
| 266 | 4 | public function setOptgroupIdentifier(string $optgroupIdentifier) : void |
|
| 270 | |||
| 271 | 2 | public function getOptgroupDefault() : ?string |
|
| 275 | |||
| 276 | 1 | public function setOptgroupDefault(string $optgroupDefault) : void |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Set if the property is a method to use as the label in the options |
||
| 283 | */ |
||
| 284 | 1 | public function setIsMethod(bool $method) : Proxy |
|
| 290 | |||
| 291 | 3 | public function getIsMethod() : ?bool |
|
| 295 | |||
| 296 | /** Set the findMethod property to specify the method to use on repository |
||
| 297 | * |
||
| 298 | * @param mixed[] $findMethod |
||
| 299 | */ |
||
| 300 | 4 | public function setFindMethod(array $findMethod) : Proxy |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Get findMethod definition |
||
| 309 | * |
||
| 310 | * @return mixed[] |
||
| 311 | */ |
||
| 312 | 20 | public function getFindMethod() : array |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param mixed $targetEntity |
||
| 319 | */ |
||
| 320 | 14 | protected function generateLabel($targetEntity) : ?string |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param mixed $value |
||
| 331 | * |
||
| 332 | * @return mixed[]|mixed|object |
||
| 333 | * |
||
| 334 | * @throws RuntimeException |
||
| 335 | */ |
||
| 336 | public function getValue($value) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Load objects |
||
| 377 | * |
||
| 378 | * @throws RuntimeException |
||
| 379 | * @throws Exception\InvalidRepositoryResultException |
||
| 380 | */ |
||
| 381 | 20 | protected function loadObjects() : void |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Load value options |
||
| 447 | * |
||
| 448 | * @throws RuntimeException |
||
| 449 | */ |
||
| 450 | 22 | protected function loadValueOptions() : void |
|
| 590 | } |
||
| 591 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: