Passed
Push — master ( 8f5376...4142cd )
by Mihail
12:41
created

CommentAdd   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 127
Duplicated Lines 18.9 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 9
dl 24
loc 127
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A before() 0 8 2
C check() 24 59 13
B make() 0 27 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
namespace Apps\Model\Api\Comments;
4
5
use Apps\ActiveRecord\CommentAnswer;
6
use Apps\ActiveRecord\CommentPost;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Exception\JsonException;
10
use Ffcms\Core\Helper\Date;
11
use Ffcms\Core\Helper\Type\Str;
12
13
class CommentAdd extends Model
14
{
15
    public $pathway;
16
    public $message;
17
    public $replayTo = 0;
18
    public $guestName;
19
    public $ip;
20
    public $user_id = 0;
21
22
    public $response = [
23
        'status' => 0,
24
        'message' => 'unknown error'
25
    ];
26
27
    private $_configs = [];
28
29
    /**
30
     * CommentAdd constructor. Pass add comment data inside model
31
     * @param array|null $configs
32
     */
33
    public function __construct(array $configs = null)
34
    {
35
        $this->_configs = $configs;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configs can be null. However, the property $_configs is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Get and store to model properties other data
41
     */
42
    public function before()
43
    {
44
        $this->ip = App::$Request->getClientIp();
45
        if (App::$User->isAuth()) {
46
            $this->user_id = App::$User->identity()->getId();
47
        }
48
        parent::before();
49
    }
50
51
    public function check()
52
    {
53
        // check if user is auth'd or guest name is defined
54 View Code Duplication
        if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            throw new JsonException(__('Guest name is not defined'));
56
        }
57
58
        // check if pathway is empty
59
        if (Str::likeEmpty($this->pathway)) {
60
            throw new JsonException(__('Wrong target pathway'));
61
        }
62
63
        // check if message length is correct
64 View Code Duplication
        if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            throw new JsonException(__('Message length is incorrect. Current: %cur% , min - %min%, max - %max%', [
66
                'cur' => Str::length($this->message),
67
                'min' => $this->_configs['minLength'],
68
                'max' => $this->_configs['maxLength']
69
            ]));
70
        }
71
72
        // sounds like answer, lets try to find post thread comment
73
        if ($this->replayTo > 0) {
74
            $count = CommentPost::where('id', '=', $this->replayTo)->count();
75
            if ($count !== 1) {
76
                throw new JsonException(__('Comment post thread is not founded'));
77
            }
78
            // check for prevent spam
79
            $query = CommentAnswer::where(function($q) {
80
                $q->where('user_id', '=', $this->user_id)
81
                    ->orWhere('ip', '=', $this->ip);
82
            })->orderBy('created_at', 'DESC')
83
            ->first();
84
85
            // something is founded :D
86 View Code Duplication
            if ($query !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                $answerTime = Date::convertToTimestamp($query->created_at);
88
                $delay = $answerTime + $this->_configs['delay'] - time();
89
                if ($delay > 0) { // sounds like config time is not passed now
90
                    throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay]));
91
                }
92
            }
93
        } else { // sounds like post, lets try to check latest post
94
            $query = CommentPost::where(function($q) {
95
                $q->where('user_id', '=', $this->user_id)
96
                    ->orWhere('ip', '=', $this->ip);
97
            })->orderBy('created_at', 'DESC')
98
            ->first();
99
100
            // check if latest post time for this user is founded
101 View Code Duplication
            if ($query !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
                $postTime = Date::convertToTimestamp($query->created_at);
103
                $delay = $postTime + $this->_configs['delay'] - time();
104
                if ($delay > 0) {
105
                    throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay]));
106
                }
107
            }
108
        }
109
    }
110
111
    public function make()
112
    {
113
        $record = null;
0 ignored issues
show
Unused Code introduced by
$record is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
        $type = null;
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
        // sounds like answer
116
        if ($this->replayTo > 0) {
117
            $type = 'answer';
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
            $record = new CommentAnswer();
119
            $record->comment_id = $this->replayTo;
120
            $record->user_id = $this->user_id;
121
            $record->guest_name = $this->guestName;
122
            $record->message = $this->message;
123
            $record->ip = $this->ip;
124
            $record->save();
125
        } else { // sounds like new post
126
            $type = 'post';
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
            $record = new CommentPost();
128
            $record->pathway = $this->pathway;
129
            $record->user_id = $this->user_id;
130
            $record->guest_name = $this->guestName;
131
            $record->message = $this->message;
132
            $record->lang = App::$Request->getLanguage();
133
            $record->save();
134
        }
135
136
137
    }
138
139
}