Helper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 52.5%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 80
ccs 21
cts 40
cp 0.525
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B validateCif() 0 35 6
A convertToLowercase() 0 12 3
A camelCase() 0 9 1
1
<?php
2
3
namespace ByTIC\MFinante;
4
5
/**
6
 * Class Helper
7
 * @package ByTIC\MFinante
8
 */
9
class Helper
10
{
11
    /**
12
     * @param $cif
13
     * @return bool
14
     */
15 2
    public static function validateCif($cif)
16
    {
17 2
        if (!is_numeric($cif)) {
18
            return false;
19
        }
20 2
        if (strlen($cif) > 10) {
21
            return false;
22
        }
23
24 2
        $controlDigit = substr($cif, -1);
25 2
        $cif = substr($cif, 0, -1);
26 2
        while (strlen($cif) != 9) {
27 2
            $cif = '0' . $cif;
28
        }
29
30 2
        $sum = $cif[0] * 7
31 2
            + $cif[1] * 5
32 2
            + $cif[2] * 3
33 2
            + $cif[3] * 2
34 2
            + $cif[4] * 1
35 2
            + $cif[5] * 7
36 2
            + $cif[6] * 5
37 2
            + $cif[7] * 3
38 2
            + $cif[8] * 2;
39
40 2
        $sum = $sum * 10;
41 2
        $rest = fmod($sum, 11);
42 2
        if ($rest == 10) {
43
            $rest = 0;
44
        }
45
46 2
        if ($rest == $controlDigit) {
47 2
            return true;
48
        } else {
49
            return false;
50
        }
51
    }
52
53
    /**
54
     * Convert a string to camelCase. Strings already in camelCase will not be harmed.
55
     *
56
     * @param  string $str The input string
57
     * @return string camelCased output string
58
     */
59
    public static function camelCase($str)
60
    {
61
        $str = self::convertToLowercase($str);
62
        return preg_replace_callback(
63
            '/_([a-z])/',
64
            function ($match) {
65
                return strtoupper($match[1]);
66
            },
67
            $str
68
        );
69
    }
70
71
    /**
72
     * Convert strings with underscores to be all lowercase before camelCase is preformed.
73
     *
74
     * @param  string $str The input string
75
     * @return string The output string
76
     */
77
    protected static function convertToLowercase($str)
78
    {
79
        $explodedStr = explode('_', $str);
80
81
        if (count($explodedStr) > 1) {
82
            foreach ($explodedStr as $value) {
83
                $lowerCasedStr[] = strtolower($value);
84
            }
85
            $str = implode('_', $lowerCasedStr);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lowerCasedStr seems to be defined by a foreach iteration on line 82. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
86
        }
87
88
        return $str;
89
    }
90
}
91