Completed
Push — master ( bedbb4...05596c )
by vistart
06:10
created

SelfBlameableTrait::setNullParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
use yii\base\ModelEvent;
16
use yii\base\InvalidConfigException;
17
use yii\base\InvalidParamException;
18
use yii\db\ActiveQuery;
19
use yii\db\IntegrityException;
20
21
/**
22
 * This trait is designed for the model who contains parent.
23
 * The BlameableTrait use this trait by default. If you want to use this trait
24
 * into seperate model, please call the `initSelfBlameableEvents()` method in
25
 * `init()` method, like following:
26
 * ```php
27
 * public function init()
28
 * {
29
 *     $this->initSelfBlameableEvents();  // put it before parent call.
30
 *     parent::init();
31
 * }
32
 * ```
33
 *
34
 * @property static $parent
35
 * @property-read static[] $ancestors
36
 * @property-read string[] $ancestorChain
37
 * @property-read array $ancestorModels
38
 * @property-read static $commonAncestor
39
 * @property-read static[] $children
40
 * @property-read static[] $oldChildren
41
 * @property array $selfBlameableRules
42
 * @version 1.0
43
 * @author vistart <[email protected]>
44
 */
45
trait SelfBlameableTrait
46
{
47
48
    /**
49
     * @var false|string attribute name of which store the parent's guid.
50
     * If you do not want to use self-blameable features, please set it false.
51
     * Or if you access any features of this trait when this parameter is false,
52
     * exception may be thrown.
53
     */
54
    public $parentAttribute = false;
55
56
    /**
57
     * @var string|array rule name and parameters of parent attribute, as well
58
     * as self referenced ID attribute.
59
     */
60
    public $parentAttributeRule = ['string', 'max' => 16];
61
62
    /**
63
     * @var string self referenced ID attribute.
64
     * If you enable self-blameable features, this parameter should be specified,
65
     * otherwise, exception will be thrown.
66
     */
67
    public $refIdAttribute = 'guid';
68
    public static $parentNone = 0;
69
    public static $parentParent = 1;
70
    public static $parentTypes = [
71
        0 => 'none',
72
        1 => 'parent',
73
    ];
74
    public static $onNoAction = 0;
75
    public static $onRestrict = 1;
76
    public static $onCascade = 2;
77
    public static $onSetNull = 3;
78
    public static $onUpdateTypes = [
79
        0 => 'on action',
80
        1 => 'restrict',
81
        2 => 'cascade',
82
        3 => 'set null',
83
    ];
84
85
    /**
86
     * @var integer indicates the on delete type. default to cascade.
87
     */
88
    public $onDeleteType = 2;
89
90
    /**
91
     * @var integer indicates the on update type. default to cascade.
92
     */
93
    public $onUpdateType = 2;
94
95
    /**
96
     * @var boolean indicates whether throw exception or not when restriction occured on updating or deleting operation.
97
     */
98
    public $throwRestrictException = false;
99
    
100
    /**
101
     * @var array store the attribute validation rules.
102
     * If this field is a non-empty array, then it will be given.
103
     */
104
    private $localSelfBlameableRules = [];
105
    public static $eventParentChanged = 'parentChanged';
106
    public static $eventChildAdded = 'childAdded';
107
108
    /**
109
     * @var false|integer Set the limit of ancestor level. False is no limit.
110
     * We strongly recommend you set an unsigned integer which is less than 256.
111
     */
112
    public $ancestorLimit = false;
113
114
    /**
115
     * @var false|integer Set the limit of children (not descendants). False is no limit.
116
     * We strongly recommend you set an unsigned integer which is less than 1024.
117
     */
118
    public $childrenLimit = false;
119
120
    /**
121
     * Get rules associated with self blameable attribute.
122
     * If self-blameable rules has been stored locally, then it will be given,
123
     * or return the parent attribute rule.
124
     * @return array rules.
125
     */
126 112
    public function getSelfBlameableRules()
127
    {
128 112
        if (!is_string($this->parentAttribute)) {
129 112
            return [];
130
        }
131 31
        if (!empty($this->localSelfBlameableRules) && is_array($this->localSelfBlameableRules)) {
132 2
            return $this->localSelfBlameableRules;
133
        }
134 31
        if (is_string($this->parentAttributeRule)) {
135
            $this->parentAttributeRule = [$this->parentAttributeRule];
136
        }
137 31
        $this->localSelfBlameableRules = [
138 31
            array_merge([$this->parentAttribute], $this->parentAttributeRule),
139
        ];
140 31
        return $this->localSelfBlameableRules;
141
    }
142
143
    /**
144
     * Set rules associated with self blameable attribute.
145
     * @param array $rules rules.
146
     */
147 1
    public function setSelfBlameableRules($rules = [])
148
    {
149 1
        $this->localSelfBlameableRules = $rules;
150 1
    }
151
152
    /**
153
     * Check whether this model has reached the ancestor limit.
154
     * If $ancestorLimit is false, it will be regared as no limit(return false).
155
     * If $ancestorLimit is not false and not an unsigned integer, 256 will be taken.
156
     * @return boolean
157
     */
158 41
    public function hasReachedAncestorLimit()
159
    {
160 41
        if ($this->ancestorLimit === false) {
161 41
            return false;
162
        }
163 3
        if (!is_numeric($this->ancestorLimit) || $this->ancestorLimit < 0) {
164 1
            $this->ancestorLimit = 256;
165
        }
166 3
        return count($this->getAncestorChain()) >= $this->ancestorLimit;
167
    }
168
169
    /**
170
     * Check whether this model has reached the children limit.
171
     * If $childrenLimit is false, it will be regarded as no limit(return false).
172
     * If $childrenLimit is not false and not an unsigned integer, 1024 will be taken.
173
     * @return boolean
174
     */
175 4
    public function hasReachedChildrenLimit()
176
    {
177 4
        if ($this->childrenLimit === false) {
178 4
            return false;
179
        }
180 1
        if (!is_numeric($this->childrenLimit) || $this->childrenLimit < 0) {
181 1
            $this->childrenLimit = 1024;
182
        }
183 1
        return ((int) $this->getChildren()->count()) >= $this->childrenLimit;
184
    }
185
186
    /**
187
     * Bear a child.
188
     * The creator of this child is not necessarily the creator of current one.
189
     * For example: Someone commit a comment on another user's comment, these
190
     * two comments are father and son, but do not belong to the same owner.
191
     * Therefore, you need to specify the creator of current model.
192
     * @param array $config
193
     * @return static|null Null if reached the ancestor limit or children limit.
194
     * @throws InvalidConfigException Self reference ID attribute or
195
     * parent attribute not determined.
196
     * @throws InvalidParamException ancestor or children limit reached.
197
     */
198 4
    public function bear($config = [])
199
    {
200 4
        if (!$this->parentAttribute) {
201 1
            throw new InvalidConfigException("Parent Attribute Not Determined.");
202
        }
203 3
        if (!$this->refIdAttribute) {
204
            throw new InvalidConfigException("Self Reference ID Attribute Not Determined.");
205
        }
206 3
        if ($this->hasReachedAncestorLimit()) {
207
            throw new InvalidParamException("Reached Ancestor Limit: " . $this->ancestorLimit);
208
        }
209 3
        if ($this->hasReachedChildrenLimit()) {
210
            throw new InvalidParamException("Reached Children Limit: ". $this->childrenLimit);
211
        }
212 3
        if (isset($config['class'])) {
213
            unset($config['class']);
214
        }
215 3
        $model = new static($config);
0 ignored issues
show
Unused Code introduced by
The call to SelfBlameableTrait::__construct() has too many arguments starting with $config.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
216 3
        if ($this->addChild($model) === false) {
217
            return false;
218
        }
219 3
        return $model;
220
    }
221
222
    /**
223
     * Add a child.
224
     * @param static $child
0 ignored issues
show
introduced by
The type SelfBlameableTrait for parameter $child is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
225
     * @return boolean
226
     */
227 3
    public function addChild($child)
228
    {
229 3
        return $this->hasReachedChildrenLimit() ? false : $child->setParent($this);
230
    }
231
232
    /**
233
     * Event triggered before deleting itself.
234
     * @param ModelEvent $event
235
     * @return boolean true if parentAttribute not specified.
236
     * @throws IntegrityException throw if $throwRestrictException is true when $onDeleteType is on restrict.
237
     */
238 54
    public function onDeleteChildren($event)
239
    {
240 54
        $sender = $event->sender;
241
        /* @var $sender static */
242 54
        if (empty($sender->parentAttribute) || !is_string($sender->parentAttribute)) {
243 49
            return true;
244
        }
245 5
        switch ($sender->onDeleteType) {
246 5
            case static::$onRestrict:
247 1
                $event->isValid = $sender->children === null;
248 1
                if ($this->throwRestrictException) {
249 1
                    throw new IntegrityException('Delete restricted.');
250
                }
251
                break;
252 4
            case static::$onCascade:
253 3
                $event->isValid = $sender->deleteChildren();
0 ignored issues
show
Documentation Bug introduced by
It seems like $sender->deleteChildren() can also be of type object<yii\db\IntegrityException>. However, the property $isValid is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
254 3
                break;
255 1
            case static::$onSetNull:
256 1
                $event->isValid = $sender->updateChildren(null);
0 ignored issues
show
Documentation Bug introduced by
It seems like $sender->updateChildren(null) can also be of type object<yii\db\IntegrityException>. However, the property $isValid is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
257 1
                break;
258
            case static::$onNoAction:
259
            default:
260
                $event->isValid = true;
261
                break;
262
        }
263 4
    }
264
265
    /**
266
     * Event triggered before updating itself.
267
     * @param ModelEvent $event
268
     * @return boolean true if parentAttribute not specified.
269
     * @throws IntegrityException throw if $throwRestrictException is true when $onUpdateType is on restrict.
270
     */
271
    public function onUpdateChildren($event)
272
    {
273
        $sender = $event->sender;
274
        /* @var $sender static */
275
        if (empty($sender->parentAttribute) || !is_string($sender->parentAttribute)) {
276
            return true;
277
        }
278
        switch ($sender->onUpdateType) {
279
            case static::$onRestrict:
280
                $event->isValid = $sender->getOldChildren() === null;
281
                if ($this->throwRestrictException) {
282
                    throw new IntegrityException('Update restricted.');
283
                }
284
                break;
285
            case static::$onCascade:
286
                $event->isValid = $sender->updateChildren();
0 ignored issues
show
Documentation Bug introduced by
It seems like $sender->updateChildren() can also be of type object<yii\db\IntegrityException>. However, the property $isValid is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
287
                break;
288
            case static::$onSetNull:
289
                $event->isValid = $sender->updateChildren(null);
0 ignored issues
show
Documentation Bug introduced by
It seems like $sender->updateChildren(null) can also be of type object<yii\db\IntegrityException>. However, the property $isValid is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
290
                break;
291
            case static::$onNoAction:
292
            default:
293
                $event->isValid = true;
294
                break;
295
        }
296
    }
297
298
    /**
299
     * Get parent query.
300
     * Or get parent instance if access by magic property.
301
     * @return ActiveQuery
302
     */
303 41
    public function getParent()
304
    {
305 41
        return $this->hasOne(static::class, [$this->refIdAttribute => $this->parentAttribute]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
306
    }
307
    
308 41
    public function getRefId()
309
    {
310 41
        if ($this->refIdAttribute == $this->guidAttribute) {
0 ignored issues
show
Bug introduced by
The property guidAttribute does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
311 41
            return $this->getGUID();
0 ignored issues
show
Bug introduced by
It seems like getGUID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
312
        }
313
        if ($this->refIdAttribute == $this->idAttribute) {
0 ignored issues
show
Bug introduced by
The property idAttribute does not seem to exist. Did you mean refIdAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
314
            return $this->getID();
0 ignored issues
show
Bug introduced by
It seems like getID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
315
        }
316
        return $this->{$this->refIdAttribute};
317
    }
318
    
319
    public function setRefId($id)
320
    {
321
        if ($this->refIdAttribute == $this->guidAttribute) {
322
            return $this->setGUID($id);
0 ignored issues
show
Bug introduced by
It seems like setGUID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
323
        }
324
        if ($this->refIdAttribute == $this->idAttribute) {
0 ignored issues
show
Bug introduced by
The property idAttribute does not seem to exist. Did you mean refIdAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
325
            return $this->setID($id);
0 ignored issues
show
Bug introduced by
It seems like setID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
326
        }
327
        return $this->{$this->refIdAttribute} = $id;
328
    }
329
330
    /**
331
     * Set parent.
332
     * Don't forget save model after setting it.
333
     * @param static $parent
0 ignored issues
show
introduced by
The type SelfBlameableTrait for parameter $parent is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
334
     * @return false|string False if restriction reached. Otherwise parent's GUID given.
335
     */
336 41
    public function setParent($parent)
337
    {
338 41
        if (empty($parent) || $this->getRefId() == $parent->getRefId() || $parent->hasAncestor($this) || $this->hasReachedAncestorLimit()) {
339 1
            return false;
340
        }
341 41
        unset($this->parent);
342 41
        unset($parent->children);
343 41
        $this->trigger(static::$eventParentChanged);
0 ignored issues
show
Bug introduced by
It seems like trigger() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
344 41
        $parent->trigger(static::$eventChildAdded);
0 ignored issues
show
Bug introduced by
It seems like trigger() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
345 41
        return $this->{$this->parentAttribute} = $parent->getRefId();
346
    }
347
    
348 1
    public function setNullParent()
349
    {
350 1
        unset($this->parent->children);
351 1
        unset($this->parent);
352 1
        $this->{$this->parentAttribute} = '';
353 1
    }
354
355
    /**
356
     * Check whether this model has parent.
357
     * @return boolean
358
     */
359 41
    public function hasParent()
360
    {
361 41
        return $this->parent !== null;
362
    }
363
364
    /**
365
     * Check whether if $ancestor is the ancestor of myself.
366
     * Note, Itself will not be regarded as the its ancestor.
367
     * @param static $ancestor
0 ignored issues
show
introduced by
The type SelfBlameableTrait for parameter $ancestor is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
368
     * @return boolean
369
     */
370 41
    public function hasAncestor($ancestor)
371
    {
372 41
        if (!$this->hasParent()) {
373 41
            return false;
374
        }
375 5
        if ($this->parent->getRefId() == $ancestor->getRefId()) {
376 2
            return true;
377
        }
378 5
        return $this->parent->hasAncestor($ancestor);
379
    }
380
381
    /**
382
     * Get ancestor chain. (Ancestors' GUID Only!)
383
     * If this model has ancestor, the return array consists all the ancestor in order.
384
     * The first element is parent, and the last element is root, otherwise return empty array.
385
     * If you want to get ancestor model, you can simplify instance a query and specify the
386
     * condition with the return value. But it will not return models under the order of ancestor chain.
387
     * @param string[] $ancestor
388
     * @return string[]
389
     */
390 8
    public function getAncestorChain($ancestor = [])
391
    {
392 8
        if (!is_string($this->parentAttribute)) {
393 1
            return [];
394
        }
395 7
        if (!$this->hasParent()) {
396 7
            return $ancestor;
397
        }
398 7
        $ancestor[] = $this->parent->getRefId();
399 7
        return $this->parent->getAncestorChain($ancestor);
400
    }
401
402
    /**
403
     * Get ancestors with specified ancestor chain.
404
     * @param string[] $ancestor Ancestor chain.
405
     * @return static[]|null
406
     */
407 2
    public static function getAncestorModels($ancestor)
408
    {
409 2
        if (empty($ancestor) || !is_array($ancestor)) {
410 1
            return [];
411
        }
412 2
        $models = [];
413 2
        foreach ($ancestor as $self) {
414 2
            $models[] = static::findOne($self);
415
        }
416 2
        return $models;
417
    }
418
419
    /**
420
     * Get ancestors.
421
     * @return static[]|null
422
     */
423 1
    public function getAncestors()
424
    {
425 1
        return (is_string($this->parentAttribute) && !empty($this->parentAttribute)) ?
426 1
        static::getAncestorModels($this->getAncestorChain()) : null;
427
    }
428
429
    /**
430
     * Check whether if this model has common ancestor with $model.
431
     * @param static $model
0 ignored issues
show
introduced by
The type SelfBlameableTrait for parameter $model is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
432
     * @return boolean
433
     */
434 2
    public function hasCommonAncestor($model)
435
    {
436 2
        return $this->getCommonAncestor($model) !== null;
437
    }
438
439
    /**
440
     * Get common ancestor. If there isn't common ancestor, null will be given.
441
     * @param static $model
0 ignored issues
show
introduced by
The type SelfBlameableTrait for parameter $model is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
442
     * @return static
443
     */
444 3
    public function getCommonAncestor($model)
445
    {
446 3
        if (empty($this->parentAttribute) || !is_string($this->parentAttribute) || empty($model) || !$model->hasParent()) {
447 1
            return null;
448
        }
449 2
        $ancestor = $this->getAncestorChain();
450 2
        if (in_array($model->parent->getRefId(), $ancestor)) {
451 2
            return $model->parent;
452
        }
453 1
        return $this->getCommonAncestor($model->parent);
454
    }
455
456
    /**
457
     * Get children query.
458
     * Or get children instances if access magic property.
459
     * @return ActiveQuery
460
     */
461 41
    public function getChildren()
462
    {
463 41
        return $this->hasMany(static::class, [$this->parentAttribute => $this->refIdAttribute])->inverseOf('parent');
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
464
    }
465
466
    /**
467
     * Get children which parent attribute point to old guid.
468
     * @return static[]
469
     */
470 1
    public function getOldChildren()
471
    {
472 1
        return static::find()->where([$this->parentAttribute => $this->getOldAttribute($this->refIdAttribute)])->all();
0 ignored issues
show
Bug introduced by
It seems like getOldAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
473
    }
474
475
    /**
476
     * Update all children, not grandchildren (descendants).
477
     * If onUpdateType is on cascade, the children will be updated automatically.
478
     * @param mixed $value set guid if false, set empty string if empty() return
479
     * true, otherwise set it to $parentAttribute.
480
     * @return IntegrityException|boolean true if all update operations
481
     * succeeded to execute, or false if anyone of them failed. If not production
482
     * environment or enable debug mode, it will return exception.
483
     * @throws IntegrityException throw if anyone update failed.
484
     * The exception message only contains the first error.
485
     */
486 1
    public function updateChildren($value = false)
487
    {
488 1
        $children = $this->getOldChildren();
489 1
        if (empty($children)) {
490
            return true;
491
        }
492 1
        $parentAttribute = $this->parentAttribute;
493 1
        $transaction = $this->getDb()->beginTransaction();
0 ignored issues
show
Bug introduced by
It seems like getDb() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
494
        try {
495 1
            foreach ($children as $child) {
496
                /* @var $child static */
497 1
                if ($value === false) {
498
                    $child->setParent($this);
499
                } elseif (empty($value)) {
500 1
                    $child->setNullParent();
501
                } else {
502
                    $child->$parentAttribute = $value;
503
                }
504 1
                if (!$child->save()) {
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
505 1
                    throw new IntegrityException('Update failed:', $child->getErrors());
0 ignored issues
show
Bug introduced by
It seems like getErrors() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
506
                }
507
            }
508 1
            $transaction->commit();
509
        } catch (IntegrityException $ex) {
510
            $transaction->rollBack();
511
            if (YII_DEBUG || YII_ENV !== YII_ENV_PROD) {
512
                Yii::error($ex->errorInfo, static::class . '\update');
513
                return $ex;
514
            }
515
            Yii::warning($ex->errorInfo, static::class . '\update');
516
            return false;
517
        }
518 1
        return true;
519
    }
520
521
    /**
522
     * Delete all children, not grandchildren (descendants).
523
     * If onDeleteType is `on cascade`, the children will be deleted automatically.
524
     * If onDeleteType is `on restrict` and contains children, the deletion will
525
     * be restricted.
526
     * @return IntegrityException|boolean true if all delete operations
527
     * succeeded to execute, or false if anyone of them failed. If not production
528
     * environment or enable debug mode, it will return exception.
529
     * @throws IntegrityException throw if anyone delete failed.
530
     * The exception message only contains the first error.
531
     */
532 3
    public function deleteChildren()
533
    {
534 3
        $children = $this->children;
535 3
        if (empty($children)) {
536 3
            return true;
537
        }
538 1
        $transaction = $this->getDb()->beginTransaction();
0 ignored issues
show
Bug introduced by
It seems like getDb() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
539
        try {
540 1
            foreach ($children as $child) {
541
                /* @var $child static */
542 1
                if (!$child->delete()) {
0 ignored issues
show
Bug introduced by
The method delete() does not exist on rhosocial\base\models\traits\SelfBlameableTrait. Did you maybe mean onDeleteChildren()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
543 1
                    throw new IntegrityException('Delete failed:', $child->getErrors());
0 ignored issues
show
Bug introduced by
It seems like getErrors() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
544
                }
545
            }
546 1
            $transaction->commit();
547
        } catch (IntegrityException $ex) {
548
            $transaction->rollBack();
549
            if (YII_DEBUG || YII_ENV !== YII_ENV_PROD) {
550
                Yii::error($ex->errorInfo, static::class . '\delete');
551
                return $ex;
552
            }
553
            Yii::warning($ex->errorInfo, static::class . '\delete');
554
            return false;
555
        }
556 1
        return true;
557
    }
558
559
    /**
560
     * Update children's parent attribute.
561
     * Event triggered before updating.
562
     * @param ModelEvent $event
563
     * @return boolean
564
     */
565 12
    public function onParentRefIdChanged($event)
566
    {
567 12
        $sender = $event->sender;
568 12
        if ($sender->isAttributeChanged($sender->refIdAttribute)) {
569
            return $sender->onUpdateChildren($event);
570
        }
571 12
    }
572
573
    /**
574
     * Attach events associated with self blameable attribute.
575
     */
576 119
    protected function initSelfBlameableEvents()
577
    {
578 119
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onParentRefIdChanged']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
579 119
        $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onDeleteChildren']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
580 119
    }
581
}
582