ContactFilter::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Website\Form;
5
6
use ReCaptcha\ReCaptcha;
7
use Zend\Filter\StringTrim;
8
use Zend\Filter\StripTags;
9
use Zend\InputFilter\Input;
10
use Zend\InputFilter\InputFilter;
11
12
class ContactFilter extends InputFilter
13
{
14
    const NAME = 'name';
15
    const EMAIL = 'email';
16
    const SUBJECT = 'subject';
17
    const COMMENTS = 'comments';
18
    const RECAPTCHA = 'g-recaptcha-response';
19
20
    /**
21
     * @var Recaptcha
22
     */
23
    protected $recaptcha;
24
25
    public function __construct(ReCaptcha $recaptcha)
26
    {
27
        $this->recaptcha = $recaptcha;
0 ignored issues
show
Documentation Bug introduced by
It seems like $recaptcha of type object<ReCaptcha\ReCaptcha> is incompatible with the declared type object<Acelaya\Website\Form\Recaptcha> of property $recaptcha.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        $this->init();
29
    }
30 7
31
    public function init()
32 7
    {
33 7
        $this->add($this->createInput(self::NAME));
34 7
        $this->add($this->createInput(self::EMAIL));
35
        $this->add($this->createInput(self::SUBJECT));
36 7
        $this->add($this->createInput(self::COMMENTS));
37
38 7
        $recaptchaInput = $this->createInput(self::RECAPTCHA);
39 7
        $recaptchaInput->getValidatorChain()->attach(new RecaptchaValidator($this->recaptcha));
40 7
        $this->add($recaptchaInput);
41 7
    }
42
43 7
    /**
44 7
     * @param $name
45 7
     * @param bool|true $required
46 7
     * @return Input
47
     */
48
    protected function createInput($name, $required = true): Input
49
    {
50
        $input = new Input($name);
51
        $input->setRequired($required)
52
              ->getFilterChain()->attach(new StripTags())
53 7
                                ->attach(new StringTrim());
54
        return $input;
55 7
    }
56
}
57