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 — master ( 0417fe...f23b3a )
by Henrique
03:49
created

Nip   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 23 4
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use function array_map;
17
use function is_scalar;
18
use function preg_match;
19
use function str_split;
20
21
/**
22
 * Validates whether the input is a Polish VAT identification number (NIP).
23
 *
24
 * @see https://en.wikipedia.org/wiki/VAT_identification_number
25
 *
26
 * @author Henrique Moody <[email protected]>
27
 * @author Tomasz Regdos <[email protected]>
28
 */
29
final class Nip extends AbstractRule
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function validate($input): bool
35
    {
36
        if (!is_scalar($input)) {
37
            return false;
38
        }
39
40
        if (!preg_match('/^\d{10}$/', (string) $input)) {
41
            return false;
42
        }
43
44
        $weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
45
        $digits = array_map('intval', str_split($input));
46
47
        $targetControlNumber = $digits[9];
48
        $calculateControlNumber = 0;
49
50
        for ($i = 0; $i < 9; ++$i) {
51
            $calculateControlNumber += $digits[$i] * $weights[$i];
52
        }
53
54
        $calculateControlNumber = $calculateControlNumber % 11;
55
56
        return $targetControlNumber == $calculateControlNumber;
57
    }
58
}
59