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
05:03
created

CreditCard::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 3
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
            throw new ComponentException(sprintf('"%s" is not a valid credit card brand', $brand));
42
        }
43
44 2
        $this->brand = $brand;
45 2
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 26
    public function validate($input)
51
    {
52 26
        $input = preg_replace('([^0-9])', '', $input);
53
54 26
        if (empty($input)) {
55 3
            return false;
56
        }
57
58 23
        if (!$this->verifyMod10($input)) {
59 3
            return false;
60
        }
61
62 20
        return $this->verifyBrand($input);
63
    }
64
65
    /**
66
     * Returns whether the input matches the Luhn algorithm or not.
67
     *
68
     * @param string $input
69
     *
70
     * @return bool
71
     */
72 23
    private function verifyMod10($input)
73
    {
74 23
        $sum = 0;
75 23
        $input = strrev($input);
76 23
        for ($i = 0; $i < strlen($input); ++$i) {
77 23
            $current = substr($input, $i, 1);
78 23
            if ($i % 2 == 1) {
79 22
                $current *= 2;
80 22
                if ($current > 9) {
81 19
                    $firstDigit = $current % 10;
82 19
                    $secondDigit = ($current - $firstDigit) / 10;
83 19
                    $current = $firstDigit + $secondDigit;
84 19
                }
85 22
            }
86 23
            $sum += $current;
87 23
        }
88
89 23
        return $sum % 10 == 0;
90
    }
91
92
    /**
93
     * Returns whether the input matches the defined credit card brand or not.
94
     *
95
     * @param string $input
96
     *
97
     * @return bool
98
     */
99 20
    private function verifyBrand($input)
100
    {
101 20
        if (null === $this->brand) {
102 7
            return true;
103
        }
104
105 13
        $pattern = $this->brands[$this->brand];
106
107 13
        return preg_match($pattern, $input) > 0;
108
    }
109
}
110