FormElement   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 26
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A renderHelper() 0 12 2
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Form\View\Helper;
12
13
use Zend\Form\Element;
14
use Zend\Form\ElementInterface;
15
use Zend\Form\View\Helper\FormElement as BaseFormElement;
16
use Zend\I18n\Translator\TranslatorAwareInterface;
17
use Zend\I18n\Translator\TranslatorAwareTrait;
18
19
/**
20
 * Class FormElement
21
 */
22
class FormElement extends BaseFormElement implements
23
    TranslatorAwareInterface
24
{
25
    use TranslatorAwareTrait;
26
27
    /**
28
     * View helper service name
29
     */
30
    const SERVICE = 'webinodrawformelement';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function renderHelper($name, ElementInterface $element)
36
    {
37
        // view helper option
38
        $viewHelper = $element->getOption('view_helper');
39
        empty($viewHelper) and $viewHelper = $name;
40
41
        $helper = $this->getView()->plugin($viewHelper);
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: WebinoDraw\View\Renderer\NotPhpRenderer, 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...
42
        $helper->setTranslatorEnabled($this->isTranslatorEnabled());
43
        $helper->setTranslatorTextDomain($this->getTranslatorTextDomain());
44
45
        return $helper($element);
46
    }
47
}
48