ValidateTypes::getSQLValueString()   D
last analyzed

Complexity

Conditions 21
Paths 40

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 57
rs 4.1666
cc 21
nc 40
nop 4

How to fix   Long Method    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
4
/**
5
 * KNUT7 K7F (https://marciozebedeu.com/)
6
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @link      https://github.com/knut7/framework/ for the canonical source repository
13
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
14
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
15
 * @author    Marcio Zebedeu - [email protected]
16
 * @version   1.0.2
17
 */
18
19
namespace Ballybran\Helpers\Security;
20
21
22
/**
23
 * Class ValidateTypes
24
 * @package Ballybran\Helpers\Security
25
 */
26
class ValidateTypes
27
{
28
29
    public static function getSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
30
    {
31
32
        $theValue = function_exists("htmlspecialchars") ? htmlspecialchars($theValue) : $theValue;
33
34
        switch ($theType) {
35
            case "string":
36
                if (!is_string($theValue)) {
37
                    return null;
38
                }
39
                return strip_tags("$theValue");
40
            case "email":
41
                if (!is_string($theValue)) {
42
                    return null;
43
                }
44
                return filter_var($theValue, FILTER_VALIDATE_EMAIL);
45
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
            case "long":
47
            case "int":
48
                if (!is_numeric($theValue)) {
49
                    return null;
50
                }
51
                return intval($theValue);
52
                break;
53
            case "double":
54
                if (!is_double($theValue)) {
55
                    return null;
56
                }
57
                return floatval($theValue);
58
                break;
59
            case "date":
60
61
                $theValue = ($theValue != "") ? "" . $theValue . "" : null;
62
                break;
63
            case "url":
64
                if (!is_string($theValue)) {
65
                    return null;
66
                }
67
                return filter_var($theValue, FILTER_VALIDATE_URL);
68
                break;
69
            case "domain":
70
                if (!is_string($theValue)) {
71
                    return null;
72
                }
73
                return filter_var($theValue, FILTER_VALIDATE_DOMAIN);
74
                break;
75
            case "ip":
76
                if (!is_double($theValue)) {
77
                    return null;
78
                }
79
                return filter_var($theValue, FILTER_VALIDATE_IP);
80
                break;
81
            case "defined":
82
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
83
                break;
84
        }
85
        return $theValue;
86
    }
87
88
89
}