ShebaNumber::rule()   B
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 11
eloc 19
c 2
b 0
f 1
nc 10
nop 4
dl 0
loc 29
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Iamfarhad\Validation\Rules;
4
5
use Iamfarhad\Validation\Contracts\AbstractValidationRule;
6
7
class ShebaNumber extends AbstractValidationRule
8
{
9
    /**
10
     * @var string
11
     */
12
    public $validationRule = 'sheba_number';
13
14
    /**
15
     * @param $attribute
16
     * @param $value
17
     * @param $parameters
18
     * @param $validator
19
     * @return bool
20
     */
21
    public function rule($attribute, $value, $parameters, $validator): bool
22
    {
23
        $ibanReplaceValues = [];
24
25
        if (empty($value)) {
26
            return false;
27
        }
28
29
        $value = preg_replace('/[\W_]+/', '', strtoupper($value));
30
        if ((4 > strlen($value) || strlen($value) > 34) || (is_numeric($value[0]) || is_numeric($value[1])) || (! is_numeric($value[2]) || ! is_numeric($value[3]))) {
31
            return false;
32
        }
33
        $ibanReplaceChars = range('A', 'Z');
34
        foreach (range(10, 35) as $tempvalue) {
35
            $ibanReplaceValues[] = strval($tempvalue);
36
        }
37
        $tmpIBAN = substr($value, 4).substr($value, 0, 4);
38
        $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN);
39
        $tmpValue = intval(substr($tmpIBAN, 0, 1));
40
        for ($i = 1; $i < strlen($tmpIBAN); $i++) {
41
            $tmpValue *= 10;
42
            $tmpValue += intval(substr($tmpIBAN, $i, 1));
43
            $tmpValue %= 97;
44
        }
45
        if ($tmpValue != 1) {
46
            return false;
47
        }
48
49
        return true;
50
    }
51
}
52