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.
Completed
Pull Request — master (#161)
by
unknown
03:30
created

Length   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 6.02 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 5
loc 83
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B validate() 5 16 5
A getMessageParameters() 0 6 1

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
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
13
/**
14
 * This rule is for validating the exact length of a string.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class Length extends Rule
19
{
20
    /**
21
     * A constant that will be used for the error message when the value is too short.
22
     */
23
    const TOO_SHORT = 'Length::TOO_SHORT';
24
25
    /**
26
     * A constant that will be used for the error message when the value is too long.
27
     */
28
    const TOO_LONG = 'Length::TOO_LONG';
29
30
    /**
31
     * The message templates which can be returned by this validator.
32
     *
33
     * @var array
34
     */
35
    protected $messageTemplates = [
36
        self::TOO_SHORT => '{{ name }} is too short and must be {{ length }} characters long',
37
        self::TOO_LONG => '{{ name }} is too long and must be {{ length }} characters long',
38
    ];
39
40
    /**
41
     * The length the value should have.
42
     *
43
     * @var int
44
     */
45
    protected $length;
46
47
    /**
48
     * The encoding to be used for multibyte string functions.
49
     *
50
     * @var null|string
51
     */
52
    protected $encoding;
53
54
    /**
55
     * Construct the Length validator.
56
     *
57
     * @param int $length
58
     * @param null|string $encoding
59
     */
60 15
    public function __construct($length, $encoding = null)
61
    {
62 15
        $this->length = $length;
63 15
        $this->encoding = $encoding;
64 15
    }
65
66
    /**
67
     * Attempts to see if the length of the value is exactly the number expected and returns the result as a bool.
68
     *
69
     * @param mixed $value
70
     * @return bool
71
     */
72 12
    public function validate($value)
73
    {
74 12 View Code Duplication
        if (is_null($this->encoding) || !function_exists('mb_strlen')) {
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...
75 6
            $actualLength = strlen($value);
76 6
        } else {
77 6
            $actualLength = mb_strlen($value, $this->encoding);
78
        }
79
        
80 12
        if ($actualLength > $this->length) {
81 1
            return $this->error(self::TOO_LONG);
82
        }
83 11
        if ($actualLength < $this->length) {
84 7
            return $this->error(self::TOO_SHORT);
85
        }
86 4
        return true;
87
    }
88
89
    /**
90
     * Returns the parameters that may be used in a validation message.
91
     *
92
     * @return array
93
     */
94 8
    protected function getMessageParameters()
95
    {
96 8
        return array_merge(parent::getMessageParameters(), [
97 8
            'length' => $this->length
98 8
        ]);
99
    }
100
}
101