GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LengthValidator::validateField()   C
last analyzed

Complexity

Conditions 11
Paths 14

Size

Total Lines 63
Code Lines 36

Duplication

Lines 47
Ratio 74.6 %

Importance

Changes 0
Metric Value
dl 47
loc 63
rs 6.1137
c 0
b 0
f 0
cc 11
eloc 36
nc 14
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo\Validator;
13
14
use Sokil\Mongo\Structure;
15
16
class LengthValidator extends \Sokil\Mongo\Validator
17
{
18
19
    public function validateField(Structure $document, $fieldName, array $params)
20
    {
21
        $value = $document->get($fieldName);
22
23
        if (!$value) {
24
            return;
25
        }
26
27
        $length = mb_strlen($value);
28
29
        // check if field is of specified length
30 View Code Duplication
        if (isset($params['is'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            if ($length === $params['is']) {
32
                return;
33
            }
34
35
            if (!isset($params['message'])) {
36
                $params['message'] = sprintf(
37
                    'Field "%s" length not equal to %s in model %s',
38
                    $fieldName,
39
                    $params['is'],
40
                    get_called_class()
41
                );
42
            }
43
44
            $document->addError($fieldName, $this->getName(), $params['message']);
45
            return;
46
        }
47
48
        // check if fied is shorter than required
49 View Code Duplication
        if (isset($params['min'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            if ($length < $params['min']) {
51
                if (!isset($params['messageTooShort'])) {
52
                    $params['messageTooShort'] = sprintf(
53
                        'Field "%s" length is shorter than %s in model %s',
54
                        $fieldName,
55
                        $params['min'],
56
                        get_called_class()
57
                    );
58
                }
59
60
                $document->addError($fieldName, $this->getName(), $params['messageTooShort']);
61
                return;
62
            }
63
        }
64
65
        // check if fied is longer than required
66 View Code Duplication
        if (isset($params['max'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            if ($length > $params['max']) {
68
                if (!isset($params['messageTooLong'])) {
69
                    $params['messageTooLong'] = sprintf(
70
                        'Field "%s" length is longer than %s in model %s',
71
                        $fieldName,
72
                        $params['max'],
73
                        get_called_class()
74
                    );
75
                }
76
77
                $document->addError($fieldName, $this->getName(), $params['messageTooLong']);
78
                return;
79
            }
80
        }
81
    }
82
}
83