Input::render()   F
last analyzed

Complexity

Conditions 16
Paths 12288

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 21
c 0
b 0
f 0
nc 12288
nop 0
dl 0
loc 26
rs 2.7326

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
/**
4
 * Koch Framework
5
 * Jens-André Koch © 2005 - onwards.
6
 *
7
 * This file is part of "Koch Framework".
8
 *
9
 * License: GNU/GPL v2 or any later version, see LICENSE file.
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace Koch\Form\Elements;
26
27
use Koch\Form\FormElement;
28
use Koch\Form\FormElementInterface;
29
30
/**
31
 * Formelement_Input.
32
 *
33
 * @link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html
34
 */
35
class Input extends FormElement implements FormElementInterface
36
{
37
    /**
38
     * The formelement input type, e.g.
39
     * text, password, checkbox, radio, submit, reset, file, hidden, image, button.
40
     *
41
     * @var string
42
     */
43
    public $type;
44
45
    /**
46
     * custom css class.
47
     *
48
     * @var string
49
     */
50
    public $class;
51
52
    /**
53
     * indicates whether checkbox is checked.
54
     *
55
     * @var int
56
     */
57
    public $checked;
58
59
    /**
60
     * indicates whether radio button is selected.
61
     *
62
     * @var int
63
     */
64
    public $selected;
65
66
    /**
67
     * length of field in letters.
68
     *
69
     * @var int
70
     */
71
    public $size;
72
73
    /**
74
     * allowed length of input in letters.
75
     *
76
     * @var int
77
     */
78
    public $maxlength;
79
80
    /**
81
     * disabled.
82
     *
83
     * @var bool
84
     */
85
    public $disabled;
86
87
    /**
88
     * additional string to attach to the opening form tag
89
     * for instance 'onSubmit="xy"'.
90
     *
91
     * @var;
92
     */
93
    public $additional_attr_text;
0 ignored issues
show
Coding Style introduced by
$additional_attr_text does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
94
95
    /**
96
     * description.
97
     *
98
     * @var int
99
     */
100
    public $description;
101
102
    /**
103
     * A regular expression pattern, e.g. [A-Za-z]+\d+.
104
     *
105
     * @var string
106
     */
107
    public $pattern;
108
109
    /**
110
     * String value for the placeholder attribute
111
     * <input placeholder="some placeholder">.
112
     *
113
     * @var string
114
     */
115
    public $placeholder;
116
117
    /**
118
     * The readonly attribute specifies that an input field should be read-only.
119
     *
120
     * @var string
121
     */
122
    public $readonly;
123
124
    /**
125
     * Sets the state of the input field to read-only.
126
     */
127
    public function setReadonly($readonly)
128
    {
129
        $this->readonly = (bool) $readonly;
0 ignored issues
show
Documentation Bug introduced by
The property $readonly was declared of type string, but (bool) $readonly is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
130
    }
131
132
    /**
133
     * Set placeholder attribute value.
134
     *
135
     * @link http://dev.w3.org/html5/spec/Overview.html#the-placeholder-attribute
136
     */
137
    public function setPlaceholder($placeholder)
138
    {
139
        $this->placeholder = $placeholder;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Get Placeholder <input placeholder="some placeholder">.
146
     *
147
     * @return string
148
     */
149
    public function getPlaceholder()
150
    {
151
        return $this->placeholder;
152
    }
153
154
    /**
155
     * Set the regular expression pattern for client-side validation
156
     * e.g. [A-Za-z]+\d+.
157
     *
158
     * @var string
159
     */
160
    public function setPattern($pattern)
161
    {
162
        $this->pattern = $pattern;
163
    }
164
165
    /**
166
     * defines length of field in letters.
167
     *
168
     * @param int $size
169
     */
170
    public function setSize($size)
171
    {
172
        $this->size = (int) $size;
173
    }
174
175
    /**
176
     * defines allowed length of input in letters.
177
     *
178
     * @param int $length
179
     */
180
    public function setMaxLength($length)
181
    {
182
        $this->maxlength = (int) $length;
183
    }
184
185
    /**
186
     * defines allowed length of input in letters.
187
     *
188
     * @param bool $disabled True or False.
189
     */
190
    public function setDisabled($disabled)
191
    {
192
        $this->disabled = (bool) $disabled;
193
    }
194
195
    /**
196
     * Set Additional Attributes as Text to formelement.
197
     *
198
     * @example
199
     * Setting the onclick attribute.
200
     * $this->setAdditionalAttributeText(' onclick="window.location.href=\''.$this->cancelURL.'\'"');
201
     *
202
     * @param string $additional_attr_text of this formelement.
203
     */
204
    public function setAdditionalAttributeAsText($additional_attr_text)
0 ignored issues
show
Coding Style introduced by
$additional_attr_text does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
205
    {
206
        $this->additional_attr_text = $additional_attr_text;
0 ignored issues
show
Coding Style introduced by
$additional_attr_text does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
207
208
        return $this;
209
    }
210
211
    /**
212
     * Renders the html code of the input element.
213
     *
214
     * @return string
215
     */
216
    public function render()
217
    {
218
        $html = null;
219
        $html .= '<input type="' . $this->type . '" name="' . $this->name . '"';
220
        $html .= (bool) $this->id ? ' id="' . $this->id . '"' : null;
221
        $html .= (bool) $this->value ? ' value="' . $this->value . '"' : null;
222
        $html .= (bool) $this->placeholder ? ' placeholder="' . $this->placeholder . '"' : null;
223
        $html .= (bool) $this->size ? ' size="' . $this->size . '"' : null;
224
        $html .= (bool) $this->readonly ? ' readonly="readonly"' : null;
225
        $html .= (bool) $this->disabled ? ' disabled="disabled"' : null;
226
        $html .= (bool) $this->maxlength ? ' maxlength="' . $this->maxlength . '"' : null;
227
        $html .= (bool) $this->pattern ? ' pattern="' . $this->pattern . '"' : null;
228
        $html .= (bool) $this->class ? ' class="' . $this->class . '"' : null;
229
        if ($this->type === 'image') {
230
            $html .= ' source="' . $this->source . '"';
0 ignored issues
show
Documentation introduced by
The property source does not exist on object<Koch\Form\Elements\Input>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
231
            if ((bool) $this->width && (bool) $this->height) {
0 ignored issues
show
Documentation introduced by
The property width does not exist on object<Koch\Form\Elements\Input>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property height does not exist on object<Koch\Form\Elements\Input>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
232
                $html .= ' style="width:' . $this->width . 'px; height:' . $this->height . 'px;"';
0 ignored issues
show
Documentation introduced by
The property width does not exist on object<Koch\Form\Elements\Input>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property height does not exist on object<Koch\Form\Elements\Input>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
233
            }
234
        }
235
        $html .= (bool) $this->checked ? ' checked="checked"' : null;
236
        $html .= (bool) $this->additional_attr_text ? $this->additional_attr_text : null;
237
        $html .= (bool) $this->additional_attributes ? $this->renderAttributes($this->additional_attributes) : null;
238
        $html .= ' />' . CR;
239
240
        return $html;
241
    }
242
}
243