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 (#735)
by Julián
04:17
created

Nif   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 46.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 58
ccs 14
cts 30
cp 0.4667
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 43 7
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
class Nif extends AbstractRule
22
{
23
    protected static $keyValues = [
24
        'K' => '',
25
        'L' => '',
26
        'M' => '',
27
        'X' => '0',
28
        'Y' => '1',
29
        'Z' => '2'
30
    ];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 30
    public function validate($input)
36
    {
37 30
        if (preg_match('/^(\d{8})([A-Z])$/', $input, $matches)) {
38
            // DNI
39 10
            $code = (int) $matches[1];
40 10
            $control = $matches[2];
41
42 10
            return substr('TRWAGMYFPDXBNJZSQVHLCKE', $code % 23, 1) === $control;
43 20
        } elseif (preg_match('/^([KLMXYZ])(\d{7})([A-Z])$/', $input, $matches)) {
44
            // NIE
45 10
            $code = (int) (static::$keyValues[$matches[1]] . $matches[2]);
46 10
            $control = $matches[3];
47
48 10
            return substr('TRWAGMYFPDXBNJZSQVHLCKE', $code % 23, 1) === $control;
49 10
        } elseif (preg_match('/^([ABCDEFGHJNPQRSUVW])(\d{7})([0-9A-Z])$/', $input, $matches)) {
50
            // CIF
51 10
            $control = $matches[3];
52
53 10
            $code = 0;
54 10
            array_walk(
55 10
                str_split($matches[2]),
0 ignored issues
show
Bug introduced by
str_split($matches[2]) cannot be passed to array_walk() as the parameter $array expects a reference.
Loading history...
56
                function ($value, $key) use (&$code) {
57
                    if (($key + 1) % 2 === 0) {
58
                        $code += $value;
59
                    } else {
60
                        $code += array_sum(str_split($value * 2));
61
                    }
62
                }
63
            );
64
            $controlKey = end(str_split($code));
0 ignored issues
show
Bug introduced by
str_split($code) cannot be passed to end() as the parameter $array expects a reference.
Loading history...
65
            if ($controlKey !== 0) {
66
                $controlKey = 10 - $controlKey;
67
            }
68
69
            if (is_numeric($control)) {
70
                $valid = $controlKey === (int) $control;
0 ignored issues
show
Unused Code introduced by
$valid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
            }
72
73
            return substr('JABCDEFGHI', $controlKey % 10, 1) === $control;
74
        }
75
76
        return false;
77
    }
78
}
79