Datas::checkType()   B
last analyzed

Complexity

Conditions 10
Paths 9

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 9
nop 1
dl 0
loc 33
rs 7.6666
c 0
b 0
f 0

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 BFW\Helpers;
4
5
use \Exception;
6
7
/**
8
 * Helpers to manage datas
9
 */
10
class Datas
11
{
12
    /**
13
     * @const ERR_CHECKTYPE_INFOS_FORMAT Exception code if the format of the
14
     * infos passed to checkType method is not correct.
15
     */
16
    const ERR_CHECKTYPE_INFOS_FORMAT = 1604001;
17
    
18
    /**
19
     * @const ERR_CHECKTYPE_DATA_OR_TYPE_VALUE_FORMAT Exception code if data or
20
     * type used to check the variable has not a correct value.
21
     */
22
    const ERR_CHECKTYPE_DATA_OR_TYPE_VALUE_FORMAT = 1604002;
23
    
24
    /**
25
     * Check types of variables
26
     * 
27
     * @param array $vars : Variables to check
28
     *  array(array('type' => 'myType', 'data' => 'myData), array(...)...)
29
     * 
30
     * @return boolean
31
     */
32
    public static function checkType(array $vars): bool
33
    {
34
        foreach ($vars as $var) {
35
            if (!is_array($var)) {
36
                throw new Exception(
37
                    'The informations need for the check is not in a correct format.',
38
                    self::ERR_CHECKTYPE_INFOS_FORMAT
39
                );
40
            }
41
42
            if (
43
                !isset($var['data'])
44
                || empty($var['type'])
45
                || (isset($var['type']) && !is_string($var['type']))
46
            ) {
47
                throw new Exception(
48
                    'Items data or type is empty or in an bad format.',
49
                    self::ERR_CHECKTYPE_DATA_OR_TYPE_VALUE_FORMAT
50
                );
51
            }
52
53
            if ($var['type'] === 'int') {
54
                $var['type'] = 'integer';
55
            } elseif ($var['type'] === 'float') {
56
                $var['type'] = 'double';
57
            }
58
59
            if (gettype($var['data']) !== $var['type']) {
60
                return false;
61
            }
62
        }
63
64
        return true;
65
    }
66
67
    /**
68
     * Check if an email address is valid
69
     * 
70
     * @param string $mail The email address to check
71
     * 
72
     * @return boolean
73
     */
74
    public static function checkMail(string $mail): bool
75
    {
76
        $securisedMail = Secure::secureData($mail, 'email', false);
77
        
78
        if ($securisedMail === false) {
79
            return false;
80
        }
81
        
82
        return true;
83
    }
84
}
85