Completed
Push — master ( 69007e...64430c )
by vistart
05:40
created

BlameableTrait::hasEverEdited()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 3
eloc 6
nc 2
nop 0
crap 12
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use yii\behaviors\BlameableBehavior;
16
use yii\caching\TagDependency;
17
18
/**
19
 * 该 Trait 用于处理属于用户的实例。包括以下功能:
20
 * 1.单列内容;多列内容待定;
21
 * 2.内容类型;具体类型应当自定义;
22
 * 3.内容规则;自动生成;
23
 * 4.归属用户 GUID;
24
 * 5.创建用户 GUID;
25
 * 6.上次更新用户 GUID;
26
 * 7.确认功能由 ConfirmationTrait 提供;
27
 * 8.实例功能由 EntityTrait 提供。
28
 * @property-read array $blameableAttributeRules Get all rules associated with
29
 * blameable.
30
 * @property array $blameableRules Get or set all the rules associated with
31
 * creator, updater, content and its ID, as well as all the inherited rules.
32
 * @property array $blameableBehaviors Get or set all the behaviors assoriated
33
 * with creator and updater, as well as all the inherited behaviors.
34
 * @property-read array $descriptionRules Get description property rules.
35
 * @property-read mixed $content Content.
36
 * @property-read boolean $contentCanBeEdited Whether this content could be edited.
37
 * @property-read array $contentRules Get content rules.
38
 * @property-read \vistart\Models\models\BaseUserModel $user
39
 * @version 2.0
40
 * @author vistart <[email protected]>
41
 */
42
trait BlameableTrait
43
{
44
    use ConfirmationTrait,
45
        SelfBlameableTrait;
46
47
    private $blameableLocalRules = [];
48
    private $blameableLocalBehaviors = [];
49
50
    /**
51
     * @var boolean|string|array Specify the attribute(s) name of content(s). If
52
     * there is only one content attribute, you can assign its name. Or there
53
     * is multiple attributes associated with contents, you can assign their
54
     * names in array. If you don't want to use this feature, please assign
55
     * false.
56
     * For example:
57
     * ```php
58
     * public $contentAttribute = 'comment'; // only one field named as 'comment'.
59
     * ```
60
     * or
61
     * ```php
62
     * public $contentAttribute = ['year', 'month', 'day']; // multiple fields.
63
     * ```
64
     * or
65
     * ```php
66
     * public $contentAttribute = false; // no need of this feature.
67
     * ```
68
     * If you don't need this feature, you should add rules corresponding with
69
     * `content` in `rules()` method of your user model by yourself.
70
     */
71
    public $contentAttribute = 'content';
72
73
    /**
74
     * @var array built-in validator name or validatation method name and
75
     * additional parameters.
76
     */
77
    public $contentAttributeRule = ['string', 'max' => 255];
78
79
    /**
80
     * @var boolean|string Specify the field which stores the type of content.
81
     */
82
    public $contentTypeAttribute = false;
83
84
    /**
85
     * @var boolean|array Specify the logic type of content, not data type. If
86
     * your content doesn't need this feature. please specify false. If the
87
     * $contentAttribute is specified to false, this attribute will be skipped.
88
     * ```php
89
     * public $contentTypes = [
90
     *     'public',
91
     *     'private',
92
     *     'friend',
93
     * ];
94
     * ```
95
     */
96
    public $contentTypes = false;
97
98
    /**
99
     * @var boolean|string This attribute speicfy the name of description
100
     * attribute. If this attribute is assigned to false, this feature will be
101
     * skipped.
102
     */
103
    public $descriptionAttribute = false;
104
105
    /**
106
     * @var string
107
     */
108
    public $initDescription = '';
109
110
    /**
111
     * @var string the attribute that will receive current user ID value. This
112
     * attribute must be assigned.
113
     */
114
    public $createdByAttribute = "user_guid";
115
116
    /**
117
     * @var string the attribute that will receive current user ID value.
118
     * Set this property to false if you do not want to record the updater ID.
119
     */
120
    public $updatedByAttribute = "user_guid";
121
122
    /**
123
     * @var boolean Add combinated unique rule if assigned to true.
124
     */
125
    public $idCreatorCombinatedUnique = true;
126
127
    /**
128
     * @var boolean|string The name of user class which own the current entity.
129
     * If this attribute is assigned to false, this feature will be skipped, and
130
     * when you use create() method of UserTrait, it will be assigned with
131
     * current user class.
132
     */
133
    public $userClass;
134
    public static $cacheKeyBlameableRules = 'blameable_rules';
135
    public static $cacheTagBlameableRules = 'tag_blameable_rules';
136
    public static $cacheKeyBlameableBehaviors = 'blameable_behaviors';
137
    public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors';
138
139
    /**
140
     * @inheritdoc
141
     */
142 24
    public function rules()
143
    {
144 24
        return $this->getBlameableRules();
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150 24
    public function behaviors()
151
    {
152 24
        return $this->getBlameableBehaviors();
153
    }
154
155
    /**
156
     * Get total of contents which owned by their owner.
157
     * @return integer
158
     */
159 2
    public function count()
160
    {
161 2
        $createdByAttribute = $this->createdByAttribute;
162 2
        return static::find()->where([$createdByAttribute => $this->$createdByAttribute])->count();
163
    }
164
165
    /**
166
     * Get content.
167
     * @return mixed
168
     */
169
    public function getContent()
170
    {
171
        $contentAttribute = $this->contentAttribute;
172
        if ($contentAttribute === false) {
173
            return null;
174
        }
175
        if (is_array($contentAttribute)) {
176
            $content = [];
177
            foreach ($contentAttribute as $key => $value) {
178
                $content[$key] = $this->$value;
179
            }
180
            return $content;
181
        }
182
        return $this->$contentAttribute;
183
    }
184
185
    /**
186
     * Set content.
187
     * @param mixed $content
188
     */
189
    public function setContent($content)
190
    {
191
        $contentAttribute = $this->contentAttribute;
192
        if ($contentAttribute === false) {
193
            return;
194
        }
195
        if (is_array($contentAttribute)) {
196
            foreach ($contentAttribute as $key => $value) {
197
                $this->$value = $content[$key];
198
            }
199
            return;
200
        }
201
        $this->$contentAttribute = $content;
202
    }
203
204
    /**
205
     * Determines whether content could be edited. Your should implement this
206
     * method by yourself.
207
     * @return boolean
208
     * @throws \yii\base\NotSupportedException
209
     */
210
    public function getContentCanBeEdited()
211
    {
212
        if ($this->contentAttribute === false) {
213
            return false;
214
        }
215
        throw new \yii\base\NotSupportedException("This method is not implemented.");
216
    }
217
218
    /**
219
     * Check it has been ever edited.
220
     * @return boolean Whether this content has ever been edited.
221
     */
222
    public function hasEverEdited()
223
    {
224
        $createdAtAttribute = $this->createdByAttribute;
225
        $updatedAtAttribute = $this->updatedByAttribute;
226
        if (!$createdAtAttribute || !$updatedAtAttribute) {
227
            return false;
228
        }
229
        return $this->$createdAtAttribute === $this->$updatedAtAttribute;
230
    }
231
232
    /**
233
     * Get blameable rules cache key.
234
     * @return string cache key.
235
     */
236 24
    public function getBlameableRulesCacheKey()
237
    {
238 24
        return static::className() . $this->cachePrefix . static::$cacheKeyBlameableRules;
0 ignored issues
show
Bug introduced by
The property cachePrefix 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...
239
    }
240
241
    /**
242
     * Get blameable rules cache tag.
243
     * @return string cache tag
244
     */
245 6
    public function getBlameableRulesCacheTag()
246
    {
247 6
        return static::className() . $this->cachePrefix . static::$cacheTagBlameableRules;
248
    }
249
250
    /**
251
     * Get the rules associated with content to be blamed.
252
     * @return array rules.
253
     */
254 24
    public function getBlameableRules()
255
    {
256 24
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() 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...
257 24
        if ($cache) {
258 24
            $this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey());
259 24
        }
260
        // 若当前规则不为空,且是数组,则认为是规则数组,直接返回。
261 24
        if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) {
262 21
            return $this->blameableLocalRules;
263
        }
264
265
        // 父类规则与确认规则合并。
266 6
        if ($cache) {
267 6
            TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]);
0 ignored issues
show
Bug introduced by
It seems like getEntityRulesCacheTag() 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...
268 6
        }
269 6
        $rules = array_merge(
270 6
            parent::rules(),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (rules() instead of getBlameableRules()). Are you sure this is correct? If so, you might want to change this to $this->rules().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
271 6
            $this->getConfirmationRules(),
272 6
            $this->getBlameableAttributeRules(),
273 6
            $this->getDescriptionRules(),
274 6
            $this->getContentRules(),
275 6
            $this->getSelfBlameableRules()
276 6
        );
277 6
        $this->setBlameableRules($rules);
278 6
        return $this->blameableLocalRules;
279
    }
280
281
    /**
282
     * Get the rules associated with `createdByAttribute`, `updatedByAttribute`
283
     * and `idAttribute`-`createdByAttribute` combination unique.
284
     * @return array rules.
285
     */
286 6
    public function getBlameableAttributeRules()
287
    {
288 6
        $rules = [];
289
        // 创建者和上次修改者由 BlameableBehavior 负责,因此标记为安全。
290 6
        if (!is_string($this->createdByAttribute) || empty($this->createdByAttribute)) {
291
            throw new \yii\base\NotSupportedException('You must assign the creator.');
292
        }
293 6
        $rules[] = [
294 6
            [$this->createdByAttribute],
295 6
            'safe',
296
        ];
297
298 6
        if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute)) {
299 1
            $rules[] = [
300 1
                [$this->updatedByAttribute],
301 1
                'safe',
302
            ];
303 1
        }
304
305 6
        if ($this->idCreatorCombinatedUnique && is_string($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...
306 3
            $rules [] = [
307 3
                [$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...
308 3
                    $this->createdByAttribute],
309 3
                'unique',
310 3
                'targetAttribute' => [$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...
311 3
                    $this->createdByAttribute],
312
            ];
313 3
        }
314 6
        return $rules;
315
    }
316
317
    /**
318
     * Get the rules associated with `description` attribute.
319
     * @return array rules.
320
     */
321 6
    public function getDescriptionRules()
322
    {
323 6
        $rules = [];
324 6
        if (is_string($this->descriptionAttribute) && !empty($this->descriptionAttribute)) {
325 1
            $rules[] = [
326 1
                [$this->descriptionAttribute],
327
                'string'
328 1
            ];
329 1
            $rules[] = [
330 1
                [$this->descriptionAttribute],
331 1
                'default',
332 1
                'value' => $this->initDescription,
333
            ];
334 1
        }
335 6
        return $rules;
336
    }
337
338
    /**
339
     * Get the rules associated with `content` and `contentType` attributes.
340
     * @return array rules.
341
     */
342 6
    public function getContentRules()
343
    {
344 6
        if (!$this->contentAttribute) {
345 2
            return [];
346
        }
347 4
        $rules = [];
348 4
        $rules[] = [[
349 4
            $this->contentAttribute],
350 4
            'required'];
351 4
        if ($this->contentAttributeRule) {
352 4
            if (is_string($this->contentAttributeRule)) {
353
                $this->contentAttributeRule = [$this->contentAttributeRule];
354
            }
355 4
            if (is_array($this->contentAttributeRule)) {
356 4
                $rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule);
357 4
            }
358 4
        }
359
360 4
        if (!$this->contentTypeAttribute) {
361 2
            return $rules;
362
        }
363
364 2
        if (is_array($this->contentTypes) && !empty($this->contentTypes)) {
365 2
            $rules[] = [[
366 2
                $this->contentTypeAttribute],
367 2
                'required'];
368 2
            $rules[] = [[
369 2
                $this->contentTypeAttribute],
370 2
                'in',
371 2
                'range' => array_keys($this->contentTypes)];
372 2
        }
373 2
        return $rules;
374
    }
375
376
    /**
377
     * Set blameable rules.
378
     * @param array $rules
379
     */
380 6
    protected function setBlameableRules($rules = [])
381
    {
382 6
        $this->blameableLocalRules = $rules;
383 6
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() 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...
384 6
        if ($cache) {
385 6
            $tagDependency = new \yii\caching\TagDependency(['tags' => [$this->getBlameableRulesCacheTag()]]);
386 6
            $cache->set($this->getBlameableRulesCacheKey(), $rules, 0, $tagDependency);
387 6
        }
388 6
    }
389
390
    /**
391
     * Get blameable behaviors cache key.
392
     * @return string cache key.
393
     */
394 24
    public function getBlameableBehaviorsCacheKey()
395
    {
396 24
        return static::className() . $this->cachePrefix . static::$cacheKeyBlameableBehaviors;
397
    }
398
399
    /**
400
     * Get blameable behaviors cache tag.
401
     * @return string cache tag.
402
     */
403 6
    public function getBlameableBehaviorsCacheTag()
404
    {
405 6
        return static::className() . $this->cachePrefix . static::$cacheTagBlameableBehaviors;
406
    }
407
408
    /**
409
     * Get blameable behaviors. If current behaviors array is empty, the init
410
     * array will be given.
411
     * @return array
412
     */
413 24
    public function getBlameableBehaviors()
414
    {
415 24
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() 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...
416 24
        if ($cache) {
417 24
            $this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey());
418 24
        }
419 24
        if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) {
420 6
            if ($cache) {
421 6
                TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]);
0 ignored issues
show
Bug introduced by
The method getEntityBehaviorsCacheTag() does not exist on vistart\Models\traits\BlameableTrait. Did you maybe mean behaviors()?

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...
422 6
            }
423 6
            $behaviors = parent::behaviors();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (behaviors() instead of getBlameableBehaviors()). Are you sure this is correct? If so, you might want to change this to $this->behaviors().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
424 6
            $behaviors['blameable'] = [
425 6
                'class' => BlameableBehavior::className(),
426 6
                'createdByAttribute' => $this->createdByAttribute,
427 6
                'updatedByAttribute' => $this->updatedByAttribute,
428 6
                'value' => [$this,
429 6
                    'onGetCurrentUserGuid'],
430
            ];
431 6
            $this->setBlameableBehaviors($behaviors);
432 6
        }
433 24
        return $this->blameableLocalBehaviors;
434
    }
435
436
    /**
437
     * Set blameable behaviors.
438
     * @param array $behaviors
439
     */
440 6
    protected function setBlameableBehaviors($behaviors = [])
441
    {
442 6
        $this->blameableLocalBehaviors = $behaviors;
443 6
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() 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...
444 6
        if ($cache) {
445 6
            $tagDependencyConfig = ['tags' => [$this->getBlameableBehaviorsCacheTag()]];
446 6
            $tagDependency = new \yii\caching\TagDependency($tagDependencyConfig);
447 6
            $cache->set($this->getBlameableBehaviorsCacheKey(), $behaviors, 0, $tagDependency);
448 6
        }
449 6
    }
450
451
    /**
452
     * Set description.
453
     * @return string description.
454
     */
455
    public function getDescription()
456
    {
457
        $descAttribute = $this->descriptionAttribute;
458
        return is_string($descAttribute) ? $this->$descAttribute : null;
459
    }
460
461
    /**
462
     * Get description.
463
     * @param string $desc description.
464
     * @return string|null description if enabled, or null if disabled.
465
     */
466
    public function setDescription($desc)
467
    {
468
        $descAttribute = $this->descriptionAttribute;
469
        return is_string($descAttribute) ? $this->$descAttribute = $desc : null;
470
    }
471
472
    /**
473
     * Get blame.
474
     * @return \vistart\Models\queries\BaseUserQuery user.
475
     */
476 1
    public function getUser()
477
    {
478 1
        $userClass = $this->userClass;
479 1
        $model = $userClass::buildNoInitModel();
480 1
        return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->createdByAttribute]);
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...
481
    }
482
483
    /**
484
     * This event is triggered before the model update.
485
     * This method is ONLY used for being triggered by event. DO NOT call,
486
     * override or modify it directly, unless you know the consequences.
487
     * @param \yii\base\Event $event
488
     */
489 9
    public function onContentChanged($event)
490
    {
491 9
        $sender = $event->sender;
492 9
        $sender->resetConfirmation();
493 9
    }
494
495
    /**
496
     * Return the current user's GUID if current model doesn't specify the owner
497
     * yet, or return the owner's GUID if current model has been specified.
498
     * This method is ONLY used for being triggered by event. DO NOT call,
499
     * override or modify it directly, unless you know the consequences.
500
     * @param \yii\base\Event $event
501
     * @return string the GUID of current user or the owner.
502
     */
503 24
    public function onGetCurrentUserGuid($event)
504
    {
505 24
        $sender = $event->sender;
506 24
        if (isset($sender->attributes[$sender->createdByAttribute])) {
507 24
            return $sender->attributes[$sender->createdByAttribute];
508
        }
509
        $identity = \Yii::$app->user->identity;
510
        if ($identity) {
511
            $igAttribute = $identity->guidAttribute;
512
            return $identity->$igAttribute;
513
        }
514
    }
515
516
    /**
517
     * Initialize type of content. the first of element[index is 0] of
518
     * $contentTypes will be used.
519
     * @param \yii\base\Event $event
520
     */
521 24
    public function onInitContentType($event)
522
    {
523 24
        $sender = $event->sender;
524 24
        if (!isset($sender->contentTypeAttribute) || !is_string($sender->contentTypeAttribute)) {
525 17
            return;
526
        }
527 7
        $contentTypeAttribute = $sender->contentTypeAttribute;
528 7
        if (!isset($sender->$contentTypeAttribute) &&
529 7
            !empty($sender->contentTypes) &&
530 7
            is_array($sender->contentTypes)) {
531 7
            $sender->$contentTypeAttribute = $sender->contentTypes[0];
532 7
        }
533 7
    }
534
535
    /**
536
     * Initialize description property with $initDescription.
537
     * @param \yii\base\Event $event
538
     */
539 24
    public function onInitDescription($event)
540
    {
541 24
        $sender = $event->sender;
542 24
        if (!isset($sender->descriptionAttribute) || !is_string($sender->descriptionAttribute)) {
543 18
            return;
544
        }
545 6
        $descriptionAttribute = $sender->descriptionAttribute;
546 6
        if (empty($sender->$descriptionAttribute)) {
547 6
            $sender->$descriptionAttribute = $sender->initDescription;
548 6
        }
549 6
    }
550
551
    /**
552
     * Attach events associated with blameable model.
553
     */
554 24
    public function initBlameableEvents()
555
    {
556 24
        $this->on(static::$eventConfirmationChanged, [$this, "onConfirmationChanged"]);
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...
557 24
        $this->on(static::$eventNewRecordCreated, [$this, "onInitConfirmation"]);
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...
558 24
        $contentTypeAttribute = $this->contentTypeAttribute;
559 24
        if (!isset($this->$contentTypeAttribute)) {
560 24
            $this->on(static::$eventNewRecordCreated, [$this, "onInitContentType"]);
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...
561 24
        }
562 24
        $descriptionAttribute = $this->descriptionAttribute;
563 24
        if (!isset($this->$descriptionAttribute)) {
564 24
            $this->on(static::$eventNewRecordCreated, [$this, 'onInitDescription']);
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...
565 24
        }
566 24
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, "onContentChanged"]);
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...
567 24
        $this->initSelfBlameableEvents();
568 24
    }
569
}
570