Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#661)
by Henrique
06:09 queued 01:35
created

CreditCard::verifyBrand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
use Respect\Validation\Exceptions\ComponentException;
15
16
class CreditCard extends AbstractRule
17
{
18
    /**
19
     * @var string
20
     */
21
    public $brand;
22
23
    /**
24
     * @var array
25
     */
26
    private $brands = [
27
        'American Express' => '/^3[47]\d{13}$/',
28
        'Diners Club' => '/^3(?:0[0-5]|[68]\d)\d{11}$/',
29
        'Discover' => '/^6(?:011|5\d{2})\d{12}$/',
30
        'JCB' => '/^(?:2131|1800|35\d{3})\d{11}$/',
31
        'MasterCard' => '/^5[1-5]\d{14}$/',
32
        'Visa' => '/^4\d{12}(?:\d{3})?$/',
33
    ];
34
35
    /**
36
     * @param string $brand Optional credit card brand.
37
     */
38 3
    public function __construct($brand = null)
39
    {
40 3
        if (null !== $brand && !isset($this->brands[$brand])) {
41 1
            $brands = implode(', ', array_keys($this->brands));
42 1
            $message = sprintf('"%s" is not a valid credit card brand (Available: %s).', $brand, $brands);
43 1
            throw new ComponentException($message);
44
        }
45
46 2
        $this->brand = $brand;
47 2
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 26
    public function validate($input)
53
    {
54 26
        $input = preg_replace('([^0-9])', '', $input);
55
56 26
        if (empty($input)) {
57 3
            return false;
58
        }
59
60 23
        if (!$this->verifyMod10($input)) {
61 3
            return false;
62
        }
63
64 20
        return $this->verifyBrand($input);
65
    }
66
67
    /**
68
     * Returns whether the input matches the Luhn algorithm or not.
69
     *
70
     * @param string $input
71
     *
72
     * @return bool
73
     */
74 23
    private function verifyMod10($input)
75
    {
76 23
        $sum = 0;
77 23
        $input = strrev($input);
78 23
        for ($i = 0; $i < strlen($input); ++$i) {
79 23
            $current = substr($input, $i, 1);
80 23
            if ($i % 2 == 1) {
81 22
                $current *= 2;
82 22
                if ($current > 9) {
83 19
                    $firstDigit = $current % 10;
84 19
                    $secondDigit = ($current - $firstDigit) / 10;
85 19
                    $current = $firstDigit + $secondDigit;
86 19
                }
87 22
            }
88 23
            $sum += $current;
89 23
        }
90
91 23
        return $sum % 10 == 0;
92
    }
93
94
    /**
95
     * Returns whether the input matches the defined credit card brand or not.
96
     *
97
     * @param string $input
98
     *
99
     * @return bool
100
     */
101 20
    private function verifyBrand($input)
102
    {
103 20
        if (null === $this->brand) {
104 7
            return true;
105
        }
106
107 13
        $pattern = $this->brands[$this->brand];
108
109 13
        return preg_match($pattern, $input) > 0;
110
    }
111
}
112