CheckToken   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B validate() 0 12 5
1
<?php
2
3
namespace mindplay\kissform\Validators;
4
5
use mindplay\kissform\Facets\FieldInterface;
6
use mindplay\kissform\Facets\TokenServiceInterface;
7
use mindplay\kissform\Facets\ValidatorInterface;
8
use mindplay\kissform\InputModel;
9
use mindplay\kissform\InputValidation;
10
use mindplay\lang;
11
12
/**
13
 * This validator is auto-generated by TokenField - you can validate the CSRF token by
14
 * simply calling Validation::check() with your TokenField instance, it will create the
15
 * CheckToken validator automatically. See "examples/demo.php" for a working sample.
16
 */
17
class CheckToken implements ValidatorInterface
18
{
19
    /**
20
     * @var TokenServiceInterface
21
     */
22
    private $service;
23
24
    /**
25
     * @var string|null
26
     */
27
    private $error;
28
29
    /**
30
     * @param TokenServiceInterface $service
31
     * @param string|null           $error optional custom error message
32
     */
33 2
    public function __construct(TokenServiceInterface $service, $error = null)
34
    {
35 2
        $this->service = $service;
36 2
        $this->error = $error;
37 2
    }
38
39 2
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
40
    {
41 2
        $input = $model->getInput($field);
42
        
43 2
        if ($input === null) {
44 1
            $model->setError($field, $this->error ?: lang::text("mindplay/kissform", "noToken"));
45
        }
46
47 2
        if (! $this->service->checkToken($field->getName(), $input)) {
0 ignored issues
show
Bug introduced by
It seems like $input defined by $model->getInput($field) on line 41 can also be of type array or null; however, mindplay\kissform\Facets...Interface::checkToken() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48 2
            $model->setError($field, $this->error ?: lang::text("mindplay/kissform", "token"));
49
        }
50 2
    }
51
}
52