Comment   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 17.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 44.44%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 6
c 4
b 0
f 1
lcom 1
cbo 6
dl 13
loc 76
ccs 4
cts 9
cp 0.4444
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A canView() 0 4 1
A canEdit() 0 4 3
A can() 13 13 2

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
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Project\Issue;
13
14
use Illuminate\Database\Eloquent\Collection;
15
use Illuminate\Database\Eloquent\Model as BaseModel;
16
use Tinyissue\Contracts\Model\AccessControl;
17
use Tinyissue\Model\Message\Queue;
18
use Tinyissue\Model\Permission;
19
use Tinyissue\Model\Project\Issue;
20
use Tinyissue\Model\Traits\Project\Issue\Comment\CrudTrait;
21
use Tinyissue\Model\Traits\Project\Issue\Comment\QueueTrait;
22
use Tinyissue\Model\Traits\Project\Issue\Comment\RelationTrait;
23
use Tinyissue\Model\User;
24
25
/**
26
 * Comment is model class for project issue comments.
27
 *
28
 * @author Mohamed Alsharaf <[email protected]>
29
 *
30
 * @property int $id
31
 * @property int $issue_id
32
 * @property int $project_id
33
 * @property string $comment
34
 * @property int $created_by
35
 * @property User $user
36
 * @property Issue $issue
37
 * @property Collection $attachments
38
 * @property User\Activity $activity
39
 * @property Queue $messagesQueue
40
 */
41
class Comment extends BaseModel implements AccessControl
42
{
43
    use CrudTrait,
44
        RelationTrait,
45
        QueueTrait;
46
47
    /**
48
     * Timestamp enabled.
49
     *
50
     * @var bool
51
     */
52
    public $timestamps = true;
53
54
    /**
55
     * Name of database table.
56
     *
57
     * @var string
58
     */
59
    protected $table = 'projects_issues_comments';
60
61
    /**
62
     * List of allowed columns to be used in $this->fill().
63
     *
64
     * @var array
65
     */
66
    protected $fillable = [
67
        'created_by',
68
        'project_id',
69
        'issue_id',
70
        'comment',
71
    ];
72
73
    /**
74
     * Whether a user can view the issue.
75
     *
76
     * @param User $user
77
     *
78
     * @return bool
79
     */
80 1
    public function canView(User $user)
81
    {
82 1
        return $this->issue->canView($user);
83
    }
84
85
    /**
86
     * Whether a user can edit the comment.
87
     *
88
     * @param User $user
89
     *
90
     * @return bool
91
     */
92 8
    public function canEdit(User $user)
93
    {
94 8
        return $user->id === $this->created_by || ($this->canView($user) && $user->permission(Permission::PERM_ISSUE_MODIFY));
95
    }
96
97
    /**
98
     * @param string $permission
99
     * @param User   $user
100
     *
101
     * @return bool
102
     */
103 View Code Duplication
    public function can($permission, User $user)
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...
104
    {
105
        $editPermissions = [
106
            Permission::PERM_ISSUE_COMMENT,
107
            Permission::PERM_ISSUE_MODIFY,
108
        ];
109
110
        if (in_array($permission, $editPermissions)) {
111
            return $this->canEdit($user);
112
        }
113
114
        return $this->canView($user);
115
    }
116
}
117