| Total Complexity | 42 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MyAssignment 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 MyAssignment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MyAssignment extends Model |
||
| 19 | { |
||
| 20 | |||
| 21 | /** @var integer[] $children_ids*/ |
||
| 22 | public $children_ids = []; |
||
| 23 | |||
| 24 | /** @var MyActiveRecord[] indexed by child PK */ |
||
| 25 | public $current_children; |
||
| 26 | |||
| 27 | /** @var MyActiveRecord Last child by Time*/ |
||
| 28 | public $last_child; |
||
| 29 | |||
| 30 | /** @var MyActiveRecord $parent */ |
||
| 31 | public $parent; |
||
| 32 | |||
| 33 | /** @var MyActiveRecord $child */ |
||
| 34 | public $child; |
||
| 35 | |||
| 36 | /** @var MyActiveRecord $assignment */ |
||
| 37 | public $assignment; |
||
| 38 | |||
| 39 | /** @var MyActiveRecord $assignmentItem The Assignment item we process at the moment */ |
||
| 40 | public $assignmentItem; |
||
| 41 | |||
| 42 | /** @var string $child_fk_colname */ |
||
| 43 | public $child_fk_colname; |
||
| 44 | |||
| 45 | /** @var string $parent_fk_colname */ |
||
| 46 | public $parent_fk_colname; |
||
| 47 | |||
| 48 | /** @var string $assignmentClassname */ |
||
| 49 | public $assignmentClassname; |
||
| 50 | |||
| 51 | /** @var string $order_colname IF assignments need to be ordered, set this name */ |
||
| 52 | public $order_colname; |
||
| 53 | |||
| 54 | /** @var bool $isChildIdInteger Whether child id is integer (to clean from input string for order comparison) */ |
||
| 55 | public $isChildIdInteger; |
||
| 56 | |||
| 57 | /** @var array items order (if are ordered) */ |
||
| 58 | public $itemsOrder; |
||
| 59 | |||
| 60 | /** @var boolean Whether Assignments have separate children table or assigner directly to parents*/ |
||
| 61 | public $hasChildTable = true; |
||
| 62 | |||
| 63 | const EVENT_BEFORE_ITEM_SAVE = 'beforeItemSave'; |
||
| 64 | |||
| 65 | /** @var array array or attribute & value pairs that will be assigned to all created children [['attributeName1'=>'defaultValue1'],['attributeNamen'=>'defaultValuen]] */ |
||
| 66 | public $defaultValues; |
||
| 67 | |||
| 68 | /** {@inheritdoc} */ |
||
| 69 | public function init() |
||
| 70 | { |
||
| 71 | $this->on(self::EVENT_BEFORE_ITEM_SAVE, [$this, 'beforeItemSave']); |
||
| 72 | |||
| 73 | if (!$this->parent) { |
||
| 74 | throw new yii\base\InvalidArgumentException('Parent not defined in ' . self::class); |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->setCurrentChildren(); |
||
| 78 | $this->itemsOrder = []; |
||
| 79 | $this->assignmentClassname = get_class($this->assignment); |
||
| 80 | parent::init(); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** {@inheritdoc} */ |
||
| 84 | public function rules() |
||
| 85 | { |
||
| 86 | return [ |
||
| 87 | [['parent', 'child', 'assignment'], 'required'], |
||
| 88 | [['children_ids'], 'each', 'rule'=>['string', 'max'=>16]], |
||
| 89 | [['itemsOrder'], 'each', 'rule'=>['integer']] |
||
| 90 | ]; |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | public function assignDefaultValues() { |
||
| 96 | if (!empty($this->defaultValues)) { |
||
| 97 | foreach ($this->defaultValues as $attribute =>$value) { |
||
| 98 | $this->$attribute = $value; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param MyAssignmentEvent $event |
||
| 105 | */ |
||
| 106 | public function beforeItemSave($event) |
||
|
|
|||
| 107 | { |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | public function save() { |
||
| 112 | $i = 0; |
||
| 113 | $this->cleanChildrenIds(); |
||
| 114 | |||
| 115 | if (is_array($this->children_ids)) { |
||
| 116 | foreach ($this->children_ids as $childId) { |
||
| 117 | |||
| 118 | if (!$this->childExists($childId)) { |
||
| 119 | $model = new $this->assignmentClassname; |
||
| 120 | } else { |
||
| 121 | $model = $this->getCurrentChildById($childId); |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | $model->{$this->parent_fk_colname} = $this->parent->primaryKey; |
||
| 126 | if ($this->hasChildTable) { |
||
| 127 | $model->{$this->child_fk_colname} = $childId; |
||
| 128 | } |
||
| 129 | $model->{$this->child_fk_colname} = $childId; |
||
| 130 | |||
| 131 | |||
| 132 | // set order if order colname is set |
||
| 133 | if ($this->order_colname <> "") { |
||
| 134 | $model->{$this->order_colname} = $i; |
||
| 135 | } |
||
| 136 | |||
| 137 | // assign default Value |
||
| 138 | if (!empty($this->defaultValues)) { |
||
| 139 | foreach ($this->defaultValues as $attribute =>$value) { |
||
| 140 | $model->{$attribute} = $value; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | // inject code before item save |
||
| 144 | $this->assignmentItem = $model; |
||
| 145 | $event = new MyAssignmentEvent; |
||
| 146 | $event->item = $model; |
||
| 147 | $this->trigger(self::EVENT_BEFORE_ITEM_SAVE, $event); |
||
| 148 | |||
| 149 | if (!$this->assignmentItem->save()) { |
||
| 150 | $this->addErrors($this->assignmentItem->errors); |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | $i++; |
||
| 154 | } |
||
| 155 | |||
| 156 | } |
||
| 157 | |||
| 158 | // delete what was unselected |
||
| 159 | if (is_array($this->current_children)) { |
||
| 160 | foreach ($this->current_children as $child) { |
||
| 161 | if ((is_array($this->children_ids) && !in_array($child->{$this->child_fk_colname}, $this->children_ids)) |
||
| 162 | or (!is_array($this->children_ids))) { |
||
| 163 | |||
| 164 | $child->delete(); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | $this->setCurrentChildren(); |
||
| 170 | |||
| 171 | return true; |
||
| 172 | |||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | public function childExists($childId) { |
||
| 177 | $currentChildrenIds = $this->getCurrentChildrenIds(false); |
||
| 178 | if (is_array($currentChildrenIds)) { |
||
| 179 | return in_array($childId, $currentChildrenIds); |
||
| 180 | } |
||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function setCurrentChildren() { |
||
| 185 | $query = $this->identifyChildrenQuery(); |
||
| 186 | |||
| 187 | // if order column is set, we order it ascending |
||
| 188 | if ($this->order_colname) { |
||
| 189 | $query->orderBy([$this->order_colname=>SORT_ASC]); |
||
| 190 | } |
||
| 191 | |||
| 192 | $indexCol = $this->child->primaryKeySingle(); |
||
| 193 | $children = $query->indexBy($indexCol)->all(); |
||
| 194 | |||
| 195 | if ($children) { |
||
| 196 | $this->current_children = $children; |
||
| 197 | } |
||
| 198 | $this->getCurrentChildrenIds(); |
||
| 199 | |||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | public function setLastChild() { |
||
| 205 | $query = $this->identifyChildrenQuery(); |
||
| 206 | $query->orderBy([ |
||
| 207 | $this->assignment->timeCreatedCol => SORT_DESC, |
||
| 208 | /** |
||
| 209 | * if db does not record milliseconds, then we might have them |
||
| 210 | * in the same second so we need to sort by id additionally |
||
| 211 | */ |
||
| 212 | $this->assignment->primaryKeySingle() => SORT_DESC |
||
| 213 | ]); |
||
| 214 | /** @var MyActiveRecord $model */ |
||
| 215 | $model = $query->one(); |
||
| 216 | $this->last_child = $model; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return \yii\db\ActiveQuery |
||
| 221 | */ |
||
| 222 | public function identifyChildrenQuery() { |
||
| 223 | $query = $this->assignment->find() |
||
| 224 | ->andWhere([$this->parent_fk_colname => $this->parent->primaryKey]); |
||
| 225 | return $query; |
||
| 226 | |||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param bool $set Whether we set the children_ids or not. |
||
| 231 | * In case we get the id's before save - we do not want to set ids since we get |
||
| 232 | * the ids externally (post) |
||
| 233 | * @return array|bool |
||
| 234 | */ |
||
| 235 | public function getCurrentChildrenIds($set = true) { |
||
| 248 | |||
| 249 | } |
||
| 250 | private function getCurrentChildById($id) { |
||
| 251 | if (is_array($this->current_children)) { |
||
| 252 | foreach ($this->current_children as $child) { |
||
| 253 | if ($child->{$this->child_fk_colname} == $id) { |
||
| 254 | return $child; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Clean Ids to be integers |
||
| 264 | */ |
||
| 265 | private function cleanChildrenIds() { |
||
| 266 | if (is_array($this->children_ids) && $this->isChildIdInteger) { |
||
| 267 | $clean = []; |
||
| 268 | foreach ($this->children_ids as $id) { |
||
| 269 | $clean[] = intval($id); |
||
| 270 | } |
||
| 271 | $this->children_ids = $clean; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the last child assignment by TIME |
||
| 278 | * @return MyActiveRecord |
||
| 279 | * @deprecated |
||
| 280 | */ |
||
| 281 | public function getLastChild() { |
||
| 286 | } |
||
| 287 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.