Completed
Push — master ( a07def...5584d6 )
by Gabriel
04:52 queued 12s
created

AbstractForm::getCache()   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 1
crap 1
1
<?php
2
3
namespace Nip\Form;
4
5
use Nip\Form\Elements\AbstractElement as ElementAbstract;
6
use Nip\View;
7
8
/**
9
 * Class AbstractForm
10
 *
11
 */
12
abstract class AbstractForm
13
{
14
    use Traits\DataProcessingTrait;
15
    use Traits\HasAttributesTrait;
16
    use Traits\HasButtonsTrait;
17
    use Traits\HasCacheTrait;
18
    use Traits\HasClassTrait;
19
    use Traits\HasDisplayGroupsTrait;
20
    use Traits\HasElementsTrait;
21
    use Traits\HasErrorsTrait;
22
    use Traits\HasExecutionMethodsTrait;
23
    use Traits\HasRendererTrait;
24
    use Traits\MagicMethodElementsFormTrait;
25
    use Traits\MessagesTrait;
26
    use Traits\NewElementsMethods;
27
28
    const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
29
    const ENCTYPE_MULTIPART = 'multipart/form-data';
30
31
    /**
32
     * @var array
33
     */
34
    protected $methods = ['delete', 'get', 'post', 'put'];
35
36
    protected $_options = [];
37
38
    protected $_decorators = [];
39
40
    protected $controllerView = false;
41
42
    /**
43
     * AbstractForm constructor.
44
     */
45 27
    public function __construct()
46
    {
47 27
        $this->init();
48 27
        $this->postInit();
49 27
    }
50
51 27
    public function init()
52
    {
53 27
        $this->initAction();
54 27
    }
55
56 27
    protected function initAction()
57
    {
58 27
        if (function_exists('current_url')) {
59
            $this->setAction(current_url());
60
        }
61 27
    }
62
63
    /**
64
     * @param string $action
65
     * @return AbstractForm
66
     */
67
    public function setAction($action)
68
    {
69
        return $this->setAttrib('action', (string)$action);
70
    }
71
72 27
    public function postInit()
73
    {
74 27
    }
75
76
    /**
77
     * @param $name
78
     * @return ElementAbstract|null
79
     */
80 1
    public function __get($name)
81
    {
82 1
        $element = $this->getElement($name);
83 1
        if ($element) {
0 ignored issues
show
introduced by
$element is of type Nip\Form\Elements\AbstractElement, thus it always evaluated to true.
Loading history...
84 1
            return $element;
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * @param $key
92
     * @param $value
93
     * @return $this
94
     */
95
    public function setOption($key, $value)
96
    {
97
        $key = (string)$key;
98
        $this->_options[$key] = $value;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param string $key
105
     * @return mixed|null
106
     */
107
    public function getOption($key)
108
    {
109
        $key = (string)$key;
110
        if (!isset($this->_options[$key])) {
111
            return null;
112
        }
113
114
        return $this->_options[$key];
115
    }
116
117
    /**
118
     * @param $method
119
     * @return AbstractForm
120
     */
121
    public function setMethod($method)
122
    {
123
        if (in_array($method, $this->methods)) {
124
            return $this->setAttrib('method', $method);
125
        }
126
        trigger_error('Method is not valid', E_USER_ERROR);
127
128
        return null;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getName()
135
    {
136
        return get_class($this);
137
    }
138
139
    /**
140
     * @return View|null
141
     */
142
    public function getControllerView()
143
    {
144
        if (!$this->controllerView) {
145
            $this->controllerView = app('kernel')->getDispatcher()->getCurrentController()->getView();
0 ignored issues
show
Unused Code introduced by
The call to app() has too many arguments starting with 'kernel'. ( Ignorable by Annotation )

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

145
            $this->controllerView = /** @scrutinizer ignore-call */ app('kernel')->getDispatcher()->getCurrentController()->getView();

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...
Bug introduced by
The method getDispatcher() 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

145
            $this->controllerView = app('kernel')->/** @scrutinizer ignore-call */ getDispatcher()->getCurrentController()->getView();

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...
146
        }
147
148
        return $this->controllerView;
149
    }
150
}
151