Passed
Push — master ( a22205...112e67 )
by Gabriel
04:04 queued 13s
created

AbstractElement   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 45.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 38
c 1
b 0
f 0
dl 0
loc 136
ccs 19
cts 42
cp 0.4524
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 2 1
A setForm() 0 5 1
A isGroup() 0 3 1
A getDataFromModel() 0 5 1
A isRequired() 0 3 1
A getData() 0 7 2
A setRequired() 0 5 1
A getForm() 0 3 1
A getDataFromRequest() 0 6 1
A __construct() 0 6 2
A validate() 0 12 6
A isRequestArray() 0 3 1
1
<?php
2
3
namespace Nip\Form\Elements;
4
5
use Nip\Form\AbstractForm;
6
use Nip\Form\Elements\Traits\HasAttributesTrait;
7
use Nip\Form\Elements\Traits\HasDecoratorsTrait;
8
use Nip\Form\Elements\Traits\HasErrorsTrait;
9
use Nip\Form\Elements\Traits\HasOptionsTrait;
10
use Nip\Form\Elements\Traits\HasRendererTrait;
11
use Nip\Form\Elements\Traits\HasTypeTrait;
12
use Nip\Form\Elements\Traits\HasUniqueIdTrait;
13
14
/**
15
 * Class AbstractElement
16
 * @package Nip\Form\Elements
17
 */
18
abstract class AbstractElement implements ElementInterface
19
{
20
    use HasUniqueIdTrait;
21
    use HasAttributesTrait;
22
    use HasOptionsTrait;
23
    use HasDecoratorsTrait;
24
    use HasRendererTrait;
25
    use HasTypeTrait;
0 ignored issues
show
Bug introduced by
The trait Nip\Form\Elements\Traits\HasTypeTrait requires the property $_type which is not provided by Nip\Form\Elements\AbstractElement.
Loading history...
26
    use HasErrorsTrait;
27
28
    protected $_form;
29
30
    protected $_isRequired;
31
    protected $_policies;
32
33
    /**
34
     * AbstractElement constructor.
35
     * @param null $form
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $form is correct as it would always require null to be passed?
Loading history...
36
     */
37 16
    public function __construct($form = null)
38
    {
39 16
        if ($form) {
0 ignored issues
show
introduced by
$form is of type null, thus it always evaluated to false.
Loading history...
40 16
            $this->setForm($form);
41
        }
42 16
        $this->init();
43 16
    }
44
45 16
    public function init()
46
    {
47 16
    }
48
49
50
    /**
51
     * @return AbstractForm
52
     */
53 12
    public function getForm()
54
    {
55 12
        return $this->_form;
56
    }
57
58
    /**
59
     * @param AbstractForm $form
60
     * @return $this
61
     */
62 16
    public function setForm(AbstractForm $form)
63
    {
64 16
        $this->_form = $form;
65
66 16
        return $this;
67
    }
68
69
    /**
70
     * @param $data
71
     * @param string $source
72
     * @return \Nip\Form\Elements\AbstractElement
73
     */
74
    public function getData($data, $source = 'abstract')
75
    {
76
        if ($source == 'model') {
77
            return $this->getDataFromModel($data);
78
        }
79
80
        return $this->getDataFromRequest($data);
81
    }
82
83
    /**
84
     * @param $data
85
     * @return $this
86
     */
87
    public function getDataFromModel($data)
88
    {
89
        $this->setValue($data);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param $request
96
     * @return $this
97
     */
98
    public function getDataFromRequest($request)
99
    {
100
        $request = clean($request);
101
        $this->setValue($request);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param boolean $isRequired
108
     * @return $this
109
     */
110 5
    public function setRequired($isRequired)
111
    {
112 5
        $this->_isRequired = (bool)$isRequired;
113
114 5
        return $this;
115
    }
116
117
    public function validate()
118
    {
119
        if ($this->isRequired() && !$this->getValue()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
            $message = $this->getForm()->getMessageTemplate('no-'.$this->getName());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getName() targeting Nip\Form\Elements\AbstractElement::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121
            if (!$message) {
122
                $translateSlug = 'general.form.errors.required';
123
                $message = app('translator')->translate($translateSlug, ['label' => $this->getLabel()]);
0 ignored issues
show
Bug introduced by
The method translate() does not exist on Nip\Container\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
                $message = app('translator')->/** @scrutinizer ignore-call */ translate($translateSlug, ['label' => $this->getLabel()]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The call to app() has too many arguments starting with 'translator'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
                $message = /** @scrutinizer ignore-call */ app('translator')->translate($translateSlug, ['label' => $this->getLabel()]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
124
                if ($message == $translateSlug) {
125
                    $message = $message ? $message : 'The field `'.$this->getLabel().'` is mandatory.';
126
                }
127
            }
128
            $this->addError($message);
129
        }
130
    }
131
132
    /**
133
     * @return bool
134
     */
135 2
    public function isRequired()
136
    {
137 2
        return (bool)$this->_isRequired;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143 6
    public function isGroup()
144
    {
145 6
        return false;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isRequestArray()
152
    {
153
        return false;
154
    }
155
}
156