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

FormWizardContainer::render()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 63
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 6.8825
c 0
b 0
f 0
cc 8
eloc 42
nc 42
nop 3

How to fix   Long Method   

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 Core\Form\ViewPartialProviderInterface;
14
use Core\Form\ExplicitParameterProviderInterface;
15
use Core\Form\Element\ViewHelperProviderInterface;
16
use Core\Form\Container;
17
use Core\Form\WizardContainer;
18
use Zend\Form\View\Helper\AbstractHelper;
19
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...
20
21
/**
22
 * Helper for rendering form wizard containers
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 */
26
class FormWizardContainer extends AbstractHelper
27
{
28
29
    /**
30
     * Invoke as function.
31
     *
32
     * Proxies to {@link render()} or returns self.
33
     *
34
     * @param  null|Container $container
35
     * @param string $layout
36
     * @param array $parameter
37
     * @return FormContainer|string
0 ignored issues
show
Documentation introduced by
Should the return type not be FormWizardContainer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39 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...
40
    {
41
        if (!$container) {
42
            return $this;
43
        }
44
45
        return $this->render($container, $layout, $parameter);
0 ignored issues
show
Compatibility introduced by
$container of type object<Core\Form\Container> is not a sub-type of object<Core\Form\WizardContainer>. It seems like you assume a child class of the class Core\Form\Container 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...
46
    }
47
48
    /**
49
     * Renders the forms of a container.
50
     *
51
     * @param WizardContainer $container
52
     * @param string $layout
53
     * @param array $parameter
54
     * @return string
55
     */
56
    public function render(WizardContainer $container, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array())
57
    {
58
        
59
        $content = '';
60
61
        $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...
62
63
        $tabsNav = '';
64
        $tabsContent = '';
65
        $containerParams = [
66
            'pager' => true,
67
            'finish_label' => 'Finish',
68
            'finish_href' => 'javascript:;',
69
            'finish_enabled' => true,
70
        ];
71
72
        if (isset($parameter['wizard'])) {
73
            $containerParams = array_merge($containerParams, $parameter['wizard']);
74
            unset($parameter['wizard']);
75
        }
76
77
        $translate = $this->getView()->plugin('translate');
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...
78
        $formContainer = $this->getView()->plugin('formcontainer');
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...
79
80
        if ($container instanceof ViewPartialProviderInterface) {
81
            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...
82
        }
83
84
        $containerId = $container->getAttribute('id');
85
        if (!$containerId) {
86
            $containerId = 'wizardcontainer-' . strtolower(str_replace('\\', '-', get_class($container)));
87
        }
88
89
        foreach ($container as $tabElement) {
90
            $tabId = $containerId . '-' . strtolower($tabElement->getName());
91
            $tabsNav .= '<li><a data-toggle="tab" href="#' . $tabId . '">' . $translate($tabElement->getLabel()) . '</a></li>';
92
            $tabsContent .= '<div class="tab-pane" id="' . $tabId . '">'
93
                          . $formContainer($tabElement, $layout, $parameter)
94
                          . '</div>';
95
        }
96
97
        $content .= '<style type="text/css">.tab-content > div > div:first-child { margin-top: 10px; }</style><div class="wizard-container" id="' . $containerId . '">'
98
                  . '<ul>' . $tabsNav . '</ul>'
99
                  . '<div class="tab-content">' . $tabsContent . '</div>';
100
        if ($containerParams['pager']) {
101
            $content .='<ul class="pager wizard">'
102
                  . '<li class="previous"><a href="javascript:;">&larr; ' . $translate('previous') . '</a></li>'
103
                  . '<li class="next"><a href="javascript:;">' . $translate('Next') . ' &rarr;</a></li>'
104
                  . '<li class="finish' . ($containerParams['finish_enabled'] ? '' : ' disabled') . '">'
105
                  . (false !== $containerParams['finish_label']
106
                     ? '<a class="pull-right" href="' . $containerParams['finish_href'] . '">'
107
                       . $translate($containerParams['finish_label']) . ' &bull;</a>'
108
                     : ''
109
                    )
110
                  . '</li></ul>';
111
        }
112
        $content .= '</div>';
113
114
        $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...
115
        
116
        return $content;
117
118
    }
119
}
120