| Total Complexity | 50 |
| Total Lines | 373 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Entities 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 Entities, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Entities extends FormRequestModel |
||
| 25 | { |
||
| 26 | use ContentManager, EntitiesTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ApiGenerator |
||
| 30 | */ |
||
| 31 | private ApiGenerator $generator; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private string $className; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected string $sourceCode = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected bool $isSoftDelete = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Entities constructor. |
||
| 50 | * @param $generator |
||
| 51 | */ |
||
| 52 | public function __construct($generator) |
||
| 53 | { |
||
| 54 | $this->generator = $generator; |
||
| 55 | $this->className = Classes::getClassName($this->generator->objectName); |
||
| 56 | $isSoftDelete = empty($this->generator->types[$this->generator->objectName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES] |
||
| 57 | [ApiInterface::RAML_PROPS][ModelsInterface::COLUMN_DEL_AT]) === false; |
||
| 58 | $this->setIsSoftDelete($isSoftDelete); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function setCodeState($generator) |
||
| 62 | { |
||
| 63 | $this->generator = $generator; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | public function isSoftDelete() : bool |
||
| 70 | { |
||
| 71 | return $this->isSoftDelete; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param bool $isSoftDelete |
||
| 76 | */ |
||
| 77 | public function setIsSoftDelete(bool $isSoftDelete) : void |
||
| 78 | { |
||
| 79 | $this->isSoftDelete = $isSoftDelete; |
||
| 80 | } |
||
| 81 | |||
| 82 | private function setRelations() |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $current current entity relations |
||
| 104 | * @param string $related entities from raml file based on relations method array |
||
| 105 | * @param string $relationEntity |
||
| 106 | */ |
||
| 107 | private function createRelationMethod(string $current, string $related, string $relationEntity) |
||
| 108 | { |
||
| 109 | $ucEntity = ucfirst($relationEntity); |
||
| 110 | $currentRels = explode(PhpInterface::PIPE, $current); |
||
| 111 | $relatedRels = explode(PhpInterface::PIPE, $related); |
||
| 112 | foreach ($relatedRels as $rel) { |
||
| 113 | if (strpos($rel, $this->generator->objectName) !== false) { |
||
| 114 | foreach ($currentRels as $cur) { |
||
| 115 | if (strpos($cur, $ucEntity) !== false) { |
||
| 116 | $isManyCurrent = strpos($cur, self::CHECK_MANY_BRACKETS); |
||
| 117 | $isManyRelated = strpos($rel, self::CHECK_MANY_BRACKETS); |
||
| 118 | if ($isManyCurrent === false && $isManyRelated === false) {// OneToOne |
||
| 119 | $this->setRelation($relationEntity, ModelsInterface::MODEL_METHOD_HAS_ONE); |
||
| 120 | } |
||
| 121 | if ($isManyCurrent !== false && $isManyRelated === false) {// ManyToOne |
||
| 122 | $this->setRelation($relationEntity, ModelsInterface::MODEL_METHOD_HAS_MANY); |
||
| 123 | } |
||
| 124 | if ($isManyCurrent === false && $isManyRelated !== false) {// OneToMany inverse |
||
| 125 | $this->setRelation($relationEntity, ModelsInterface::MODEL_METHOD_BELONGS_TO); |
||
| 126 | } |
||
| 127 | if ($isManyCurrent !== false && $isManyRelated !== false) {// ManyToMany |
||
| 128 | // check inversion of a pivot |
||
| 129 | $entityFile = $this->generator->formatEntitiesPath() |
||
| 130 | . PhpInterface::SLASH . $this->generator->objectName . |
||
| 131 | ucfirst($relationEntity) . |
||
| 132 | PhpInterface::PHP_EXT; |
||
| 133 | $relEntity = $relationEntity; |
||
| 134 | $objName = $this->generator->objectName; |
||
| 135 | if (file_exists($entityFile) === false) { |
||
| 136 | $relEntity = $this->generator->objectName; |
||
| 137 | $objName = $relationEntity; |
||
| 138 | } |
||
| 139 | $this->setRelation( |
||
| 140 | $relationEntity, ModelsInterface::MODEL_METHOD_BELONGS_TO_MANY, |
||
| 141 | MigrationsHelper::getTableName($objName . ucfirst($relEntity)) |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $ucEntity |
||
| 152 | * @throws \ReflectionException |
||
| 153 | */ |
||
| 154 | public function setPivot(string $ucEntity): void |
||
| 155 | { |
||
| 156 | $file = $this->generator->formatEntitiesPath() . |
||
| 157 | PhpInterface::SLASH . |
||
| 158 | $this->className . Classes::getClassName($ucEntity) . PhpInterface::PHP_EXT; |
||
| 159 | if ($this->generator->isMerge === true) { |
||
| 160 | $this->resetPivotContent($ucEntity, $file); |
||
| 161 | } else { |
||
| 162 | $this->setPivotContent($ucEntity); |
||
| 163 | } |
||
| 164 | $isCreated = FileManager::createFile( |
||
| 165 | $file, $this->sourceCode, |
||
| 166 | FileManager::isRegenerated($this->generator->options) |
||
| 167 | ); |
||
| 168 | if ($isCreated) { |
||
| 169 | Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | public function createPivot() |
||
| 174 | { |
||
| 175 | $formRequestEntity = $this->getFormRequestEntity($this->generator->version, $this->className); |
||
| 176 | /** @var BaseFormRequest $formRequest * */ |
||
| 177 | $formRequest = new $formRequestEntity(); |
||
| 178 | if (method_exists($formRequest, ModelsInterface::MODEL_METHOD_RELATIONS)) { |
||
| 179 | $relations = $formRequest->relations(); |
||
| 180 | $this->sourceCode .= PHP_EOL; // margin top from props |
||
| 181 | foreach ($relations as $relationEntity) { |
||
| 182 | $ucEntity = ucfirst($relationEntity); |
||
| 183 | $file = $this->generator->formatEntitiesPath() |
||
| 184 | . PhpInterface::SLASH . ucfirst($relationEntity) . $this->generator->objectName . |
||
| 185 | PhpInterface::PHP_EXT; |
||
| 186 | // check if inverse Entity pivot exists |
||
| 187 | if (file_exists($file) === false) { |
||
| 188 | // determine if ManyToMany, OneToMany, OneToOne rels |
||
| 189 | $current = $this->getRelationType($this->generator->objectName); |
||
| 190 | $related = $this->getRelationType($ucEntity); |
||
| 191 | if (empty($current) === false && empty($related) === false) { |
||
| 192 | $this->createPivotClass($current, $related, $relationEntity); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $current current entity relations |
||
| 201 | * @param string $related entities from raml file based on relations method array |
||
| 202 | * @param string $relationEntity |
||
| 203 | * @throws \ReflectionException |
||
| 204 | */ |
||
| 205 | private function createPivotClass(string $current, string $related, string $relationEntity): void |
||
| 206 | { |
||
| 207 | $ucEntity = ucfirst($relationEntity); |
||
| 208 | $currentRels = explode(PhpInterface::PIPE, $current); |
||
| 209 | $relatedRels = explode(PhpInterface::PIPE, $related); |
||
| 210 | foreach ($relatedRels as $rel) { |
||
| 211 | if (strpos($rel, $this->generator->objectName) !== false) { |
||
| 212 | foreach ($currentRels as $cur) { |
||
| 213 | if (strpos($cur, $ucEntity) !== false) { |
||
| 214 | $isManyCurrent = strpos($cur, self::CHECK_MANY_BRACKETS); |
||
| 215 | $isManyRelated = strpos($rel, self::CHECK_MANY_BRACKETS); |
||
| 216 | if ($isManyCurrent !== false && $isManyRelated !== false) {// ManyToMany |
||
| 217 | $this->setPivot($ucEntity); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $entity |
||
| 227 | * @param string $method |
||
| 228 | * @param \string[] ...$args |
||
| 229 | */ |
||
| 230 | private function setRelation(string $entity, string $method, string ...$args): void |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $entity |
||
| 242 | * @param string $method |
||
| 243 | * @param \string[] ...$args |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | private function getRelationReturn(string $entity, string $method, array $args): string |
||
| 247 | { |
||
| 248 | $toReturn = PhpInterface::DOLLAR_SIGN . PhpInterface::PHP_THIS |
||
| 249 | . PhpInterface::ARROW . $method |
||
| 250 | . PhpInterface::OPEN_PARENTHESES . Classes::getClassName($entity) |
||
| 251 | . PhpInterface::DOUBLE_COLON . PhpInterface::PHP_CLASS; |
||
| 252 | |||
| 253 | if (empty($args) === false) { |
||
| 254 | foreach ($args as $val) { |
||
| 255 | $toReturn .= PhpInterface::COMMA |
||
| 256 | . PhpInterface::SPACE . PhpInterface::QUOTES . $val . |
||
| 257 | PhpInterface::QUOTES; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | $toReturn .= PhpInterface::CLOSE_PARENTHESES; |
||
| 261 | return $toReturn; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Sets entity content to $sourceCode |
||
| 266 | */ |
||
| 267 | private function setContent(): void |
||
| 268 | { |
||
| 269 | $this->setTag(); |
||
| 270 | $this->setNamespace( |
||
| 271 | $this->generator->entitiesDir |
||
| 272 | ); |
||
| 273 | $baseMapper = BaseModel::class; |
||
| 274 | $baseMapperName = Classes::getName($baseMapper); |
||
| 275 | |||
| 276 | if ($this->isSoftDelete()) { |
||
| 277 | $this->setUse(SoftDeletes::class); |
||
| 278 | } |
||
| 279 | |||
| 280 | $this->setUse($baseMapper, false, true); |
||
| 281 | $this->startClass($this->className, $baseMapperName); |
||
| 282 | $this->setUseSoftDelete(); |
||
| 283 | $this->setComment(DefaultInterface::PROPS_START); |
||
| 284 | $this->setPropSoftDelete(); |
||
| 285 | $this->createProperty( |
||
| 286 | ModelsInterface::PROPERTY_PRIMARY_KEY, PhpInterface::PHP_MODIFIER_PROTECTED, |
||
| 287 | ApiInterface::RAML_ID, true |
||
| 288 | ); |
||
| 289 | $this->createProperty( |
||
| 290 | ModelsInterface::PROPERTY_TABLE, PhpInterface::PHP_MODIFIER_PROTECTED, |
||
| 291 | MigrationsHelper::getTableName($this->generator->objectName), true |
||
| 292 | ); |
||
| 293 | $this->createProperty( |
||
| 294 | ModelsInterface::PROPERTY_TIMESTAMPS, PhpInterface::PHP_MODIFIER_PUBLIC, |
||
| 295 | PhpInterface::PHP_TYPES_BOOL_FALSE |
||
| 296 | ); |
||
| 297 | $this->setIncrementingProperty(); |
||
| 298 | $this->setComment(DefaultInterface::PROPS_END); |
||
| 299 | $this->setComment(DefaultInterface::METHOD_START); |
||
| 300 | $this->setRelations(); |
||
| 301 | $this->setComment(DefaultInterface::METHOD_END); |
||
| 302 | $this->endClass(); |
||
| 303 | } |
||
| 304 | |||
| 305 | private function setIncrementingProperty() |
||
| 306 | { |
||
| 307 | // O(4) |
||
| 308 | $idObject = $this->generator->types[$this->generator->types[$this->generator->objectName][ApiInterface::RAML_PROPS][ApiInterface::RAML_ID]]; |
||
| 309 | if ($idObject[ApiInterface::RAML_TYPE] === ApiInterface::RAML_TYPE_STRING) { |
||
| 310 | $this->createProperty( |
||
| 311 | ModelsInterface::PROPERTY_INCREMENTING, PhpInterface::PHP_MODIFIER_PUBLIC, |
||
| 312 | PhpInterface::PHP_TYPES_BOOL_FALSE |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Sets entity content to $sourceCode |
||
| 319 | */ |
||
| 320 | private function resetContent(): void |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Sets pivot entity content to $sourceCode |
||
| 344 | * @param string $ucEntity an entity upper case first name |
||
| 345 | * @throws \ReflectionException |
||
| 346 | */ |
||
| 347 | private function setPivotContent(string $ucEntity): void |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Re-Sets pivot entity content to $sourceCode |
||
| 377 | * @param string $ucEntity an entity upper case first name |
||
| 378 | * @param string $file |
||
| 379 | */ |
||
| 380 | private function resetPivotContent(string $ucEntity, string $file) |
||
| 397 | } |
||
| 398 | } |