Completed
Push — master ( 7b0f49...e26a65 )
by James Ekow Abaka
08:21
created

FormHelper   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 130
Duplicated Lines 10.77 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 14
loc 130
ccs 73
cts 73
cp 1
rs 10
c 0
b 0
f 0
wmc 23
lcom 0
cbo 5

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __toString() 0 8 1
A create() 0 7 2
A setId() 0 4 1
A add() 0 10 2
A setErrors() 0 4 1
A setData() 0 4 1
A open() 0 9 2
A close() 0 11 3
C __call() 14 37 7
A getContainer() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * Forms helper
5
 * 
6
 * Ntentan Framework
7
 * Copyright (c) 2008-2012 James Ekow Abaka Ainooson
8
 * 
9
 * Permission is hereby granted, free of charge, to any person obtaining
10
 * a copy of this software and associated documentation files (the
11
 * "Software"), to deal in the Software without restriction, including
12
 * without limitation the rights to use, copy, modify, merge, publish,
13
 * distribute, sublicense, and/or sell copies of the Software, and to
14
 * permit persons to whom the Software is furnished to do so, subject to
15
 * the following conditions:
16
 * 
17
 * The above copyright notice and this permission notice shall be
18
 * included in all copies or substantial portions of the Software.
19
 * 
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
27
 * 
28
 * @author James Ainooson <[email protected]>
29
 * @copyright Copyright 2010 James Ekow Abaka Ainooson
30
 * @license MIT
31
 */
32
33
namespace ntentan\honam\helpers;
34
35
use ntentan\honam\Helper;
36
use ntentan\utils\Text;
37
use \ReflectionMethod;
38
use \ReflectionClass;
39
40
/**
41
 * Forms helper for rendering forms.
42
 */
43
class FormHelper extends Helper
44
{
45
46
    private $container;
47
    private $data = array();
48
    private $errors = array();
49 14
50
    public function __construct()
51 14
    {
52 14
        \ntentan\honam\TemplateEngine::appendPath(
53
            __DIR__ . "/../../templates/forms"
54 14
        );
55
    }
56
57
    /**
58
     * Renders the form when the value is used as a string
59
     * @return string
60 3
     */
61
    public function __toString()
62 3
    {
63 3
        $this->container->setData($this->data);
64 3
        $this->container->setErrors($this->errors);
65 3
        $return = (string) $this->container;
66 3
        $this->container = null;
67
        return $return;
68
    }
69 1
70
    public static function create()
71 1
    {
72
        $args = func_get_args();
73
        $element = __NAMESPACE__ . "\\form\\" . array_shift($args);
74 3
        $element = new ReflectionClass($element);
75
        return $element->newInstanceArgs($args == null ? array() : $args);
76 3
    }
77 3
78 3
    public function setId($id)
79 3
    {
80
        $this->getContainer()->setId($id);
81
    }
82 3
83
    public function add()
84 3
    {
85 3
        $args = func_get_args();
86
        if (is_string($args[0])) {
87 2
            $elementClass = new ReflectionMethod(__NAMESPACE__ . "\\FormHelper", 'create');
88
            $element = $elementClass->invokeArgs(null, $args);
89 2
            $this->getContainer()->add($element);
90 2
        }
91
        return $this;
92 2
    }
93 2
94 2
    public function setErrors($errors)
95
    {
96 2
        $this->errors = $errors;
97
    }
98
99 1
    public function setData($data)
100
    {
101 1
        $this->data = $data;
102 1
    }
103
104 2
    public function open($formId = '')
105
    {
106 2
        $this->container = new form\Form();
107 2
        if ($formId != '') {
108
            $this->container->setId($formId);
109 8
        }
110
        $this->container->setRenderMode(form\Container::RENDER_MODE_HEAD);
111 8
        return $this->container;
112 8
    }
113
114 1
    public function close($submit = 'Submit')
115
    {
116 8
        $arguments = func_get_args();
117 8
        if ($submit === false) {
118
            $this->container->setShowSubmit(false);
119
        } else if (count($arguments) > 0) {
120 8
            $this->container->setSubmitValues($arguments);
121
        }
122 8
        $this->container->setRenderMode(form\Container::RENDER_MODE_FOOT);
123 8
        return $this->container;
124
    }
125 1
126
    public function __call($function, $arguments)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
127 8
    {
128
        if (substr($function, 0, 5) == "open_") {
129 3
            $container = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 5, strlen($function)));
130
            $containerClass = new ReflectionClass($container);
131 8
            $containerObject = $containerClass->newInstanceArgs($arguments);
132 8
            $containerObject->setRenderMode(form\Container::RENDER_MODE_HEAD);
133
            $return = $containerObject;
134 View Code Duplication
        } elseif (substr($function, 0, 6) == "close_") {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135 7
            $container = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 6, strlen($function)));
136
            $containerClass = new ReflectionClass($container);
137 7
            $containerObject = $containerClass->newInstanceArgs($arguments);
138
            $containerObject->setRenderMode(form\Container::RENDER_MODE_FOOT);
139 1
            $return = $containerObject;
140 1
        } elseif (substr($function, 0, 4) == "get_") {
141 1
            $element = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 4, strlen($function)));
142 1
            $elementClass = new ReflectionClass($element);
143
            $elementObject = $elementClass->newInstanceArgs($arguments);
144 7
            $name = $elementObject->getName();
145
            if (isset($this->data[$name])) {
146 1
                $elementObject->setValue($this->data[$name]);
147 1
            }
148 1
            if (isset($this->errors[$name])) {
149 1
                $elementObject->setErrors($this->errors[$name]);
150
            }
151 7
            $return = $elementObject;
152 View Code Duplication
        } elseif (substr($function, 0, 4) == "add_") {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153 5
            $element = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 4, strlen($function)));
154 5
            $elementClass = new ReflectionClass($element);
155 5
            $elementObject = $elementClass->newInstanceArgs($arguments);
156 5
            $return = $this->container->add($elementObject);
157 5
        } else {
158
            throw new \ntentan\honam\exceptions\HelperException("Function *$function* not found in form helper.");
159 1
        }
160
161 5
        return $return;
162
    }
163 1
164
    private function getContainer()
165 5
    {
166
        if ($this->container === null) {
167 2
            $this->container = new form\Form();
168
        }
169 1
        return $this->container;
170 1
    }
171 1
172
}
173