Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

FormContainer::render()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 3
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 Core\Form\ViewPartialProviderInterface;
14
use Core\Form\ExplicitParameterProviderInterface;
15
use Core\Form\Element\ViewHelperProviderInterface;
16
use Core\Form\Container;
17
use Zend\Form\View\Helper\AbstractHelper;
18
use Core\Form\SummaryForm as CoreSummaryForm;
19
20
/**
21
 * Helper for rendering form containers
22
 *
23
 * @author Mathias Gelhausen <[email protected]>
24
 */
25
class FormContainer extends AbstractHelper
26
{
27
    
28
    /**
29
     * Invoke as function.
30
     *
31
     * Proxies to {@link render()} or returns self.
32
     *
33
     * @param  null|Container $container
34
     * @param string $layout
35
     * @param array $parameter
36
     * @return FormContainer|string
37
     */
38 View Code Duplication
    public function __invoke(Container $container = null, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40
        if (!$container) {
41
            return $this;
42
        }
43
    
44
        return $this->render($container, $layout, $parameter);
45
    }
46
    
47
    /**
48
     * Renders the forms of a container.
49
     *
50
     * @param Container $container
51
     * @param string $layout
52
     * @param array $parameter
53
     * @return string
54
     */
55
    public function render(Container $container, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array())
56
    {
57
        
58
        $content = '';
59
60
        $content .= $container->renderPre($this->getView());
0 ignored issues
show
Documentation introduced by
$this->getView() is of type null|object<Zend\View\Renderer\RendererInterface>, but the function expects a object<Zend\View\Renderer\PhpRenderer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
    
62
        if ($container instanceof ViewPartialProviderInterface) {
63
            return $this->getView()->partial($container->getViewPartial(), array('element' => $container));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method partial() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
64
        }
65
66
        if (isset($parameter['render_label']) && $parameter['render_label'] && ($label = $container->getLabel())) {
67
            $content .= '<div class="container-headline"><h3>' . $this->getView()->translate($label) . '</h3></div>';
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        }
69
        foreach ($container as $element) {
70
            $content .= $this->renderElement($element, $layout, $parameter);
71
        }
72
73
        $content .= $container->renderPost($this->getView());
0 ignored issues
show
Documentation introduced by
$this->getView() is of type null|object<Zend\View\Renderer\RendererInterface>, but the function expects a object<Zend\View\Renderer\PhpRenderer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
        
75
        return $content;
76
77
    }
78
79
    public function renderElement($element, $layout, $parameter)
80
    {
81
        $parameterPartial = $parameter;
82
        $content = '';
83
        if ($element instanceof ExplicitParameterProviderInterface) {
84
            $parameterPartial = array_merge($element->getParams(), $parameterPartial);
85
        }
86
        if ($element instanceof ViewPartialProviderInterface) {
87
            $parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial);
88
            $content .= $this->getView()->partial(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method partial() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
89
                             $element->getViewPartial(),
90
                             $parameterPartial
91
            );
92
93
        } elseif ($element instanceof ViewHelperProviderInterface) {
94
            $helper = $element->getViewHelper();
95
            if (is_string($helper)) {
96
                $helper = $this->getView()->plugin($helper);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method plugin() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
97
            }
98
            $content .= $helper($element);
99
        } elseif ($element instanceof CoreSummaryForm) {
100
            $content .= $this->getView()->summaryForm($element);
0 ignored issues
show
Bug introduced by
The method summaryForm() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        } elseif ($element instanceof Container) {
102
            $content.= $this->render($element, $layout, $parameter);
103
        } else {
104
            $content.= $this->getView()->form($element, $layout, $parameter);
0 ignored issues
show
Bug introduced by
The method form() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
        }
106
107
        return $content;
108
    }
109
}
110