| Total Complexity | 45 |
| Total Lines | 321 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ObjectBeanPropertyDescriptor 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 ObjectBeanPropertyDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
||
| 21 | { |
||
| 22 | use ForeignKeyAnalyzerTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var ForeignKeyConstraint |
||
| 26 | */ |
||
| 27 | private $foreignKey; |
||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $beanNamespace; |
||
| 32 | /** |
||
| 33 | * @var BeanDescriptor |
||
| 34 | */ |
||
| 35 | private $foreignBeanDescriptor; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * ObjectBeanPropertyDescriptor constructor. |
||
| 39 | * @param Table $table |
||
| 40 | * @param ForeignKeyConstraint $foreignKey |
||
| 41 | * @param NamingStrategyInterface $namingStrategy |
||
| 42 | * @param string $beanNamespace |
||
| 43 | * @param AnnotationParser $annotationParser |
||
| 44 | * @param BeanDescriptor $foreignBeanDescriptor The BeanDescriptor of FK foreign table |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 47 | Table $table, |
||
| 48 | ForeignKeyConstraint $foreignKey, |
||
| 49 | NamingStrategyInterface $namingStrategy, |
||
| 50 | string $beanNamespace, |
||
| 51 | AnnotationParser $annotationParser, |
||
| 52 | BeanDescriptor $foreignBeanDescriptor |
||
| 53 | ) { |
||
| 54 | parent::__construct($table, $namingStrategy); |
||
| 55 | $this->foreignKey = $foreignKey; |
||
| 56 | $this->beanNamespace = $beanNamespace; |
||
| 57 | $this->annotationParser = $annotationParser; |
||
| 58 | $this->table = $table; |
||
| 59 | $this->namingStrategy = $namingStrategy; |
||
| 60 | $this->foreignBeanDescriptor = $foreignBeanDescriptor; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the foreignkey the column is part of, if any. null otherwise. |
||
| 65 | * |
||
| 66 | * @return ForeignKeyConstraint |
||
| 67 | */ |
||
| 68 | public function getForeignKey(): ForeignKeyConstraint |
||
| 69 | { |
||
| 70 | return $this->foreignKey; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the name of the class linked to this property or null if this is not a foreign key. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getClassName(): string |
||
| 79 | { |
||
| 80 | return $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the PHP type for the property (it can be a scalar like int, bool, or class names, like \DateTimeInterface, App\Bean\User....) |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getPhpType(): string |
||
| 89 | { |
||
| 90 | return '\\' . $this->beanNamespace . '\\' . $this->getClassName(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function isCompulsory(): bool |
||
| 99 | { |
||
| 100 | // Are all columns nullable? |
||
| 101 | foreach ($this->getLocalColumns() as $column) { |
||
| 102 | if ($column->getNotnull()) { |
||
| 103 | return true; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns true if the property has a default value. |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function hasDefault(): bool |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the code that assigns a value to its default value. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | * |
||
| 125 | * @throws TDBMException |
||
| 126 | */ |
||
| 127 | public function assignToDefaultCode(): string |
||
| 128 | { |
||
| 129 | throw new TDBMException('Foreign key based properties cannot be assigned a default value.'); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns true if the property is the primary key. |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function isPrimaryKey(): bool |
||
| 138 | { |
||
| 139 | $fkColumns = $this->foreignKey->getUnquotedLocalColumns(); |
||
| 140 | sort($fkColumns); |
||
| 141 | |||
| 142 | $pkColumns = TDBMDaoGenerator::getPrimaryKeyColumnsOrFail($this->table); |
||
| 143 | sort($pkColumns); |
||
| 144 | |||
| 145 | return $fkColumns == $pkColumns; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the PHP code for getters and setters. |
||
| 150 | * |
||
| 151 | * @return MethodGenerator[] |
||
| 152 | */ |
||
| 153 | public function getGetterSetterCode(): array |
||
| 154 | { |
||
| 155 | $tableName = $this->table->getName(); |
||
| 156 | $getterName = $this->getGetterName(); |
||
| 157 | $setterName = $this->getSetterName(); |
||
| 158 | $isNullable = !$this->isCompulsory(); |
||
| 159 | |||
| 160 | $referencedBeanName = $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
||
| 161 | |||
| 162 | $getter = new MethodGenerator($getterName); |
||
| 163 | $getter->setDocBlock(new DocBlockGenerator('Returns the ' . $referencedBeanName . ' object bound to this object via the ' . implode(' and ', $this->foreignKey->getUnquotedLocalColumns()) . ' column.')); |
||
| 164 | |||
| 165 | /*$types = [ $referencedBeanName ]; |
||
| 166 | if ($isNullable) { |
||
| 167 | $types[] = 'null'; |
||
| 168 | } |
||
| 169 | $getter->getDocBlock()->setTag(new ReturnTag($types));*/ |
||
| 170 | |||
| 171 | $getter->setReturnType(($isNullable ? '?' : '') . $this->beanNamespace . '\\' . $referencedBeanName); |
||
| 172 | $tdbmFk = ForeignKey::createFromFk($this->foreignKey); |
||
| 173 | |||
| 174 | $getter->setBody('return $this->getRef(' . var_export($tdbmFk->getCacheKey(), true) . ', ' . var_export($tableName, true) . ');'); |
||
| 175 | |||
| 176 | if ($this->isGetterProtected()) { |
||
| 177 | $getter->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); |
||
| 178 | } |
||
| 179 | |||
| 180 | $setter = new MethodGenerator($setterName); |
||
| 181 | $setter->setDocBlock(new DocBlockGenerator('The setter for the ' . $referencedBeanName . ' object bound to this object via the ' . implode(' and ', $this->foreignKey->getUnquotedLocalColumns()) . ' column.')); |
||
| 182 | |||
| 183 | $setter->setParameter(new ParameterGenerator('object', ($isNullable ? '?' : '') . $this->beanNamespace . '\\' . $referencedBeanName)); |
||
| 184 | |||
| 185 | $setter->setReturnType('void'); |
||
| 186 | |||
| 187 | $setter->setBody('$this->setRef(' . var_export($tdbmFk->getCacheKey(), true) . ', $object, ' . var_export($tableName, true) . ');'); |
||
| 188 | |||
| 189 | if ($this->isSetterProtected()) { |
||
| 190 | $setter->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); |
||
| 191 | } |
||
| 192 | |||
| 193 | return [$getter, $setter]; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the part of code useful when doing json serialization. |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getJsonSerializeCode(): string |
||
| 254 | } |
||
| 255 | |||
| 256 | private function getLazySerializeCode(string $propertyAccess): string |
||
| 257 | { |
||
| 258 | $rows = []; |
||
| 259 | foreach ($this->getForeignKey()->getUnquotedForeignColumns() as $column) { |
||
| 260 | $descriptor = $this->getBeanPropertyDescriptor($column); |
||
| 261 | $shouldFlatten = false; |
||
| 262 | if ($descriptor instanceof InheritanceReferencePropertyDescriptor) { |
||
| 263 | $descriptor = $descriptor->getNonScalarReferencedPropertyDescriptor(); |
||
| 264 | $shouldFlatten = true; |
||
| 265 | } |
||
| 266 | |||
| 267 | $indexName = ltrim($descriptor->getVariableName(), '$'); |
||
| 268 | $columnGetterName = $descriptor->getGetterName(); |
||
| 269 | if ($descriptor instanceof ObjectBeanPropertyDescriptor) { |
||
| 270 | if ($shouldFlatten) { |
||
| 271 | $rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]'); |
||
| 272 | } else { |
||
| 273 | $varName = $descriptor->getSafeVariableName(); |
||
| 274 | $lazySerializeCode = $descriptor->getLazySerializeCode($varName); |
||
| 275 | $rows[] = "'$indexName' => ($varName = $propertyAccess->$columnGetterName()) ? $lazySerializeCode : null"; |
||
| 276 | } |
||
| 277 | } elseif ($descriptor instanceof ScalarBeanPropertyDescriptor) { |
||
| 278 | $rows[] = "'$indexName' => $propertyAccess->$columnGetterName()"; |
||
| 279 | } else { |
||
| 280 | throw new TDBMException('PropertyDescriptor of class `' . get_class($descriptor) . '` cannot be serialized.'); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | return '[' . implode(', ', $rows) . ']'; |
||
| 284 | } |
||
| 285 | |||
| 286 | private function getBeanPropertyDescriptor(string $column): AbstractBeanPropertyDescriptor |
||
| 287 | { |
||
| 288 | foreach ($this->foreignBeanDescriptor->getBeanPropertyDescriptors() as $descriptor) { |
||
| 289 | if ($descriptor instanceof ScalarBeanPropertyDescriptor && $descriptor->getColumnName() === $column) { |
||
| 290 | return $descriptor; |
||
| 291 | } |
||
| 292 | if ($descriptor instanceof ObjectBeanPropertyDescriptor && in_array($column, $descriptor->getForeignKey()->getLocalColumns(), true)) { |
||
| 293 | return $descriptor; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | throw new TDBMException('PropertyDescriptor for `'.$this->table->getName().'`.`' . $column . '` not found in `' . $this->foreignBeanDescriptor->getTable()->getName() . '`'); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * The code to past in the __clone method. |
||
| 301 | * @return null|string |
||
| 302 | */ |
||
| 303 | public function getCloneRule(): ?string |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Tells if this property is a type-hintable in PHP (resource isn't for example) |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function isTypeHintable(): bool |
||
| 316 | } |
||
| 317 | |||
| 318 | private function isGetterProtected(): bool |
||
| 319 | { |
||
| 320 | return $this->findAnnotation(Annotation\ProtectedGetter::class) !== null; |
||
| 321 | } |
||
| 322 | |||
| 323 | private function isSetterProtected(): bool |
||
| 324 | { |
||
| 325 | return $this->findAnnotation(Annotation\ProtectedSetter::class) !== null; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $type |
||
| 330 | * @return null|object |
||
| 331 | */ |
||
| 332 | private function findAnnotation(string $type) |
||
| 341 | } |
||
| 342 | } |
||
| 343 |