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

CreditCard   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 69
ccs 20
cts 22
cp 0.9091
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 3
A validateChecksum() 0 4 1
B validateFormat() 0 18 7
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
use PayBreak\Luhn\Luhn;
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
        return Luhn::validateNumber($value);
62
    }
63
64
    /**
65
     * @param $value
66
     *
67
     * @return bool
68
     */
69 15
    private function validateFormat($value)
70
    {
71 15
        if (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/', $value) === 1) {
72 3
            return true; // Visa
73 12
        } elseif (preg_match('/^5[1-5][0-9]{14}$/', $value) == 1) {
74 3
            return true; // Mastercard
75 9
        } elseif (preg_match('/^3[47][0-9]{13}$/', $value) == 1) {
76 3
            return true; // American Express
77 6
        } elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', $value) == 1) {
78
            return true; // Diners Club
79 6
        } elseif (preg_match('/^6(?:011|5[0-9]{2})[0-9]{12}$/', $value) == 1) {
80 3
            return true; // Discover
81 3
        } elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/', $value) == 1) {
82
            return true; // JCB
83
        }
84
85 3
        return false;
86
    }
87
}
88