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
Push — master ( 115690...6ac2d3 )
by Rick
02:27
created

Hash   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 89
ccs 21
cts 22
cp 0.9545
rs 10

3 Methods

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