| Total Complexity | 52 |
| Total Lines | 334 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ScalarBeanPropertyDescriptor 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 ScalarBeanPropertyDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var Column |
||
| 25 | */ |
||
| 26 | private $column; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Annotations |
||
| 30 | */ |
||
| 31 | private $annotations; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * ScalarBeanPropertyDescriptor constructor. |
||
| 35 | * @param Table $table |
||
| 36 | * @param Column $column |
||
| 37 | * @param NamingStrategyInterface $namingStrategy |
||
| 38 | */ |
||
| 39 | public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
||
| 40 | { |
||
| 41 | parent::__construct($table, $namingStrategy); |
||
| 42 | $this->table = $table; |
||
| 43 | $this->column = $column; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Returns the param annotation for this property (useful for constructor). |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public function getParamAnnotation() |
||
| 52 | { |
||
| 53 | $paramType = $this->getPhpType(); |
||
| 54 | |||
| 55 | $str = ' * @param %s %s'; |
||
| 56 | |||
| 57 | return sprintf($str, $paramType, $this->getVariableName()); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns the name of the class linked to this property or null if this is not a foreign key. |
||
| 62 | * |
||
| 63 | * @return null|string |
||
| 64 | */ |
||
| 65 | public function getClassName(): ?string |
||
| 66 | { |
||
| 67 | return null; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the PHP type for the property (it can be a scalar like int, bool, or class names, like \DateTimeInterface, App\Bean\User....) |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getPhpType(): string |
||
| 76 | { |
||
| 77 | $type = $this->column->getType(); |
||
| 78 | return TDBMDaoGenerator::dbalTypeToPhpType($type); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function isCompulsory() |
||
| 87 | { |
||
| 88 | return $this->column->getNotnull() && !$this->isAutoincrement() && $this->column->getDefault() === null && !$this->hasUuidAnnotation(); |
||
| 89 | } |
||
| 90 | |||
| 91 | private function isAutoincrement() : bool |
||
| 92 | { |
||
| 93 | return $this->column->getAutoincrement() || $this->getAutoincrementAnnotation() !== null; |
||
| 94 | } |
||
| 95 | |||
| 96 | private function hasUuidAnnotation(): bool |
||
| 97 | { |
||
| 98 | return $this->getUuidAnnotation() !== null; |
||
| 99 | } |
||
| 100 | |||
| 101 | private function getUuidAnnotation(): ?Annotation |
||
| 102 | { |
||
| 103 | return $this->getAnnotations()->findAnnotation('UUID'); |
||
| 104 | } |
||
| 105 | |||
| 106 | private function getAutoincrementAnnotation(): ?Annotation |
||
| 107 | { |
||
| 108 | return $this->getAnnotations()->findAnnotation('Autoincrement'); |
||
| 109 | } |
||
| 110 | |||
| 111 | private function getAnnotations(): Annotations |
||
| 112 | { |
||
| 113 | if ($this->annotations === null) { |
||
| 114 | $comment = $this->column->getComment(); |
||
| 115 | if ($comment === null) { |
||
| 116 | return new Annotations([]); |
||
| 117 | } |
||
| 118 | $parser = new AnnotationParser(); |
||
| 119 | $this->annotations = $parser->parse($comment); |
||
| 120 | } |
||
| 121 | return $this->annotations; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns true if the property has a default value (or if the @UUID annotation is set for the column) |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | public function hasDefault() |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns the code that assigns a value to its default value. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function assignToDefaultCode() |
||
| 173 | } |
||
| 174 | |||
| 175 | private function getUuidCode(Annotation $uuidAnnotation): string |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns true if the property is the primary key. |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | public function isPrimaryKey(): bool |
||
| 195 | { |
||
| 196 | $primaryKey = $this->table->getPrimaryKey(); |
||
| 197 | if ($primaryKey === null) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | return in_array($this->column->getName(), $primaryKey->getUnquotedColumns()); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns the PHP code for getters and setters. |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getGetterSetterCode() |
||
| 209 | { |
||
| 210 | $normalizedType = $this->getPhpType(); |
||
| 211 | |||
| 212 | $columnGetterName = $this->getGetterName(); |
||
| 213 | $columnSetterName = $this->getSetterName(); |
||
| 214 | |||
| 215 | // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
||
| 216 | $isNullable = !$this->column->getNotnull() || $this->isAutoincrement(); |
||
| 217 | |||
| 218 | $resourceTypeCheck = ''; |
||
| 219 | if ($normalizedType === 'resource') { |
||
| 220 | $resourceTypeCheck .= <<<EOF |
||
| 221 | |||
| 222 | if (!\is_resource($%s)) { |
||
| 223 | throw \TheCodingMachine\TDBM\TDBMInvalidArgumentException::badType('resource', $%s, __METHOD__); |
||
| 224 | } |
||
| 225 | EOF; |
||
| 226 | $resourceTypeCheck = sprintf($resourceTypeCheck, $this->column->getName(), $this->column->getName()); |
||
| 227 | } |
||
| 228 | |||
| 229 | $getterAndSetterCode = ' /** |
||
| 230 | * The getter for the "%s" column. |
||
| 231 | * |
||
| 232 | * @return %s |
||
| 233 | */ |
||
| 234 | public function %s()%s%s%s |
||
| 235 | { |
||
| 236 | return $this->get(%s, %s); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * The setter for the "%s" column. |
||
| 241 | * |
||
| 242 | * @param %s $%s |
||
| 243 | */ |
||
| 244 | public function %s(%s%s$%s) : void |
||
| 245 | {%s |
||
| 246 | $this->set(%s, $%s, %s); |
||
| 247 | } |
||
| 248 | |||
| 249 | '; |
||
| 250 | |||
| 251 | return sprintf( |
||
| 252 | $getterAndSetterCode, |
||
| 253 | // Getter |
||
| 254 | $this->column->getName(), |
||
| 255 | $normalizedType.($isNullable ? '|null' : ''), |
||
| 256 | $columnGetterName, |
||
| 257 | ($this->isTypeHintable() ? ' : ' : ''), |
||
| 258 | ($isNullable && $this->isTypeHintable() ? '?' : ''), |
||
| 259 | ($this->isTypeHintable() ? $normalizedType: ''), |
||
| 260 | var_export($this->column->getName(), true), |
||
| 261 | var_export($this->table->getName(), true), |
||
| 262 | // Setter |
||
| 263 | $this->column->getName(), |
||
| 264 | $normalizedType.(($this->column->getNotnull() || !$this->isTypeHintable()) ? '' : '|null'), |
||
| 265 | $this->column->getName(), |
||
| 266 | $columnSetterName, |
||
| 267 | ($this->column->getNotnull() || !$this->isTypeHintable()) ? '' : '?', |
||
| 268 | $this->isTypeHintable() ? $normalizedType . ' ' : '', |
||
| 269 | //$castTo, |
||
| 270 | $this->column->getName(), |
||
| 271 | $resourceTypeCheck, |
||
| 272 | var_export($this->column->getName(), true), |
||
| 273 | $this->column->getName(), |
||
| 274 | var_export($this->table->getName(), true) |
||
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns the part of code useful when doing json serialization. |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getJsonSerializeCode() |
||
| 284 | { |
||
| 285 | $normalizedType = $this->getPhpType(); |
||
| 286 | |||
| 287 | if (!$this->canBeSerialized()) { |
||
| 288 | return ''; |
||
| 289 | } |
||
| 290 | |||
| 291 | if ($normalizedType == '\\DateTimeImmutable') { |
||
| 292 | if ($this->column->getNotnull()) { |
||
| 293 | return ' $array['.var_export($this->namingStrategy->getJsonProperty($this), true).'] = $this->'.$this->getGetterName()."()->format('c');\n"; |
||
| 294 | } else { |
||
| 295 | return ' $array['.var_export($this->namingStrategy->getJsonProperty($this), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
||
| 296 | } |
||
| 297 | } else { |
||
| 298 | return ' $array['.var_export($this->namingStrategy->getJsonProperty($this), true).'] = $this->'.$this->getGetterName()."();\n"; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns the column name. |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function getColumnName() |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * The code to past in the __clone method. |
||
| 314 | * @return null|string |
||
| 315 | */ |
||
| 316 | public function getCloneRule(): ?string |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * tells is this type is suitable for Json Serialization |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function canBeSerialized() : bool |
||
| 331 | { |
||
| 332 | $type = $this->column->getType(); |
||
| 333 | |||
| 334 | $unserialisableTypes = [ |
||
| 335 | Type::BLOB, |
||
| 336 | Type::BINARY |
||
| 337 | ]; |
||
| 338 | |||
| 339 | return \in_array($type->getName(), $unserialisableTypes, true) === false; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Tells if this property is a type-hintable in PHP (resource isn't for example) |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function isTypeHintable() : bool |
||
| 355 | } |
||
| 356 | } |
||
| 357 |