Passed
Push — develop ( 28c299...928e2a )
by Mathias
12:40
created

Form::renderBare()   C

Complexity

Conditions 15
Paths 156

Size

Total Lines 99
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 16.2818

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 99
c 0
b 0
f 0
ccs 46
cts 56
cp 0.8214
rs 5.4499
cc 15
nc 156
nop 3
crap 16.2818

How to fix   Long Method    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
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Core forms view helpers */
11
namespace Core\Form\View\Helper;
12
13
use Zend\Form\View\Helper\Form as ZendForm;
14
use Zend\Form\FormInterface;
15
use Zend\Form\FieldsetInterface;
16
use Core\Form\ViewPartialProviderInterface;
17
use Core\Form\ExplicitParameterProviderInterface;
18
use Core\Form\Element\ViewHelperProviderInterface;
19
use Core\Form\DescriptionAwareFormInterface;
20
21
/**
22
 * Helper to render a formular.
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 */
26
class Form extends ZendForm
27
{
28
    /**#@+
29
     * Layout constants.
30
     * @var string
31
     */
32
    const LAYOUT_HORIZONTAL = 'form-horizontal';
33
    const LAYOUT_INLINE     = 'form-inline';
34
    const LAYOUT_VERTICAL   = '';
35
    const LAYOUT_BARE       = 'form-bare';
36
    /**#@-*/
37
    
38
    
39
    /**
40
     * Invoke as function
41
     *
42
     * @param  null|FormInterface $form
43
     * @param string $layout
44
     * @param array $parameter
45
     * @return string
46
     */
47 4
    public function __invoke(FormInterface $form = null, $layout = self::LAYOUT_HORIZONTAL, $parameter = array())
48
    {
49 4
        if (!$form) {
50
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Core\Form\View\Helper\Form which is incompatible with the documented return type string.
Loading history...
51
        }
52
    
53 4
        return $this->render($form, $layout, $parameter);
54
    }
55
    
56
    /**
57
     * Render a form from the provided $form,
58
     *
59
     * @param FormInterface $form
60
     * @param string $layout
61
     * @param array $parameter
62
     * @return string
63
     */
64 4
    public function renderBare(FormInterface $form, $layout = self::LAYOUT_HORIZONTAL, $parameter = array())
65
    {
66
        /* @var $renderer \Zend\View\Renderer\PhpRenderer
67
         * @var $headscript \Zend\View\Helper\HeadScript
68
         * @var $basepath \Zend\View\Helper\BasePath */
69 4
        $renderer   = $this->getView();
70 4
        $headscript = $renderer->plugin('headscript');
71 4
        $basepath   = $renderer->plugin('basepath');
72
        
73 4
        $headscript->appendFile($basepath('modules/Core/js/core.spinnerbutton.js'))
0 ignored issues
show
Bug introduced by
The method appendFile() does not exist on Zend\View\Helper\AbstractHelper. It seems like you code against a sub-type of Zend\View\Helper\AbstractHelper such as Zend\View\Helper\FlashMessenger or Zend\View\Helper\Placeho...iner\AbstractStandalone or Zend\Mvc\Plugin\FlashMes...w\Helper\FlashMessenger or Core\View\Helper\Alert or Auth\View\Helper\Auth or Zend\View\Helper\Navigation\AbstractHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $headscript->/** @scrutinizer ignore-call */ 
74
                     appendFile($basepath('modules/Core/js/core.spinnerbutton.js'))
Loading history...
74
                   //->appendFile($basepath('/assets/select2/js/select2.min.js'))
75 4
                   ->appendFile($basepath('modules/Core/js/core.forms.js'));
76
77
        /* @noinspection PhpParamsInspection */
78
        //$renderer->headLink()->appendStylesheet($basepath('/assets/select2/css/select2.css'));
79
80 4
        if ($scripts = $form->getOption('headscript')) {
81
            if (!is_array($scripts)) {
82
                $scripts = array($scripts);
83
            }
84
            foreach ($scripts as $script) {
85
                $headscript->appendFile($basepath($script));
86
            }
87
        }
88
        
89 4
        $class = $form->getAttribute('class');
90 4
        $class = preg_replace('~\bform-[^ ]+\b~', '', $class);
91 4
        $class .= ' ' . $layout;
92
        
93 4
        $form->setAttribute('class', $class);
94
95 4
        $formId = $form->getAttribute('id') ?: $form->getName();
96 4
        $formId = preg_replace(
97 4
            array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'),
98 4
            array('-'              , '-'    , ''       ),
99 4
            $formId
100
        );
101 4
        $form->setAttribute(
102 4
            'id',
103 4
            $formId
104
105
        );
106
107 4
        if (method_exists($form, 'prepare')) {
108 4
            $form->prepare();
109
        }
110
    
111 4
        $formContent = '';
112
    
113 4
        if ($form instanceof ViewPartialProviderInterface) {
114
            return $renderer->partial($form->getViewPartial(), array_merge(['element' => $form], $parameter));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $renderer->partia...=> $form), $parameter)) also could return the type Zend\View\Helper\Partial which is incompatible with the documented return type string.
Loading history...
115
        }
116
117
        /* @var $element \Zend\Form\ElementInterface */
118 4
        foreach ($form as $element) {
119 4
            $parameterPartial = $parameter;
120 4
            $elementId = $element->getAttribute('id');
121 4
            if (!$elementId) {
122 4
                $elementId = preg_replace(
123 4
                    array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'),
124 4
                    array('-'             , '-',     ''),
125 4
                    $element->getName()
126
                );
127
            }
128 4
            $element->setAttribute('id', "$formId-$elementId");
129 4
            $element->setOption('__form_id__', $formId);
130 4
            if ($element instanceof ExplicitParameterProviderInterface) {
131
                /* @var $element ExplicitParameterProviderInterface */
132
                $parameterPartial = array_merge($element->getParams(), $parameterPartial);
133
            }
134
135 4
            if ($element instanceof ViewPartialProviderInterface) {
136
                /* @var $element ViewPartialProviderInterface */
137 4
                $parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial);
138
                /** @noinspection PhpToStringImplementationInspection */
139 4
                $formContent .= $renderer->partial(
140 4
                    $element->getViewPartial(),
141 4
                    $parameterPartial
142
                );
143 4
            } elseif ($element instanceof FieldsetInterface) {
144 3
                if ($element instanceof ViewHelperProviderInterface) {
145
                    /* @var $element ViewHelperProviderInterface */
146
                    $helper = $element->getViewHelper();
147
                    if (is_string($helper)) {
148
                        $helper = $renderer->plugin($helper);
149
                    }
150
151
                    $formContent .= $helper($element);
152
                } else {
153 3
                    $formContent .= $renderer->formCollection($element, true, $layout);
154
                }
155 4
            } elseif (false !== $element->getOption('use_formrow_helper')) {
156 4
                $formContent .= $renderer->formRow($element, null, null, $layout);
157
            } else {
158 4
                $formContent .= $renderer->formElement($element);
159
            }
160
        }
161
        
162 4
        return $this->openTag($form) . $formContent . $this->closeTag();
163
    }
164
    
165
    /**
166
     * Renders a form from the provided form.
167
     * Wraps this form in a div-container and renders the label,
168
     * if any.
169
     *
170
     * @param FormInterface $form
171
     * @param string $layout
172
     * @param array $parameter
173
     * @uses renderBare()
174
     * @see \Zend\Form\View\Helper\Form::render()
175
     * @return string
176
     */
177 4
    public function render(FormInterface $form, $layout = self::LAYOUT_HORIZONTAL, $parameter = array())
178
    {
179
        /* @var $renderer \Zend\View\Renderer\PhpRenderer */
180 4
        $formContent = $this->renderBare($form, $layout, $parameter);
181 4
        $renderer    = $this->getView();
182
        
183 4
        if ($form instanceof DescriptionAwareFormInterface && $form->isDescriptionsEnabled()) {
184
            /* @var $form DescriptionAwareFormInterface|FormInterface */
185 1
            $renderer->headscript()->appendFile(
0 ignored issues
show
Bug introduced by
The method headscript() does not exist on Zend\View\Renderer\RendererInterface. It seems like you code against a sub-type of Zend\View\Renderer\RendererInterface such as Zend\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
            $renderer->/** @scrutinizer ignore-call */ 
186
                       headscript()->appendFile(
Loading history...
186 1
                    $renderer->basepath('modules/Core/js/forms.descriptions.js')
0 ignored issues
show
Bug introduced by
The method basepath() does not exist on Zend\View\Renderer\RendererInterface. It seems like you code against a sub-type of Zend\View\Renderer\RendererInterface such as Zend\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
                    $renderer->/** @scrutinizer ignore-call */ 
187
                               basepath('modules/Core/js/forms.descriptions.js')
Loading history...
187
                );
188
                
189 1
            if ($desc = $form->getOption('description', '')) {
0 ignored issues
show
Unused Code introduced by
The call to Zend\Form\ElementInterface::getOption() has too many arguments starting with ''. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
            if ($desc = $form->/** @scrutinizer ignore-call */ getOption('description', '')) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
The method getOption() does not exist on Core\Form\DescriptionAwareFormInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Core\Form\DescriptionAwareFormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
            if ($desc = $form->/** @scrutinizer ignore-call */ getOption('description', '')) {
Loading history...
190 1
                $descriptionParams=$form->getOption('description_params');
191 1
                $translator = $this->getTranslator();
192 1
                $textDomain = $this->getTranslatorTextDomain();
193 1
                $desc = $translator->translate($desc, $textDomain);
194 1
                if ($descriptionParams) {
195
                    array_unshift($descriptionParams, $desc);
196
                    $desc = call_user_func_array('sprintf', $descriptionParams);
197
                }
198
            }
199
                
200 1
            $formContent = sprintf(
201 1
                    '<div class="daf-form-container row">
202
                        <div class="daf-form col-md-8"><div class="panel panel-default"><div class="panel-body">%s</div></div></div>
203
                        <div class="daf-desc col-md-4">
204
                            <div class="daf-desc-content alert alert-info">%s</div>
205
                        </div>
206
                    </div>',
207 1
                    $formContent,
208 1
                    $desc
209
                );
210
        } else {
211 3
            $formContent = '<div class="form-content">' . $formContent . '</div>';
212
        }
213
        
214
        $markup = '<div id="form-%s" class="form-container">'
215
                . '%s'
216
                . '%s'
217 4
                . '</div>';
218
        
219 4
        if ($label = $form->getLabel()) {
0 ignored issues
show
Bug introduced by
The method getLabel() does not exist on Core\Form\DescriptionAwareFormInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Core\Form\DescriptionAwareFormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
        if ($label = $form->/** @scrutinizer ignore-call */ getLabel()) {
Loading history...
220 1
            $label = '<div class="form-headline"><h3>' . $renderer->translate($label) . '</h3></div>';
0 ignored issues
show
Bug introduced by
The method translate() does not exist on Zend\View\Renderer\RendererInterface. It seems like you code against a sub-type of Zend\View\Renderer\RendererInterface such as Zend\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
            $label = '<div class="form-headline"><h3>' . $renderer->/** @scrutinizer ignore-call */ translate($label) . '</h3></div>';
Loading history...
221
        }
222
        
223 4
        return sprintf(
224 4
            $markup,
225 4
            $form->getAttribute('id') ?: $form->getName(),
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Core\Form\DescriptionAwareFormInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Core\Form\DescriptionAwareFormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
            $form->getAttribute('id') ?: $form->/** @scrutinizer ignore-call */ getName(),
Loading history...
Bug introduced by
The method getAttribute() does not exist on Core\Form\DescriptionAwareFormInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Core\Form\DescriptionAwareFormInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
            $form->/** @scrutinizer ignore-call */ 
226
                   getAttribute('id') ?: $form->getName(),
Loading history...
226 4
            $label,
227 4
            $formContent
228
        );
229
    }
230
}
231