Passed
Push — master ( f52af3...89df9c )
by Mihail
05:09
created

FormAnswerAdd   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 17
loc 95
rs 10
wmc 10
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A before() 0 9 2
A labels() 8 8 1
A rules() 9 9 1
B make() 0 30 5

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\Front\Feedback;
4
5
use Apps\ActiveRecord\FeedbackAnswer;
6
use Apps\Model\Front\Profile\EntityAddNotification;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Helper\Text;
10
11
/**
12
 * Class FormAnswerAdd. Model to work with add answer to feedback post thread
13
 * @package Apps\Model\Front\Feedback
14
 */
15
class FormAnswerAdd extends Model
16
{
17
    public $name;
18
    public $email;
19
    public $message;
20
21
    /** @var \Apps\ActiveRecord\FeedbackPost $_post */
22
    protected $_post;
23
    protected $_userId;
24
    protected $_ip;
25
26
    /**
27
     * FormAnswerAdd constructor. Pass active record of comment post and user id
28
     * @param $recordPost
29
     * @param int $userId
30
     */
31
    public function __construct($recordPost, $userId = 0)
32
    {
33
        $this->_post = $recordPost;
34
        $this->_userId = (int)$userId;
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Define local properties if user is authorized
40
     */
41
    public function before()
42
    {
43
        if ($this->_userId > 0) {
44
            $user = App::$User->identity($this->_userId);
45
            $this->name = $user->getProfile()->nick;
46
            $this->email = $user->getParam('email');
47
        }
48
        $this->_ip = App::$Request->getClientIp();
49
    }
50
51
    /**
52
    * Labels for display form
53
    */
54 View Code Duplication
    public function labels()
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...
55
    {
56
        return [
57
            'name' => __('Name'),
58
            'email' => __('Email'),
59
            'message' => __('Message')
60
        ];
61
    }
62
63
    /**
64
    * Form validation rules
65
    */
66 View Code Duplication
    public function rules()
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...
67
    {
68
        return [
69
            [['name', 'email', 'message'], 'required'],
70
            ['name', 'length_min', 2],
71
            ['email', 'email'],
72
            ['message', 'length_min', 10]
73
        ];
74
    }
75
76
    /**
77
     * Add new row to database and set post is unreaded
78
     */
79
    public function make()
80
    {
81
        // update readed marker
82
        $this->_post->readed = 0;
83
        $this->_post->save();
84
85
        // add new answer row in database
86
        $record = new FeedbackAnswer();
87
        $record->feedback_id = $this->_post->id;
88
        $record->name = App::$Security->strip_tags($this->name);
89
        $record->email = App::$Security->strip_tags($this->email);
90
        $record->message = App::$Security->strip_tags($this->message);
91
        if ($this->_userId > 0) {
92
            $record->user_id = $this->_userId;
93
        }
94
        $record->ip = $this->_ip;
95
        $record->save();
96
97
        // add notification msg
98
        $targetId = $this->_post->user_id;
99
        if ($targetId !== null && (int)$targetId > 0 && $targetId !== $this->_userId) {
100
            $notify = new EntityAddNotification($targetId);
0 ignored issues
show
Documentation introduced by
$targetId is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
            $uri = '/feedback/read/' . $this->_post->id . '/' . $this->_post->hash . '#feedback-answer-' . $record->id;
102
            $notify->add($uri,  EntityAddNotification::MSG_ADD_FEEDBACKANSWER, [
103
                'snippet' => Text::snippet(App::$Security->strip_tags($this->message), 50),
0 ignored issues
show
Bug introduced by
It seems like \Ffcms\Core\App::$Securi...ip_tags($this->message) targeting Ffcms\Core\Helper\Security::strip_tags() can also be of type array; however, Ffcms\Core\Helper\Text::snippet() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
                'post' => Text::snippet(App::$Security->strip_tags($this->_post->message), 50)
0 ignored issues
show
Bug introduced by
It seems like \Ffcms\Core\App::$Securi...($this->_post->message) targeting Ffcms\Core\Helper\Security::strip_tags() can also be of type array; however, Ffcms\Core\Helper\Text::snippet() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
105
            ]);
106
        }
107
108
    }
109
}