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.

CardNumberValidator::getMod()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo\Validator;
13
14
use Sokil\Mongo\Structure;
15
16
/**
17
 * Credit card number validator based on Luhn algorithm
18
 *
19
 * @author Dmytro Sokil <[email protected]>
20
 */
21
class CardNumberValidator extends \Sokil\Mongo\Validator
22
{
23
    private function getMod($cardNumber)
24
    {
25
        $digitList = str_split($cardNumber);
26
        $digitListLength = count($digitList);
27
        
28
        for ($i = 0; $i < $digitListLength; $i = $i + 2) {
29
            $digit = $digitList[$i] * 2;
30
            if ($digit > 9) {
31
                $digit -= 9;
32
            }
33
            $digitList[$i] = $digit;
34
        }
35
        
36
        return array_sum($digitList) % 10;
37
    }
38
    
39
    public function validateField(Structure $document, $fieldName, array $params)
40
    {
41
        if (!$document->get($fieldName)) {
42
            return;
43
        }
44
        
45
        $carsNumber = $document->get($fieldName);
46
        
47
        if (is_numeric($carsNumber) && 0 === $this->getMod($carsNumber)) {
48
            return;
49
        }
50
        
51
        if (!isset($params['message'])) {
52
            $params['message'] = 'Value of field "' . $fieldName . '" is not valid card number at ' . get_called_class();
53
        }
54
        
55
        $document->addError($fieldName, $this->getName(), $params['message']);
56
    }
57
}
58