Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — 2.0 ( e6a123...4184ab )
by Henrique
03:41
created

Nif::validateDni()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
/**
15
 * Validates Spain's fiscal identification number (NIF).
16
 *
17
 * @author Julián Gutiérrez <[email protected]>
18
 *
19
 * @see https://es.wikipedia.org/wiki/N%C3%BAmero_de_identificaci%C3%B3n_fiscal
20
 */
21
final class Nif extends AbstractRule
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function validate($input)
27
    {
28
        if (!is_string($input)) {
29
            return false;
30
        }
31
32
        if (preg_match('/^(\d{8})([A-Z])$/', $input, $matches)) {
33
            return $this->validateDni($matches[1], $matches[2]);
34
        }
35
36
        if (preg_match('/^([KLMXYZ])(\d{7})([A-Z])$/', $input, $matches)) {
37
            return $this->validateNie($matches[1], $matches[2], $matches[3]);
38
        }
39
40
        if (preg_match('/^([A-HJNP-SUVW])(\d{7})([0-9A-Z])$/', $input, $matches)) {
41
            return $this->validateCif($matches[2], $matches[3]);
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * @param int $number
49
     * @param int $control
50
     */
51
    private function validateDni($number, $control)
52
    {
53
        return mb_substr('TRWAGMYFPDXBNJZSQVHLCKE', ($number % 23), 1) === $control;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of mb_substr('TRWAGMYFPDXBN...LCKE', $number % 23, 1) (string) and $control (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
54
    }
55
56
    /**
57
     * @param string $prefix
58
     * @param int    $number
59
     * @param int    $control
60
     */
61
    private function validateNie($prefix, $number, $control)
62
    {
63
        if ($prefix === 'Y') {
64
            return $this->validateDni('1'.$number, $control);
65
        }
66
67
        if ($prefix === 'Z') {
68
            return $this->validateDni('2'.$number, $control);
69
        }
70
71
        return $this->validateDni($number, $control);
72
    }
73
74
    /**
75
     * @param int    $number
76
     * @param string $control
77
     */
78
    private function validateCif($number, $control)
79
    {
80
        $code = 0;
81
        $position = 1;
82
        foreach (str_split($number) as $digit) {
83
            $increaser = $digit;
84
            if ($position % 2 !== 0) {
85
                $increaser = array_sum(str_split($digit * 2));
86
            }
87
88
            $code += $increaser;
89
            ++$position;
90
        }
91
92
        $digits = str_split($code);
93
        $lastDigit = array_pop($digits);
94
        $key = $lastDigit === 0 ? 0 : (10 - $lastDigit);
95
96
        if (is_numeric($control)) {
97
            return (int) $key === (int) $control;
98
        }
99
100
        return mb_substr('JABCDEFGHI', ($key % 10), 1) === $control;
101
    }
102
}
103