Validator::failed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of user_management
4
 * User: Sinan TURGUT <[email protected]>
5
 * Date: 24.06.2019
6
 * php version 7.2
7
 *
8
 * @category Assessment
9
 * @package  UserManagement
10
 * @author   Sinan TURGUT <[email protected]>
11
 * @license  See LICENSE file
12
 * @link     https://dev.sinanturgut.com.tr
13
 */
14
15
namespace UserManagement\Validation;
16
17
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Respect\Validation\Exceptions\NestedValidationException;
0 ignored issues
show
Bug introduced by
The type Respect\Validation\Excep...stedValidationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * Class Validator
22
 * @package UserManagement\Validation
23
 */
24
class Validator
25
{
26
27
    /**
28
     * @var array
29
     */
30
    protected $errors = [];
31
32
    /**
33
     * @param ServerRequestInterface $request
34
     * @param array $rules
35
     * @return $this
36
     */
37
    public function validate(ServerRequestInterface $request, array $rules)
38
    {
39
        foreach ($rules as $field => $rule) {
40
            try {
41
                $rule->setName($field)->assert($request->getParam($field));
42
            } catch (NestedValidationException $e) {
43
                $this->errors[$field] = $e->getMessages();
44
            }
45
        }
46
        $_SESSION['errors'] = $this->errors;
47
        return $this;
48
    }
49
50
    /**
51
     * @param array $values
52
     * @param array $rules
53
     * @return $this
54
     */
55
    public function validateArray(array $values, array $rules)
56
    {
57
        foreach ($rules as $field => $rule) {
58
            try {
59
                $rule->setName($field)->assert($this->getValue($values, $field));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getValue($values, $field) targeting UserManagement\Validation\Validator::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...
60
            } catch (NestedValidationException $e) {
61
                $this->errors[$field] = $e->getMessages();
62
            }
63
        }
64
        $_SESSION['errors'] = $this->errors;
65
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function failed()
74
    {
75
        return ! empty($this->errors);
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getErrors()
82
    {
83
        return $this->errors;
84
    }
85
86
    /**
87
     * @param $values
88
     * @param $field
89
     * @return null
90
     */
91
    private function getValue($values, $field)
92
    {
93
        return isset($values[$field]) ? $values[$field] : null;
94
    }
95
96
}