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
Pull Request — master (#738)
by Henrique
18:32 queued 14:31
created

Nif::validateNie()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 3
crap 3
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 30
    public function validate($input)
27
    {
28 30
        if (preg_match('/^(\d{8})([A-Z])$/', $input, $matches)) {
29 10
            return $this->validateDni($matches[1], $matches[2]);
30
        }
31
32 20
        if (preg_match('/^([KLMXYZ])(\d{7})([A-Z])$/', $input, $matches)) {
33 10
            return $this->validateNie($matches[1], $matches[2], $matches[3]);
34
        }
35
36 10
        if (preg_match('/^([A-HJNP-SUVW])(\d{7})([0-9A-Z])$/', $input, $matches)) {
37 10
            return $this->validateCif($matches[2], $matches[3]);
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * @param int $number
45
     * @param int $control
46
     */
47 20
    private function validateDni($number, $control)
48
    {
49 20
        return 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 substr('TRWAGMYFPDXBNJZS...LCKE', $number % 23, 1) (string) and $control (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
50
    }
51
52
    /**
53
     * @param string $prefix
54
     * @param int    $number
55
     * @param int    $control
56
     */
57 10
    private function validateNie($prefix, $number, $control)
58
    {
59 10
        if ($prefix === 'Y') {
60 7
            return $this->validateDni('1'.$number, $control);
61
        }
62
63 3
        if ($prefix === 'Z') {
64 1
            return $this->validateDni('2'.$number, $control);
65
        }
66
67 2
        return $this->validateDni($number, $control);
68
    }
69
70
    /**
71
     * @param int    $number
72
     * @param string $control
73
     */
74 10
    private function validateCif($number, $control)
75
    {
76 10
        $code = 0;
77 10
        $position = 1;
78 10
        foreach (str_split($number) as $digit) {
79 10
            $increaser = $digit;
80 10
            if ($position % 2 !== 0) {
81 10
                $increaser = array_sum(str_split($digit * 2));
82 10
            }
83
84 10
            $code += $increaser;
85 10
            ++$position;
86 10
        }
87
88 10
        $digits = str_split($code);
89 10
        $lastDigit = array_pop($digits);
90 10
        $key = $lastDigit === 0 ? 0 : (10 - $lastDigit);
91
92 10
        if (is_numeric($control)) {
93 2
            return $key === (int) $control;
94
        }
95
96 8
        return substr('JABCDEFGHI', ($key % 10), 1) === $control;
97
    }
98
}
99