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 (#118)
by Carlos
02:47
created

CreditCard::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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 byrokrat\checkdigit\Luhn;
12
use Particle\Validator\Rule;
13
14
/**
15
 * This rule is for validating if a value is a valid credit card number.
16
 *
17
 * @package Particle\Validator\Rule
18
 */
19
class CreditCard extends Rule
20
{
21
    /**
22
     * A constant that will be used when the value is not a valid credit card number.
23
     */
24
    const INVALID_FORMAT = 'CreditCard::INVALID_FORMAT';
25
    const INVALID_CHECKSUM = 'CreditCard::INVALID_CHECKSUM';
26
27
    /**
28
     * The message templates which can be returned by this validator.
29
     *
30
     * @var array
31
     */
32
    protected $messageTemplates = [
33
        self::INVALID_FORMAT => '{{ name }} must have a valid credit card number format',
34
        self::INVALID_CHECKSUM => '{{ name }} must be a valid credit card number',
35
    ];
36
37
    /**
38
     * Validates if the value is a valid credit card number.
39
     *
40
     * @param mixed $value
41
     * @return bool
42
     */
43 15
    public function validate($value)
44
    {
45 15
        if (!$this->validateFormat($value)) {
46 3
            return $this->error(self::INVALID_FORMAT);
47 12
        } elseif (!$this->validateChecksum($value)) {
48 4
            return $this->error(self::INVALID_CHECKSUM);
49
        }
50
51 8
        return true;
52
    }
53
54
    /**
55
     * @param $value
56
     *
57
     * @return bool
58
     */
59 12
    private function validateChecksum($value)
60
    {
61 12
        $luhn = new Luhn();
62
63 12
        return $luhn->isValid($value);
64
    }
65
66
    /**
67
     * @param $value
68
     *
69
     * @return bool
70
     */
71 15
    private function validateFormat($value)
72
    {
73 15
        if (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/', $value) === 1) {
74 3
            return true; // Visa
75 12
        } elseif (preg_match('/^5[1-5][0-9]{14}$/', $value) == 1) {
76 3
            return true; // Mastercard
77 9
        } elseif (preg_match('/^3[47][0-9]{13}$/', $value) == 1) {
78 3
            return true; // American Express
79 6
        } elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', $value) == 1) {
80
            return true; // Diners Club
81 6
        } elseif (preg_match('/^6(?:011|5[0-9]{2})[0-9]{12}$/', $value) == 1) {
82 3
            return true; // Discover
83 3
        } elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/', $value) == 1) {
84
            return true; // JCB
85
        }
86
87 3
        return false;
88
    }
89
}
90