Test Failed
Pull Request — master (#5)
by Anton
02:48
created

ReadableBehavior   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 30.19 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 16
loc 53
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A markAsRead() 8 8 2
A markAsUnread() 8 8 2
A isRead() 0 4 1
A isUnread() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\notifications\behaviors;
7
8
9
use yii\base\Behavior;
10
use yii\db\ActiveRecordInterface;
11
12
class ReadableBehavior extends Behavior
13
{
14
15
    public $readAttribute = 'read_at';
16
17
    /**
18
     * Mark the notification as read.
19
     * @throws \Exception
20
     * @throws \Throwable
21
     */
22 View Code Duplication
    public function markAsRead()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        /** @var ActiveRecordInterface $model */
25
        $model = $this->owner;
26
        if (is_null($model->{$this->readAttribute})) {
27
            $model->update(false, [$this->readAttribute => date('Y-m-d H:i:s')]);
28
        }
29
    }
30
31
    /**
32
     * Mark the notification as unread.
33
     * @return void
34
     * @throws \Exception
35
     * @throws \Throwable
36
     */
37 View Code Duplication
    public function markAsUnread()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        /** @var ActiveRecordInterface $model */
40
        $model = $this->owner;
41
        if (!is_null($model->{$this->readAttribute})) {
42
            $model->update(false, [$this->readAttribute => null]);
43
        }
44
    }
45
46
    /**
47
     * Determine if a notification has been read.
48
     *
49
     * @return bool
50
     */
51
    public function isRead()
52
    {
53
        return $this->owner->{$this->readAttribute} !== null;
54
    }
55
    /**
56
     * Determine if a notification has not been read.
57
     *
58
     * @return bool
59
     */
60
    public function isUnread()
61
    {
62
        return $this->owner->{$this->readAttribute} === null;
63
    }
64
}