Form   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 129
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B renderDecorated() 0 60 9
A renderAll() 0 10 3
A makeLabel() 0 8 2
A renderImage() 0 37 4
1
<?php
2
3
/**
4
 * Form
5
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
6
 * @author Aleksandr Torosh <[email protected]>
7
 */
8
9
namespace Application\Form;
10
11
use Phalcon\Forms\Element\Hidden;
12
use Phalcon\Forms\Element\Check;
13
use Phalcon\Forms\Element\File;
14
use Application\Form\Element\Image;
15
16
abstract class Form extends \Phalcon\Forms\Form
17
{
18
19
    protected $helper;
20
21
    public function renderDecorated($name)
22
    {
23
        if (!$this->has($name)) {
24
            return "form element '$name' not found<br />";
25
        }
26
27
        $this->helper = $this->getDI()->get('helper');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
28
29
        $element = $this->get($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $element is correct as $this->get($name) (which targets Phalcon\Forms\Form::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
30
        $messages = $this->getMessagesFor($element->getName());
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $element (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
Are you sure the assignment to $messages is correct as $this->getMessagesFor($element->getName()) (which targets Phalcon\Forms\Form::getMessagesFor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
31
32
        $html = '';
33
        if (count($messages)) {
34
            $html .= '<div class="ui error message">';
35
            $html .= '<div class="header">' . $this->helper->translate('Ошибка валидации формы') . '</div>';
36
            foreach ($messages as $message) {
0 ignored issues
show
Bug introduced by
The expression $messages of type null is not traversable.
Loading history...
37
                $html .= '<p>' . $message . '</p>';
38
            }
39
            $html .= '</div>';
40
        }
41
42
        if ($element instanceof Hidden) {
43
            echo $element;
44
        } else {
45
            switch (true) {
46
                case $element instanceof Check:
47
                    $html .= '<div class="field checkbox">';
48
                    $html .= '<div class="ui toggle checkbox">';
49
                    $html .= $element;
50
                    if ($element->getLabel()) {
51
                        $html .= '<label>';
52
                        $html .= $element->getLabel();
53
                        $html .= '</label>';
54
                    }
55
                    $html .= '</div>';
56
                    $html .= '</div>';
57
                    break;
58
59
                case $element instanceof Image:
60
                    $html = $this->renderImage($element);
61
                    break;
62
63
                case $element instanceof File:
64
                    $html .= '<div class="inline field">';
65
                    $html .= $this->makeLabel($element);
66
                    $html .= $element;
67
                    $html .= '</div>';
68
                    break;
69
70
                default:
71
                    $html .= '<div class="field">';
72
                    $html .= $this->makeLabel($element);
73
                    $html .= $element;
74
                    $html .= '</div>';
75
            }
76
        }
77
78
        return $html;
79
80
    }
81
82
    public function renderAll()
83
    {
84
        $html = '';
85
        if ($this->getElements()) {
86
            foreach ($this->getElements() as $element) {
87
                $html .= $this->renderDecorated($element->getName());
88
            }
89
        }
90
        return $html;
91
    }
92
93
    private function makeLabel($element)
94
    {
95
        if ($element->getLabel()) {
96
            return '<label for="' . $element->getName() . '">' . $this->helper->translate($element->getLabel()) . '</label>';
97
        } else {
98
            return '';
99
        }
100
    }
101
102
    /**
103
     * @param Image $element
104
     * @return string $html
105
     */
106
    private function renderImage($element)
107
    {
108
        $html = '<div class="form-group">';
109
110
        if ($element->getLabel()) {
111
            $html .= '<label>' . $element->getLabel() . '</label>';
112
        }
113
        if ($element->getValue()) {
114
            $html .= '<section onclick="selectText(this);">' . $element->getValue() . '</section>';
115
        } else {
116
            $html .= '<br>';
117
        }
118
119
        $html .= '<div class="fileinput fileinput-new" data-provides="fileinput">
120
                            <div class="fileinput-preview thumbnail" data-trigger="fileinput"
121
                                 style="width: 200px; min-height: 100px">';
122
123
        if ($element->getValue()) {
124
            $url = $this->getDI()->get('url');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
125
            $html .= '<img src="' . $url->path() . $element->getValue() . '" width="200">';
126
        }
127
128
        $html .= '</div>
129
                        <div>
130
                            <span class="btn btn-default btn-file">
131
                                <span class="fileinput-new">Select image</span>
132
                                <span class="fileinput-exists">Change</span>
133
                                <input type="file" name="'.$element->getName() . '">
134
                            </span>
135
                            <a href="#" class="btn btn-default fileinput-exists"
136
                               data-dismiss="fileinput">Remove</a>
137
                        </div>
138
                    </div>
139
                </div>';
140
141
        return $html;
142
    }
143
144
}
145