Completed
Push — develop ( 168e99...0122be )
by
unknown
18:37 queued 07:11
created

FormContainer::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 9
nc 3
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Core\Form\View\Helper\SummaryForm.

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...
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
Compatibility introduced by
$this->getView() of type object<Zend\View\Renderer\RendererInterface> is not a sub-type of object<Zend\View\Renderer\PhpRenderer>. It seems like you assume a concrete implementation of the interface Zend\View\Renderer\RendererInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

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
        foreach ($container as $element) {
67
            $content .= $this->renderElement($element, $layout, $parameter);
68
        }
69
70
        $content .= $container->renderPost($this->getView());
0 ignored issues
show
Compatibility introduced by
$this->getView() of type object<Zend\View\Renderer\RendererInterface> is not a sub-type of object<Zend\View\Renderer\PhpRenderer>. It seems like you assume a concrete implementation of the interface Zend\View\Renderer\RendererInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
71
        
72
        return $content;
73
74
    }
75
76
    public function renderElement($element, $layout, $parameter)
77
    {
78
        $parameterPartial = $parameter;
79
        $content = '';
80
        if ($element instanceof ExplicitParameterProviderInterface) {
81
            $parameterPartial = array_merge($element->getParams(), $parameterPartial);
82
        }
83
        if ($element instanceof ViewPartialProviderInterface) {
84
            $parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial);
85
            $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...
86
                             $element->getViewPartial(),
87
                             $parameterPartial
88
            );
89
90
        } elseif ($element instanceof ViewHelperProviderInterface) {
91
            $helper = $element->getViewHelper();
92
            if (is_string($helper)) {
93
                $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...
94
            }
95
            $content .= $helper($element);
96
        } elseif ($element instanceof SummaryForm) {
97
            $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...
98
        } elseif ($element instanceof Container) {
99
            $content.= $this->render($element, $layout, $parameter);
100
        } else {
101
            $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...
102
        }
103
104
        return $content;
105
    }
106
}
107