| Total Complexity | 43 |
| Total Lines | 290 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
Complex classes like StructAttribute 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.
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 StructAttribute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class StructAttribute extends AbstractModel |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Type of the struct attribute |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $type = ''; |
||
| 20 | /** |
||
| 21 | * Defines that this property is not a simple value but an array of values |
||
| 22 | * Infos at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints} |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $containsElements = false; |
||
| 26 | /** |
||
| 27 | * Defines that this property can be removed from request or not. |
||
| 28 | * The property can be removed from the request (meaning from the Struct) as soon as the nillable=true && minOccurs=0 |
||
| 29 | * Infos at {@link http://www.w3schools.com/xml/el_element.asp} |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $removableFromRequest = false; |
||
| 33 | /** |
||
| 34 | * Main constructor |
||
| 35 | * @see AbstractModel::__construct() |
||
| 36 | * @uses StructAttribute::setType() |
||
| 37 | * @uses AbstractModel::setOwner() |
||
| 38 | * @param Generator $generator |
||
| 39 | * @param string $name the original name |
||
| 40 | * @param string $type the type |
||
| 41 | * @param Struct $struct defines the struct which owns this value |
||
| 42 | */ |
||
| 43 | public function __construct(Generator $generator, $name, $type = '', Struct $struct = null) |
||
| 44 | { |
||
| 45 | parent::__construct($generator, $name); |
||
| 46 | $this->setType($type)->setOwner($struct); |
||
| 47 | } |
||
| 48 | /** |
||
| 49 | * Returns the unique name in the current struct (for setters/getters and struct constructor array) |
||
| 50 | * @uses AbstractModel::getCleanName() |
||
| 51 | * @uses AbstractModel::getName() |
||
| 52 | * @uses AbstractModel::uniqueName() |
||
| 53 | * @uses StructAttribute::getOwner() |
||
| 54 | * @param string $string |
||
| 55 | * @param string $additionalContext |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getUniqueString($string, $additionalContext = '') |
||
| 59 | { |
||
| 60 | return self::uniqueName($string, spl_object_hash($this->getOwner()) . $this->getOwner()->getName() . $additionalContext); |
||
| 61 | } |
||
| 62 | /** |
||
| 63 | * Returns the unique name in the current struct (for setters/getters and struct constructor array) |
||
| 64 | * @uses AbstractModel::getCleanName() |
||
| 65 | * @uses AbstractModel::getName() |
||
| 66 | * @uses AbstractModel::uniqueName() |
||
| 67 | * @uses StructAttribute::getOwner() |
||
| 68 | * @param string $additionalContext |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getUniqueName($additionalContext = '') |
||
| 72 | { |
||
| 73 | return $this->getUniqueString($this->getCleanName(), $additionalContext); |
||
| 74 | } |
||
| 75 | /** |
||
| 76 | * Returns the getter name for this attribute |
||
| 77 | * @uses StructAttribute::getUniqueName() |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getGetterName() |
||
| 81 | { |
||
| 82 | return $this->replaceReservedMethod(sprintf('get%s', ucfirst($this->getUniqueName('get'))), $this->getOwner()->getPackagedName()); |
||
| 83 | } |
||
| 84 | /** |
||
| 85 | * Returns the getter name for this attribute |
||
| 86 | * @uses StructAttribute::getUniqueName() |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function getSetterName() |
||
| 92 | } |
||
| 93 | /** |
||
| 94 | * Returns the type value |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getType($useTypeStruct = false) |
||
| 107 | } |
||
| 108 | /** |
||
| 109 | * Sets the type value |
||
| 110 | * @param string $type |
||
| 111 | * @return StructAttribute |
||
| 112 | */ |
||
| 113 | public function setType($type) |
||
| 117 | } |
||
| 118 | /** |
||
| 119 | * Returns the type value |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function getContainsElements() |
||
| 123 | { |
||
| 124 | return $this->containsElements; |
||
| 125 | } |
||
| 126 | /** |
||
| 127 | * Sets the type value |
||
| 128 | * @param bool $containsElements |
||
| 129 | * @return StructAttribute |
||
| 130 | */ |
||
| 131 | public function setContainsElements($containsElements) |
||
| 132 | { |
||
| 133 | $this->containsElements = $containsElements; |
||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | /** |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function getRemovableFromRequest() |
||
| 140 | { |
||
| 141 | return $this->removableFromRequest; |
||
| 142 | } |
||
| 143 | /** |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function isAChoice() |
||
| 147 | { |
||
| 148 | return is_array($this->getMetaValue('choice')); |
||
| 149 | } |
||
| 150 | /** |
||
| 151 | * @param bool $removableFromRequest |
||
| 152 | * @return StructAttribute |
||
| 153 | */ |
||
| 154 | public function setRemovableFromRequest($removableFromRequest) |
||
| 155 | { |
||
| 156 | $this->removableFromRequest = $removableFromRequest; |
||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | /** |
||
| 160 | * If this attribute contains elements then it's an array |
||
| 161 | * only if its parent, the Struct, is not itself an array, |
||
| 162 | * if the parent is an array, then it is certainly not an array too |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function isArray() |
||
| 166 | { |
||
| 167 | return $this->containsElements || $this->isTypeStructArray(); |
||
| 168 | } |
||
| 169 | /** |
||
| 170 | * If this attribute is based on a struct that is a list, |
||
| 171 | * then it is list of basic scalar values that are sent space-separated |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function isList() |
||
| 178 | } |
||
| 179 | /** |
||
| 180 | * Returns potential default value |
||
| 181 | * @uses AbstractModel::getMetaValueFirstSet() |
||
| 182 | * @uses Utils::getValueWithinItsType() |
||
| 183 | * @uses StructAttribute::getType() |
||
| 184 | * @uses StructAttribute::getContainsElements() |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public function getDefaultValue() |
||
| 188 | { |
||
| 189 | if ($this->isArray() || $this->isList()) { |
||
| 190 | return []; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (($struct = $this->getTypeStruct()) && $struct->isStruct()) { |
||
| 194 | return null; |
||
| 195 | } |
||
| 196 | |||
| 197 | return Utils::getValueWithinItsType($this->getMetaValueFirstSet([ |
||
| 198 | 'default', |
||
| 199 | 'Default', |
||
| 200 | 'DefaultValue', |
||
| 201 | 'defaultValue', |
||
| 202 | 'defaultvalue', |
||
| 203 | ]), $this->getType(true)); |
||
| 204 | } |
||
| 205 | /** |
||
| 206 | * Returns true or false depending on minOccurs information associated to the attribute |
||
| 207 | * @uses AbstractModel::getMetaValueFirstSet() |
||
| 208 | * @uses AbstractModel::getMetaValue() |
||
| 209 | * @return bool true|false |
||
| 210 | */ |
||
| 211 | public function isRequired() |
||
| 212 | { |
||
| 213 | return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet([ |
||
| 214 | 'minOccurs', |
||
| 215 | 'minoccurs', |
||
| 216 | 'MinOccurs', |
||
| 217 | 'Minoccurs', |
||
| 218 | ], false)); |
||
| 219 | } |
||
| 220 | /** |
||
| 221 | * Returns the owner model object, meaning a Struct object |
||
| 222 | * @see AbstractModel::getOwner() |
||
| 223 | * @uses AbstractModel::getOwner() |
||
| 224 | * @return Struct |
||
| 225 | */ |
||
| 226 | public function getOwner() |
||
| 227 | { |
||
| 228 | return parent::getOwner(); |
||
| 229 | } |
||
| 230 | /** |
||
| 231 | * @uses StructAttribute::getType() |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public function isXml() |
||
| 235 | { |
||
| 236 | return mb_stripos($this->getType(), '\DOM') === 0; |
||
| 237 | } |
||
| 238 | /** |
||
| 239 | * @return Struct|null |
||
| 240 | */ |
||
| 241 | public function getTypeStruct() |
||
| 242 | { |
||
| 243 | $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance()); |
||
| 244 | return $struct ? $struct : $this->getGenerator()->getStructByName($this->getType()); |
||
| 245 | } |
||
| 246 | /** |
||
| 247 | * @return string[] |
||
| 248 | */ |
||
| 249 | public function getTypeStructMeta() |
||
| 250 | { |
||
| 251 | $typeStruct = $this->getTypeStruct(); |
||
| 252 | return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : []; |
||
| 253 | } |
||
| 254 | /** |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function isTypeStructArray() |
||
| 258 | { |
||
| 259 | $typeStruct = $this->getTypeStruct(); |
||
| 260 | return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct(); |
||
| 261 | } |
||
| 262 | /** |
||
| 263 | * @return Struct|null |
||
| 264 | */ |
||
| 265 | public function getInheritanceStruct() |
||
| 266 | { |
||
| 267 | return $this->getGenerator()->getStructByName($this->getInheritance()); |
||
| 268 | } |
||
| 269 | /** |
||
| 270 | * @return string[] |
||
| 271 | */ |
||
| 272 | public function getInheritanceStructMeta() |
||
| 273 | { |
||
| 274 | $inheritanceStruct = $this->getInheritanceStruct(); |
||
| 275 | return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : []; |
||
| 276 | } |
||
| 277 | /** |
||
| 278 | * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta() |
||
| 279 | * @return string[] |
||
| 280 | */ |
||
| 281 | public function getMeta() |
||
| 282 | { |
||
| 283 | return $this->mergeMeta($this->getInheritanceStructMeta(), $this->getTypeStructMeta(), parent::getMeta()); |
||
| 284 | } |
||
| 285 | /** |
||
| 286 | * @param $filename |
||
| 287 | * @return StructReservedMethod|StructArrayReservedMethod |
||
| 288 | */ |
||
| 289 | public function getReservedMethodsInstance($filename = null) |
||
| 290 | { |
||
| 291 | return $this->getOwner()->getReservedMethodsInstance($filename); |
||
| 292 | } |
||
| 293 | /** |
||
| 294 | * {@inheritDoc} |
||
| 295 | * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize() |
||
| 296 | */ |
||
| 297 | protected function toJsonSerialize() |
||
| 303 | ]; |
||
| 304 | } |
||
| 305 | } |
||
| 306 |