Completed
Push — master ( fd659c...61fdbb )
by Davide
02:14
created

Validation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 118
ccs 34
cts 34
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 4
A hasErrors() 0 3 1
A getErrors() 0 3 1
A getValidators() 0 3 1
A setValidators() 0 3 1
A getTranslator() 0 3 1
A setTranslator() 0 3 1
A __construct() 0 10 4
1
<?php
2
namespace DavidePastore\Slim\Validation;
3
4
use Respect\Validation\Exceptions\NestedValidationException;
5
6
/**
7
 * Validation for Slim.
8
 */
9
class Validation
10
{
11
12
    /**
13
     * Validators.
14
     *
15
     * @var array
16
     */
17
    protected $validators = [];
18
19
    /**
20
     * The translator to use fro the exception message.
21
     *
22
     * @var callable
23
     */
24
    protected $translator = null;
25
26
    /**
27
     * Errors from the validation.
28
     *
29
     * @var array
30
     */
31
    protected $errors = [];
32
33
    /**
34
     * Create new Validator service provider
35
     *
36
     * @param null|array|ArrayAccess $validators
37
     * @param null|callable $translator
38
     */
39 10
    public function __construct($validators = null, $translator = null)
40
    {
41
        // Set the validators
42 10
        if (is_array($validators) || $validators instanceof ArrayAccess) {
0 ignored issues
show
Bug introduced by
The class DavidePastore\Slim\Validation\ArrayAccess does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
43 9
            $this->validators = $validators;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validators can also be of type object<DavidePastore\Slim\Validation\ArrayAccess>. However, the property $validators is declared as type array. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44 10
        } elseif (is_null($validators)) {
45 1
            $this->validators = [];
46 1
        }
47 10
        $this->translator = $translator;
48 10
    }
49
50
    /**
51
     * Validation middleware invokable class
52
     *
53
     * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
54
     * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
55
     * @param  callable                                 $next     Next middleware
56
     *
57
     * @return \Psr\Http\Message\ResponseInterface
58
     */
59 10
    public function __invoke($request, $response, $next)
60
    {
61 10
        $this->errors = [];
62
63
        //Validate every parameters in the validators array
64 10
        foreach ($this->validators as $key => $validator) {
65 9
          $param = $request->getParam($key);
66
          try {
67 9
              $validator->assert($param);
68 9
          } catch(NestedValidationException $exception) {
69 6
              if($this->translator){
70 2
                  $exception->setParam('translator', $this->translator);
71 2
              }
72 6
              $this->errors[$key] = $exception->getMessages();
73
          }
74 10
        }
75
76 10
        return $next($request, $response);
77
    }
78
79
    /**
80
     * Check if there are any errors.
81
     * @return boolean
82
     */
83 10
    public function hasErrors(){
84 10
        return !empty($this->errors);
85
    }
86
87
    /**
88
     * Get errors.
89
     * @return array The errors array.
90
     */
91 8
    public function getErrors(){
92 8
      return $this->errors;
93
    }
94
95
    /**
96
     * Get validators.
97
     * @return array The validators array.
98
     */
99 10
    public function getValidators(){
100 10
      return $this->validators;
101
    }
102
103
    /**
104
     * Set validators.
105
     * @param array $validators The validators array.
106
     */
107 1
    public function setValidators($validators){
108 1
      $this->validators = $validators;
109 1
    }
110
111
    /**
112
     * Get translator.
113
     * @return callable The translator.
114
     */
115 2
    public function getTranslator(){
116 2
      return $this->translator;
117
    }
118
119
    /**
120
     * Set translator.
121
     * @param callable $translator The translator.
122
     */
123 1
    public function setTranslator($translator){
124 1
      $this->translator = $translator;
125 1
    }
126
}
127