| Conditions | 15 |
| Paths | 400 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | public static function build(array $properties, string $modelClass, bool $autoTimestamps, bool $softDelete): Definition |
||
| 56 | { |
||
| 57 | /** @var Model $modelClass */ |
||
| 58 | // add in the default ID property |
||
| 59 | if (!isset($properties[Model::DEFAULT_ID_NAME]) && $modelClass::getIDProperties() == [Model::DEFAULT_ID_NAME]) { |
||
| 60 | $properties[Model::DEFAULT_ID_NAME] = self::DEFAULT_ID_PROPERTY; |
||
| 61 | } |
||
| 62 | |||
| 63 | // generates created_at and updated_at timestamps |
||
| 64 | if ($autoTimestamps) { |
||
| 65 | $properties = array_replace(self::AUTO_TIMESTAMPS, $properties); |
||
| 66 | } |
||
| 67 | |||
| 68 | // generates deleted_at timestamps |
||
| 69 | if ($softDelete) { |
||
| 70 | $properties = array_replace(self::SOFT_DELETE_TIMESTAMPS, $properties); |
||
| 71 | } |
||
| 72 | |||
| 73 | $result = []; |
||
| 74 | foreach ($properties as $k => $property) { |
||
| 75 | // populate relationship property settings |
||
| 76 | if (isset($property['relation'])) { |
||
| 77 | // this is added for BC with older versions of pulsar |
||
| 78 | // that only supported belongs to relationships |
||
| 79 | if (!isset($property['relation_type'])) { |
||
| 80 | $property['relation_type'] = Model::RELATIONSHIP_BELONGS_TO; |
||
| 81 | $property['local_key'] = $k; |
||
| 82 | } elseif (!isset($property['persisted'])) { |
||
| 83 | $property['persisted'] = false; |
||
| 84 | } |
||
| 85 | |||
| 86 | $tempProperty = new Property($property); |
||
| 87 | $relation = RelationFactory::make(new $modelClass(), $k, $tempProperty); |
||
| 88 | if (!isset($property['foreign_key'])) { |
||
| 89 | $property['foreign_key'] = $relation->getForeignKey(); |
||
| 90 | } |
||
| 91 | |||
| 92 | if (!isset($property['local_key'])) { |
||
| 93 | $property['local_key'] = $relation->getLocalKey(); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!isset($property['pivot_tablename']) && $relation instanceof BelongsToMany) { |
||
| 97 | $property['pivot_tablename'] = $relation->getTablename(); |
||
| 98 | } |
||
| 99 | |||
| 100 | // when a belongs_to relationship is used then we automatically add a |
||
| 101 | // new property for the ID field which gets persisted to the DB |
||
| 102 | if (Model::RELATIONSHIP_BELONGS_TO == $property['relation_type'] && !isset($result[$property['local_key']])) { |
||
| 103 | $result[$property['local_key']] = new Property([ |
||
| 104 | 'type' => Type::INTEGER, |
||
| 105 | ]); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $result[$k] = new Property($property); |
||
| 110 | } |
||
| 111 | |||
| 112 | // order the properties array by name for consistency |
||
| 113 | // since it is constructed in a random order |
||
| 114 | ksort($result); |
||
| 115 | |||
| 116 | return new Definition($result); |
||
| 117 | } |
||
| 118 | } |
||
| 119 |