Passed
Push — master ( 68443e...939347 )
by Mihail
04:31
created

FormFeedbackAdd   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 9.89 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A before() 0 8 2
A labels() 9 9 1
A rules() 0 14 2
A make() 0 21 2
A getHash() 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
namespace Apps\Model\Front\Feedback;
4
5
use Apps\ActiveRecord\FeedbackPost;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\Type\Str;
9
10
class FormFeedbackAdd extends Model
11
{
12
    public $name;
13
    public $email;
14
    public $captcha;
15
    public $message;
16
17
    private $_hash;
18
19
    private $_useCaptcha = true;
20
21
    public function __construct($captcha = true)
22
    {
23
        $this->_useCaptcha = (bool)$captcha;
24
        parent::__construct();
25
    }
26
27
28
    /**
29
    * Set user data if he is logged in
30
    */
31
    public function before()
32
    {
33
        if (App::$User->isAuth()) {
34
            $data = App::$User->identity();
35
            $this->name = $data->getProfile()->nick;
36
            $this->email = $data->email;
0 ignored issues
show
Bug introduced by
Accessing email on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
37
        }
38
    }
39
40
    /**
41
    * Labels to display form
42
    */
43 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...
44
    {
45
        return [
46
            'name' => __('Name'),
47
            'email' => __('Email'),
48
            'message' => __('Message'),
49
            'captcha' => __('Captcha')
50
        ];
51
    }
52
53
    /**
54
    * Rules to validate send form
55
    */
56
    public function rules()
57
    {
58
        $rules = [
59
            [['name', 'email', 'message'], 'required'],
60
            ['name', 'length_min', '2'],
61
            ['message', 'length_min', 10],
62
            ['email', 'email'],
63
            ['captcha', 'used']
64
        ];
65
        if (true === $this->_useCaptcha) {
66
            $rules[] = ['captcha', 'App::$Captcha::validate'];
67
        }
68
        return $rules;
69
    }
70
71
    public function make()
72
    {
73
        // calculate security hash to direct-on access
74
        $hash = Str::randomLatinNumeric(mt_rand(16, 64));
75
76
        // init new row and set row data
77
        $record = new FeedbackPost();
78
        $record->name = App::$Security->strip_tags($this->name);
79
        $record->email = App::$Security->strip_tags($this->email);
80
        $record->message = App::$Security->strip_tags($this->message);
81
        $record->hash = $hash;
82
        if (App::$User->isAuth()) {
83
            $record->user_id = App::$User->identity()->getId();
84
        }
85
86
        $record->ip = App::$Request->getClientIp();
87
        // save row to db
88
        $record->save();
89
90
        return $record;
91
    }
92
93
    /**
94
     * @return string|null
95
     */
96
    public function getHash()
97
    {
98
        return $this->_hash;
99
    }
100
}