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   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 70.15 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 47
loc 67
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validateField() 47 63 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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