Completed
Push — master ( c9b7ea...c1fd74 )
by Mihail
02:38
created

Constructor::setDefaultProperties()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 12
nc 6
nop 3
1
<?php
2
3
namespace Ffcms\Core\Helper\HTML\Form;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Exception\SyntaxException;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Obj;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class Constructor. Form field construction manager.
14
 * @package Ffcms\Core\Helper\HTML\Form
15
 */
16
class Constructor
17
{
18
    const TYPE_TEXT = 'text';
19
    const TYPE_PASSWORD = 'password';
20
    const TYPE_EMAIL = 'email';
21
    const TYPE_CHECKBOX = 'checkbox';
22
    const TYPE_SELECT = 'select';
23
    const TYPE_MULTISELECT = 'multiselect';
24
    const TYPE_TEXTAREA = 'textarea';
25
    const TYPE_MULTI_CHECKBOXES = 'checkboxes';
26
    const TYPE_CAPTCHA = 'captcha';
27
    const TYPE_FILE = 'file';
28
    const TYPE_HIDDEN = 'hidden';
29
    const TYPE_DIV_FAKE = 'div';
30
    const TYPE_RADIO = 'radio';
31
    
32
    /** @var \Ffcms\Core\Arch\Model $model */
33
    private $model;
34
    private $formName;
35
    private $type;
36
    
37
    /**
38
     * Initialize Constructor. Pass model and type inside of current field inside.
39
     * @param \Ffcms\Core\Arch\Model $model
40
     * @param string|null $formName
41
     * @param string $type
42
     */
43
    public function __construct(Model $model, ?string $formName = null, ?string $type = 'text')
44
    {
45
        $this->model = $model;
46
        $this->formName = $formName;
47
        $this->type = $type;
48
    }
49
50
    /**
51
     * @param string|array $name
52
     * @param string|array|null $value
53
     * @param array|null $properties
54
     * @return null|string
55
     */
56
    public function makeTag($name, $value = null, ?array $properties = null): ?string
57
    {
58
        // check if properties is passed well
59
        if ($properties !== null && !Any::isArray($properties))
60
            return null;
61
        
62
        // add properties to autovalidation by js (properties passed by ref)
63
        $this->addValidationProperties($name, $properties);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...dValidationProperties() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
        // set default form data as properties: name="", value="", id="" based tag info
65
        $this->setDefaultProperties($name, $value, $properties);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...:setDefaultProperties() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...:setDefaultProperties() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
        
67
        // initialize build model depend of current type
68
        switch ($this->type) {
69
            case static::TYPE_TEXT: // for <input type="text">
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
70
                $builder = new TextField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...extField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...extField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
                break;
72
            case static::TYPE_CHECKBOX:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73
                $builder = new CheckboxField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
                break;
75
            case static::TYPE_PASSWORD:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
76
                $builder = new PasswordField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...ordField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ordField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
77
                break;
78
            case static::TYPE_EMAIL:
79
                $builder = new EmailField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...ailField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ailField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
                break;
81
            case static::TYPE_SELECT:
82
                $builder = new SelectField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
83
                break;
84
            case static::TYPE_TEXTAREA:
85
                $builder = new TextareaField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...reaField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...reaField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...reaField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
                break;
87
            case static::TYPE_MULTI_CHECKBOXES:
88
                $builder = new MultiCheckboxField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
89
                break;
90
            case static::TYPE_CAPTCHA:
91
                $builder = new CaptchaField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...chaField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...chaField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
                break;
93
            case static::TYPE_FILE:
94
                $builder = new FileField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...ileField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ileField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95
                break;
96
            case static::TYPE_HIDDEN:
97
                $builder = new HiddenField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...denField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...denField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...denField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
98
                break;
99
            case static::TYPE_DIV_FAKE:
100
                $builder = new DivFakeField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...akeField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...akeField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...akeField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
101
                break;
102
            case static::TYPE_MULTISELECT:
103
                $builder = new MultiSelectField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
                break;
105
            case static::TYPE_RADIO:
106
                $builder = new RadioField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $properties defined by parameter $properties on line 56 can also be of type null; however, Ffcms\Core\Helper\HTML\F...dioField::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $name defined by parameter $name on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...dioField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $value defined by parameter $value on line 56 can also be of type array; however, Ffcms\Core\Helper\HTML\F...dioField::__construct() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
107
                break;
108
            default:
109
                if (App::$Debug)
110
                    App::$Debug->addMessage('Field has unknown type: ' . App::$Security->strip_tags($name));
111
        }
112
113
        return $builder->make();
0 ignored issues
show
Bug introduced by
The variable $builder does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
114
    }
115
    
116
    /**
117
     * Set validator options to current properties
118
     * @param string $name
119
     * @param array|null $properties
120
     * @return void
121
     */
122
    private function addValidationProperties(string $name, ?array &$properties = null): void
123
    {
124
        // jquery validation quick-build some rules
125
        $rules = $this->model->getValidationRule($name);
126
        if (count($rules) > 0) {
127
            foreach ($rules as $type => $value) {
128
                switch ($type) {
129
                    case 'required':
130
                        $properties['required'] = null;
131
                        break;
132
                    case 'length_min':
133
                        $properties['minlength'] = $value;
134
                        break;
135
                    case 'length_max':
136
                        $properties['maxlength'] = $value;
137
                        break;
138
                }
139
            }
140
        }
141
    }
142
    
143
    /**
144
     * Prepare field global properties - name, id and value
145
     * @param string $name
146
     * @param string|null $value
147
     * @param array|null $properties
148
     */
149
    private function setDefaultProperties(string $name, $value = null, array &$properties = null): void
150
    {
151
        // standard property data definition
152
        $properties['name'] = $properties['id'] = $this->formName; // form global name
153
        if ($value !== null && !Any::isEmpty($value))
154
            $properties['value'] = $value;
155
        
156
        // sounds like a array-path based obj name
157
        if (Str::contains('.', $name)) {
158
            $splitedName = explode('.', $name);
159
            foreach ($splitedName as $nameKey) {
160
                $properties['name'] .= '[' . $nameKey . ']';
161
                $properties['id'] .= '-' . $nameKey;
162
            }
163
        } else { // standard property definition - add field name
164
            $properties['name'] .= '[' . $name . ']';
165
            $properties['id'] .= '-' . $name;
166
        }
167
    }
168
}