Completed
Pull Request — 1.x (#23)
by Akihito
01:16
created

AbstractForm::__toString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
crap 3.0261
1
<?php
2
/**
3
 * This file is part of the Ray.WebFormModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\WebFormModule;
8
9
use Aura\Filter\FilterFactory;
10
use Aura\Filter\SubjectFilter;
11
use Aura\Html\HelperLocator;
12
use Aura\Html\HelperLocatorFactory;
13
use Aura\Input\AntiCsrfInterface;
14
use Aura\Input\BuilderInterface;
15
use Aura\Input\Fieldset;
16
use Ray\WebFormModule\Exception\CsrfViolationException;
17
use Ray\WebFormModule\Exception\LogicException;
18
19
abstract class AbstractForm extends Fieldset implements FormInterface
20
{
21
    /**
22
     * @var SubjectFilter
23
     */
24
    protected $filter;
25
26
    /**
27
     * @var null | array
28
     */
29
    protected $errorMessages;
30
31
    /**
32
     * @var HelperLocator
33
     */
34
    protected $helper;
35
36
    /**
37
     * @var AntiCsrfInterface
38
     */
39
    protected $antiCsrf;
40
41 24
    public function __construct()
42
    {
43 24
    }
44
45 1
    public function __clone()
46
    {
47 1
        $this->filter = clone $this->filter;
48 1
        $this->init();
49 1
    }
50
51
    /**
52
     * @param BuilderInterface     $builder
53
     * @param FilterFactory        $filterFactory
54
     * @param HelperLocatorFactory $helperFactory
55
     *
56
     * @\Ray\Di\Di\Inject
57
     */
58 24
    public function setBaseDependencies(
59
        BuilderInterface $builder,
60
        FilterFactory $filterFactory,
61
        HelperLocatorFactory $helperFactory
62
    ) {
63 24
        $this->builder = $builder;
0 ignored issues
show
Documentation Bug introduced by
$builder is of type object<Aura\Input\BuilderInterface>, but the property $builder was declared to be of type object<Aura\Input\Builder>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
64 24
        $this->filter = $filterFactory->newSubjectFilter();
65 24
        $this->helper = $helperFactory->newInstance();
66 24
    }
67
68 1
    public function setAntiCsrf(AntiCsrfInterface $antiCsrf)
69
    {
70 1
        $this->antiCsrf = $antiCsrf;
71 1
    }
72
73
    /**
74
     * @\Ray\Di\Di\PostConstruct
75
     */
76 24
    public function postConstruct()
77
    {
78 24
        $this->init();
79 24
        if ($this->antiCsrf instanceof AntiCsrfInterface) {
80 13
            $this->antiCsrf->setField($this);
81
        }
82 24
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 2
    public function input($input)
88
    {
89 2
        return $this->helper->input($this->get($input));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 2
    public function error($input)
96
    {
97 2
        if (! $this->errorMessages) {
98 2
            $failure = $this->filter->getFailures();
99 2
            if ($failure) {
100 1
                $this->errorMessages = $failure->getMessages();
101
            }
102
        }
103
104 2
        if (isset($this->errorMessages[$input])) {
105 1
            return $this->errorMessages[$input][0];
106
        }
107
108 1
        return '';
109
    }
110
111
    /**
112
     * @param array $attr attributes for the form tag
113
     *
114
     * @throws \Aura\Html\Exception\HelperNotFound
115
     * @throws \Aura\Input\Exception\NoSuchInput
116
     *
117
     * @return string
118
     */
119 3
    public function form($attr = [])
120
    {
121 3
        $form = $this->helper->form($attr);
122 3
        if (isset($this->inputs['__csrf_token'])) {
123 1
            $form .= $this->helper->input($this->get('__csrf_token'));
124
        }
125
126 3
        return $form;
127
    }
128
129
    /**
130
     * Applies the filter to a subject.
131
     *
132
     * @param array $data
133
     *
134
     * @throws CsrfViolationException
135
     *
136
     * @return bool
137
     */
138 10
    public function apply(array $data)
139
    {
140 10
        if ($this->antiCsrf && ! $this->antiCsrf->isValid($data)) {
141 1
            throw new CsrfViolationException;
142
        }
143 9
        $this->fill($data);
144
145 9
        return $this->filter->apply($data);
146
    }
147
148
    /**
149
     * Returns all failure messages for all fields.
150
     *
151
     * @return array
152
     */
153 4
    public function getFailureMessages()
154
    {
155 4
        return $this->filter->getFailures()->getMessages();
156
    }
157
158
    /**
159
     * Returns all the fields collection
160
     *
161
     * @return \ArrayIterator
162
     */
163 1
    public function getIterator()
164
    {
165 1
        return new \ArrayIterator($this->inputs);
166
    }
167
168
    /**
169
     * Return form markup string
170
     *
171
     * @return string
172
     */
173 1
    public function __toString()
174
    {
175
        try {
176 1
            if (! $this instanceof ToStringInterface) {
177 1
                throw new LogicException(ToStringInterface::class . ' is not implemented');
178
            }
179
            return $this->toString();
0 ignored issues
show
Bug introduced by
The method toString() does not exist on Ray\WebFormModule\AbstractForm. Did you maybe mean __toString()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
180
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
181 1
        } catch (\Exception $e) {
182 1
            trigger_error($e->getMessage() . PHP_EOL . $e->getTraceAsString(), E_USER_ERROR);
183 1
            return '';
184
        }
185
    }
186
}
187