Passed
Push — master ( eb0fcb...47bdab )
by Gabriel
04:01 queued 10s
created

AbstractElement::getForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 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\HasOptionsTrait;
9
use Nip\Form\Elements\Traits\HasRendererTrait;
10
use Nip\Form\Elements\Traits\HasTypeTrait;
11
use Nip\Form\Elements\Traits\HasUniqueIdTrait;
12
13
/**
14
 * Class AbstractElement
15
 * @package Nip\Form\Elements
16
 */
17
abstract class AbstractElement implements ElementInterface
18
{
19
    use HasUniqueIdTrait;
20
    use HasAttributesTrait;
21
    use HasOptionsTrait;
22
    use HasDecoratorsTrait;
23
    use HasRendererTrait;
24
    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...
25
26
    protected $_form;
27
28
    protected $_isRequired;
29
    protected $_errors = [];
30
    protected $_policies;
31
32
    /**
33
     * AbstractElement constructor.
34
     * @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...
35
     */
36 10
    public function __construct($form = null)
37
    {
38 10
        if ($form) {
0 ignored issues
show
introduced by
$form is of type null, thus it always evaluated to false.
Loading history...
39 10
            $this->setForm($form);
40
        }
41 10
        $this->init();
42 10
    }
43
44 10
    public function init()
45
    {
46 10
    }
47
48
49
    /**
50
     * @return AbstractForm
51
     */
52 9
    public function getForm()
53
    {
54 9
        return $this->_form;
55
    }
56
57
    /**
58
     * @param AbstractForm $form
59
     * @return $this
60
     */
61 10
    public function setForm(AbstractForm $form)
62
    {
63 10
        $this->_form = $form;
64
65 10
        return $this;
66
    }
67
68
    /**
69
     * @param $data
70
     * @param string $source
71
     * @return Nip_Form_Element_Abstract
0 ignored issues
show
Bug introduced by
The type Nip\Form\Elements\Nip_Form_Element_Abstract was not found. Did you mean Nip_Form_Element_Abstract? If so, make sure to prefix the type with \.
Loading history...
72
     */
73
    public function getData($data, $source = 'abstract')
74
    {
75
        if ($source == 'model') {
76
            return $this->getDataFromModel($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getDataFromModel($data) returns the type Nip\Form\Elements\AbstractElement which is incompatible with the documented return type Nip\Form\Elements\Nip_Form_Element_Abstract.
Loading history...
77
        }
78
79
        return $this->getDataFromRequest($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getDataFromRequest($data) returns the type Nip\Form\Elements\AbstractElement which is incompatible with the documented return type Nip\Form\Elements\Nip_Form_Element_Abstract.
Loading history...
80
    }
81
82
    /**
83
     * @param $data
84
     * @return $this
85
     */
86
    public function getDataFromModel($data)
87
    {
88
        $this->setValue($data);
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param $request
95
     * @return $this
96
     */
97
    public function getDataFromRequest($request)
98
    {
99
        $request = clean($request);
0 ignored issues
show
Bug introduced by
The function clean was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

99
        $request = /** @scrutinizer ignore-call */ clean($request);
Loading history...
100
        $this->setValue($request);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param boolean $isRequired
107
     * @return $this
108
     */
109 3
    public function setRequired($isRequired)
110
    {
111 3
        $this->_isRequired = (bool)$isRequired;
112
113 3
        return $this;
114
    }
115
116
    public function validate()
117
    {
118
        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...
119
            $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...
120
            if (!$message) {
121
                $translateSlug = 'general.form.errors.required';
122
                $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

122
                $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...
123
                if ($message == $translateSlug) {
124
                    $message = $message ? $message : 'The field `'.$this->getLabel().'` is mandatory.';
125
                }
126
            }
127
            $this->addError($message);
128
        }
129
    }
130
131
    /**
132
     * @return bool
133
     */
134 1
    public function isRequired()
135
    {
136 1
        return (bool)$this->_isRequired;
137
    }
138
139
    /**
140
     * @param $message
141
     * @return $this
142
     */
143
    public function addError($message)
144
    {
145
        $this->_errors[] = $message;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    public function getErrors()
154
    {
155
        return $this->_errors;
156
    }
157
158
    /**
159
     * @return bool
160
     */
161 6
    public function isError()
162
    {
163 6
        return count($this->_errors) > 0;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169 4
    public function isGroup()
170
    {
171 4
        return false;
172
    }
173
}
174