UserValidator::validatePassword()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4286
cc 2
eloc 9
nc 2
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
use NilPortugues\Validator\Validator;
3
4
include realpath(dirname(__FILE__)).'/../vendor/autoload.php';
5
6
class Request
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @var string
10
     */
11
    public $username;
12
13
    /**
14
     * @var string
15
     */
16
    public $password;
17
18
    /**
19
     * @var string
20
     */
21
    public $email;
22
23
    /**
24
     * @var int
25
     */
26
    public $gender;
27
28
    /**
29
     * @param string $username
30
     * @param string $password
31
     * @param string $email
32
     * @param string $gender
33
     */
34
    public function __construct($username, $password, $email, $gender)
35
    {
36
        settype($username, 'string');
37
        settype($password, 'string');
38
        settype($email, 'string');
39
        settype($gender, 'int');
40
41
        $this->username = $username;
42
        $this->password = $password;
43
        $this->email    = $email;
44
        $this->gender   = $gender;
45
    }
46
}
47
48
class UserValidator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
49
{
50
    /**
51
     * @var NilPortugues\Validator\Validator
52
     */
53
    private static $validator;
54
55
    /**
56
     * @var array
57
     */
58
    private $errors = [];
59
60
    /**
61
     * @var bool
62
     */
63
    private $isValid = true;
64
65
    /**
66
     * 
67
     */
68
    public function __construct()
69
    {
70
        if(!isset(self::$validator)) {
71
            self::$validator = Validator::create();
72
        }
73
    }
74
75
    /**
76
     * @param Request $request
77
     *
78
     * @return bool
79
     */
80
    public function validate(Request $request)
81
    {
82
        $this->validateUsername($request);
83
        $this->validatePassword($request);
84
        $this->validateEmail($request);
85
        $this->validateGender($request);
86
87
        return $this->isValid;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getErrors()
94
    {
95
        return $this->errors;
96
    }
97
98
    /**
99
     * @param Request $request
100
     *
101
     * @return boolean|null
102
     */
103
    private function validateGender(Request $request)
104
    {
105
        $gender = self::$validator->isInteger('gender');
106
107
        $result = $gender
108
            ->isRequired()
109
            ->isPositive()
110
            ->isBetween(0, 2, true)
111
            ->validate($request->gender);
112
113
        $this->isValid = $this->isValid && $result;
114
        $this->errors  = array_merge($this->errors, $gender->getErrors());
115
    }
116
117
    /**
118
     * @param Request $request
119
     *
120
     * @return boolean|null
121
     */
122
    private function validateEmail(Request $request)
123
    {
124
        $email = self::$validator->isString('email');
125
126
        $result = $email
127
            ->isRequired()
128
            ->isEmail()
129
            ->validate($request->email);
130
131
        $this->isValid = $this->isValid && $result;
132
        $this->errors  = array_merge($this->errors, $email->getErrors());
133
    }
134
135
    /**
136
     * @param Request $request
137
     *
138
     * @return boolean|null
139
     */
140
    private function validatePassword(Request $request)
141
    {
142
        $password = self::$validator->isString('password');
143
144
        $result = $password
145
            ->isRequired()
146
            ->isBetween(5, 72, true)
147
            ->isAlphanumeric()
148
            ->validate($request->password);
149
150
        $this->isValid = $this->isValid && $result;
151
        $this->errors  = array_merge($this->errors, $password->getErrors());
152
    }
153
154
    /**
155
     * Validates is a username has alphanumeric values and is between 3 to 12 characters long.
156
     *
157
     * @param Request $request
158
     *
159
     * @return boolean|null
160
     */
161
    private function validateUsername(Request $request)
162
    {
163
        $username = self::$validator->isString('username');
164
165
        $result = $username
166
            ->isRequired()
167
            ->isAlpha()
168
            ->isBetween(4, 12, true)
169
            ->validate($request->username);
170
171
        $this->isValid = $this->isValid && $result;
172
        $this->errors  = array_merge($this->errors, $username->getErrors());
173
    }
174
}
175
176
177
178
$userValidator = new UserValidator();
179
180
$request1 = new Request('nilportugues', 'password', '[email protected]', '1');
181
$request2 = new Request('nil', 'password', 'not-an-email.com', '');
182
183
var_dump($userValidator->validate($request1)); //true
0 ignored issues
show
Security Debugging Code introduced by
var_dump($userValidator->validate($request1)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
184
print_r($userValidator->getErrors()); //no errors
185
186
var_dump($userValidator->validate($request2)); //false
187
print_r($userValidator->getErrors()); //array with errors
188