Completed
Push — master ( 9de2ae...02afd8 )
by Ronan
06:45
created

CreditCard   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 0
cbo 0
dl 0
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 25 5
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class CreditCard.
7
 */
8
class CreditCard implements IsoCodeInterface
9
{
10
    /**
11
     * Credit Card validator.
12
     *
13
     * @param string $creditCard
14
     *
15
     * @author ronan.guilloux
16
     *
17
     * @link   http://www.prometee-creation.com/tutoriels/fonction-de-luhn-en-php.html
18
     *
19
     * @return bool
20
     */
21
    public static function validate($creditCard)
22
    {
23
        if (trim($creditCard) === '') {
24
            return false;
25
        }
26
27
        //longueur de la chaine $creditCard
28
        $length = strlen($creditCard);
29
30
        //resultat de l'addition de tous les chiffres
31
        $tot = 0;
32
        for ($i = $length - 1; $i >= 0; --$i) {
33
            $digit = substr($creditCard, $i, 1);
34
35
            if ((($length - $i) % 2) == 0) {
36
                $digit = $digit * 2;
37
                if ($digit > 9) {
38
                    $digit = $digit - 9;
39
                }
40
            }
41
            $tot += (int) $digit;
42
        }
43
44
        return ($tot % 10) == 0;
45
    }
46
}
47