Passed
Push — master ( 9cd1f0...eb6ee6 )
by Iman
09:06 queued 05:17
created

FieldDetector::detect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator;
4
5
class FieldDetector
6
{
7
    /**
8
     * @param $fieldName string
9
     * @return bool
10
     */
11
    static function isExceptional($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
    {
13
        return in_array($fieldName, ['id', 'created_at', 'updated_at', 'deleted_at']);
14
    }
15
16
    /**
17
     * @param $fieldName string
18
     * @return bool
19
     */
20
    static function isForeignKey($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
    {
22
        return substr($fieldName, 0, 3) == 'id_' || substr($fieldName, -3) == '_id';
23
    }
24
25
    static function isUploadField($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        return self::isWithin($fieldName, 'UPLOAD_TYPES');
28
    }
29
30
    static function isWithin($fieldName, $configKey)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
    {
32
        return in_array($fieldName, explode(',', cbConfig($configKey)));
33
    }
34
35
    static function detect($colName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        $map = [
38
            'isPassword' => 'password',
39
            'isImage' => 'image',
40
            'isGeographical' => 'geo',
41
            'isPhone' => 'phone',
42
            'isEmail' => 'email',
43
            'isNameField' => 'name',
44
            'isUrlField' => 'url',
45
        ];
46
        foreach ($map as $methodName => $fieldType){
47
            if (self::$methodName($colName)) {
48
                return $fieldType;
49
            }
50
        }
51
    }
52
53
    /**
54
     * @param $fieldName string
55
     * @return bool
56
     */
57
    static function isPassword($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
    {
59
        return self::isWithin($fieldName, 'PASSWORD_FIELDS_CANDIDATE');
60
    }
61
62
    /**
63
     * @param $fieldName string
64
     * @return bool
65
     */
66
    static function isImage($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        return self::isWithin($fieldName, 'IMAGE_FIELDS_CANDIDATE');
69
    }
70
71
    /**
72
     * @param $fieldName string
73
     * @return bool
74
     */
75
    static function isGeographical($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
    {
77
        return in_array($fieldName, ['latitude', 'longitude']);
78
    }
79
80
    /**
81
     * @param $fieldName string
82
     * @return bool
83
     */
84
    static function isPhone($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
    {
86
        return self::isWithin($fieldName, 'PHONE_FIELDS_CANDIDATE');
87
    }
88
89
    /**
90
     * @param $fieldName string
91
     * @return bool
92
     */
93
    static function isEmail($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
94
    {
95
        return self::isWithin($fieldName, 'EMAIL_FIELDS_CANDIDATE');
96
    }
97
98
    /**
99
     * @param $fieldName string
100
     * @return bool
101
     */
102
    static function isNameField($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
    {
104
        return self::isWithin($fieldName, 'NAME_FIELDS_CANDIDATE');
105
    }
106
107
    /**
108
     * @param $fieldName string
109
     * @return bool
110
     */
111
    static function isUrlField($fieldName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
112
    {
113
        return self::isWithin($fieldName, 'URL_FIELDS_CANDIDATE');
114
    }
115
}