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:27
created

Hash::validate()   B

Complexity

Conditions 12
Paths 12

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 7.4259
cc 12
eloc 13
nc 12
nop 1
crap 12

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 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' : '';
0 ignored issues
show
Unused Code introduced by
$caseSensitive is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
76 12
        if ($this->hashAlgorithm == self::ALGO_MD5 && $this->validateHexString($value, 32)) {
77 2
            return true;
78 10
        } elseif ($this->hashAlgorithm == self::ALGO_SHA1 && $this->validateHexString($value, 40)) {
79 1
            return true;
80 9
        } elseif ($this->hashAlgorithm == self::ALGO_SHA256 && $this->validateHexString($value, 64)) {
81 1
            return true;
82 8
        } elseif ($this->hashAlgorithm == self::ALGO_SHA512 && $this->validateHexString($value, 128)) {
83 1
            return true;
84 7
        } elseif ($this->hashAlgorithm == self::ALGO_CRC32 && $this->validateHexString($value, 8)) {
85 1
            return true;
86
        }
87
88 6
        return $this->error(self::INVALID_FORMAT);
89
    }
90
91
    /**
92
     * @param string $value
93
     * @param int $length
94
     *
95
     * @return bool
96
     */
97 12
    private function validateHexString($value, $length)
98
    {
99 12
        $caseSensitive = $this->allowUppercase ? 'i' : '';
100
101 12
        return preg_match(sprintf('/^[0-9a-f]{%s}$/%s', $length, $caseSensitive), $value) === 1;
102
    }
103
}
104