Form   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 4

Importance

Changes 0
Metric Value
wmc 19
lcom 4
cbo 4
dl 0
loc 163
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 3
A addLogicalChild() 0 4 1
A setStoredValues() 0 4 1
A getSingleControlTemplate() 0 4 1
A dataValid() 0 11 4
A getHtmlName() 0 4 1
A getName() 0 4 1
A getPost() 0 8 2
A getGet() 0 8 2
A getValueStored() 0 8 2
getValueUser() 0 1 ?
A __toString() 0 5 1
1
<?php
2
3
namespace hemio\form\Abstract_;
4
5
use hemio\form\template;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hemio\form\Abstract_\template.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
abstract class Form extends \hemio\html\Form
8
{
9
    /**
10
     *
11
     * @var string
12
     */
13
    protected $name;
14
15
    /**
16
     *
17
     * @var array
18
     */
19
    private $get = [];
20
21
    /**
22
     *
23
     * @var array
24
     */
25
    private $post = [];
26
27
    /**
28
     *
29
     * @var array
30
     */
31
    protected $storedValues = [];
32
33
    public function __construct($name, array $post = null, array $get = null,
34
                                array $stored = [])
35
    {
36
        if ($post === null)
37
            $post = $_POST;
38
39
        if ($get === null)
40
            $get = $_GET;
41
42
        $this->post         = $post;
43
        $this->get          = $get;
44
        $this->name         = $name;
45
        $this->storedValues = $stored;
46
47
        $this->setAttribute('name', $this->getHtmlName());
48
        $this->setId($this->getHtmlName());
49
        $this->addInheritableAppendage(self::FORM_FIELD_TEMPLATE,
50
                                       new template\FormLineP);
51
        $this->addInheritableAppendage('_FORM', $this);
52
53
        $this['_LOGICAL_CHILDS'] = new \hemio\form\ContainerHiding();
54
    }
55
56
    public function addLogicalChild(FormElement $element)
57
    {
58
        $this['_LOGICAL_CHILDS']->addChild($element);
59
    }
60
61
    /**
62
     *
63
     * @param array $storedValues
64
     */
65
    public function setStoredValues(array $storedValues)
66
    {
67
        $this->storedValues = $storedValues;
68
    }
69
70
    /**
71
     * @return TemplateFormField
72
     */
73
    public function getSingleControlTemplate()
74
    {
75
        return $this->getInheritableAppendage(self::FORM_FIELD_TEMPLATE);
76
    }
77
    const FORM_FIELD_TEMPLATE = '_INPUT_SINGLE_TEMPLATE';
78
79
    /**
80
     * Check for occured errors
81
     *
82
     * @return boolean
83
     */
84
    public function dataValid()
85
    {
86
        foreach (new \RecursiveIteratorIterator($this,
87
                                                \RecursiveIteratorIterator::SELF_FIRST) as $child) {
88
            if ($child instanceof FormElement && !$child->dataValid()) {
89
                return false;
90
            }
91
        }
92
93
        return true;
94
    }
95
96
    /**
97
     *
98
     * @return string
99
     */
100
    public function getHtmlName()
101
    {
102
        return 'form_'.$this->name;
103
    }
104
105
    /**
106
     *
107
     * @return string
108
     */
109
    public function getName()
110
    {
111
        return $this->name;
112
    }
113
114
    /**
115
     *
116
     * @param string $key
117
     * @return null|string
118
     */
119
    public function getPost($key)
120
    {
121
        if (isset($this->post[$key])) {
122
            return $this->post[$key];
123
        } else {
124
            return null;
125
        }
126
    }
127
128
    /**
129
     *
130
     * @param string $key
131
     * @return null|string
132
     */
133
    public function getGet($key)
134
    {
135
        if (isset($this->get[$key])) {
136
            return $this->get[$key];
137
        } else {
138
            return null;
139
        }
140
    }
141
142
    /**
143
     * Stored values like from database
144
     *
145
     * @param string $key
146
     * @return mixed
147
     */
148
    public function getValueStored($key)
149
    {
150
        if (isset($this->storedValues[$key])) {
151
            return $this->storedValues[$key];
152
        } else {
153
            return null;
154
        }
155
    }
156
157
    /**
158
     * Get value from GET or POST
159
     *
160
     * @since 1.0
161
     */
162
    abstract public function getValueUser($key);
163
164
    public function __toString()
165
    {
166
167
        return parent::__toString();
168
    }
169
}
170