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 (#122)
by Carlos
02:25
created

Hash   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 28.09 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 25
loc 89
ccs 32
cts 32
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C validate() 25 34 12

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-2015 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 if a value is a valid cryptographic hash.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class Hash extends Rule
19
{
20
    const ALGO_MD5 = 'md5';
21
    const ALGO_SHA1 = 'sha1';
22
    const ALGO_SHA256 = 'sha256';
23
    const ALGO_SHA512 = 'sha512';
24
    const ALGO_CRC32 = 'crc32';
25
26
    /**
27
     * A constant that will be used when the value is not a valid cryptographic hash.
28
     */
29
    const INVALID_FORMAT = 'Hash::INVALID_FORMAT';
30
31
    /**
32
     * The message templates which can be returned by this validator.
33
     *
34
     * @var array
35
     */
36
    protected $messageTemplates = [
37
        self::INVALID_FORMAT => '{{ name }} must be a valid hash'
38
    ];
39
40
    /**
41
     * @var string
42
     */
43
    protected $hashAlgorithm;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $allowUppercase;
49
50
    /**
51
     * Construct the Hash validator.
52
     *
53
     * @param string $hashAlgorithm
54
     * @param bool $allowUppercase
55
     */
56 12
    public function __construct($hashAlgorithm, $allowUppercase = false)
57
    {
58 12
        $this->hashAlgorithm = $hashAlgorithm;
59 12
        $this->allowUppercase = $allowUppercase;
60
61 12
        $this->messageTemplates = [
62 12
            self::INVALID_FORMAT => sprintf('{{ name }} must be a valid %s hash', $hashAlgorithm)
63 12
        ];
64 12
    }
65
66
    /**
67
     * Validates if the value is a valid cryptographic hash.
68
     *
69
     * @param mixed $value
70
     * @return bool
71
     */
72 12
    public function validate($value)
73
    {
74 12
        $caseSensitive = $this->allowUppercase ? 'i' : '';
75
76 12
        switch ($this->hashAlgorithm) {
77 12 View Code Duplication
            case self::ALGO_MD5:
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...
78 4
                if (preg_match('/^[0-9a-f]{32}$/' . $caseSensitive, $value) === 1) {
79 2
                    return true;
80
                }
81 2
                break;
82 8 View Code Duplication
            case self::ALGO_SHA1:
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...
83 2
                if (preg_match('/^[0-9a-f]{40}$/' . $caseSensitive, $value) === 1) {
84 1
                    return true;
85
                }
86 1
                break;
87 6 View Code Duplication
            case self::ALGO_SHA256:
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...
88 2
                if (preg_match('/^[0-9a-f]{64}$/' . $caseSensitive, $value) === 1) {
89 1
                    return true;
90
                }
91 1
                break;
92 4 View Code Duplication
            case self::ALGO_SHA512:
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...
93 2
                if (preg_match('/^[0-9a-f]{128}$/' . $caseSensitive, $value) === 1) {
94 1
                    return true;
95
                }
96 1
                break;
97 2 View Code Duplication
            case self::ALGO_CRC32:
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...
98 2
                if (preg_match('/^[0-9a-f]{8}$/' . $caseSensitive, $value) === 1) {
99 1
                    return true;
100
                }
101 1
                break;
102 6
        }
103
104 6
        return $this->error(self::INVALID_FORMAT);
105
    }
106
}
107