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
07:37
created

Hash::validate()   B

Complexity

Conditions 11
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 16
rs 7.1162
cc 11
eloc 12
nc 6
nop 1

How to fix   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
 * 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
    public function __construct($hashAlgorithm, $allowUppercase = false)
57
    {
58
        $this->hashAlgorithm = $hashAlgorithm;
59
        $this->allowUppercase = $allowUppercase;
60
61
        $this->messageTemplates = [
62
            self::INVALID_FORMAT => sprintf('{{ name }} must be a valid %s hash', $hashAlgorithm)
63
        ];
64
    }
65
66
    /**
67
     * Validates if the value is a valid cryptographic hash.
68
     *
69
     * @param mixed $value
70
     * @return bool
71
     */
72
    public function validate($value)
73
    {
74
        if ($this->hashAlgorithm == self::ALGO_MD5 && $this->validateHexString($value, 32)) {
75
            return true;
76
        } elseif ($this->hashAlgorithm == self::ALGO_SHA1 && $this->validateHexString($value, 40)) {
77
            return true;
78
        } elseif ($this->hashAlgorithm == self::ALGO_SHA256 && $this->validateHexString($value, 64)) {
79
            return true;
80
        } elseif ($this->hashAlgorithm == self::ALGO_SHA512 && $this->validateHexString($value, 128)) {
81
            return true;
82
        } elseif ($this->hashAlgorithm == self::ALGO_CRC32 && $this->validateHexString($value, 8)) {
83
            return true;
84
        }
85
86
        return $this->error(self::INVALID_FORMAT);
87
    }
88
89
    /**
90
     * @param string $value
91
     * @param int $length
92
     *
93
     * @return bool
94
     */
95
    private function validateHexString($value, $length)
96
    {
97
        $caseSensitive = $this->allowUppercase ? 'i' : '';
98
99
        return preg_match(sprintf('/^[0-9a-f]{%s}$/%s', $length, $caseSensitive), $value) === 1;
100
    }
101
}
102