Completed
Pull Request — master (#93)
by Sullivan
01:52
created

CreditCard   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
c 4
b 1
f 0
lcom 0
cbo 0
dl 0
loc 48
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 34 7
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 (!is_string($creditCard) && !empty($creditCard)) {
24
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
25
                'Passing other values than a string on '.__METHOD__.' is deprecated since version 2.2.'
26
                .' It will be considered as invalid on version 3.0.',
27
                E_USER_DEPRECATED
28
            );
29
        }
30
31
        // Add !is_string($creditCard) to the condition on 3.0.
32
        if (trim($creditCard) === '') {
33
            return false;
34
        }
35
36
        //longueur de la chaine $creditCard
37
        $length = strlen($creditCard);
38
39
        //resultat de l'addition de tous les chiffres
40
        $tot = 0;
41
        for ($i = $length - 1; $i >= 0; --$i) {
42
            $digit = substr($creditCard, $i, 1);
43
44
            if ((($length - $i) % 2) == 0) {
45
                $digit = $digit * 2;
46
                if ($digit > 9) {
47
                    $digit = $digit - 9;
48
                }
49
            }
50
            $tot += (int) $digit;
51
        }
52
53
        return ($tot % 10) == 0;
54
    }
55
}
56