MenuFactory::createItem()   C
last analyzed

Complexity

Conditions 9
Paths 18

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 52
rs 6.5703
cc 9
eloc 36
nc 18
nop 2

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
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DP\Core\CoreBundle\Menu;
13
14
use Knp\Menu\MenuFactory as BaseMenuFactory;
15
use DP\Core\CoreBundle\Menu\MenuItem;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
18
class MenuFactory extends BaseMenuFactory
19
{
20
    protected $generator;
21
22
    public function __construct(UrlGeneratorInterface $generator)
23
    {
24
        $this->generator = $generator;
25
    }
26
    
27
    public function createItem($name, array $options = array())
28
    {
29
        $item = new MenuItem($name, $this);
30
31
        $options = array_merge(
32
            array(
33
                'uri' => null,
34
                'label' => null,
35
                'attributes' => array(),
36
                'linkAttributes' => array(),
37
                'childrenAttributes' => array(),
38
                'labelAttributes' => array(),
39
                'extras' => array(),
40
                'display' => true,
41
                'displayChildren' => true,
42
                'pattern' => null, 
43
            ),
44
            $options
45
        );
46
        
47
        if (!empty($options['route'])) {            
48
            $params = isset($options['routeParameters']) ? $options['routeParameters'] : array();
49
            $absolute = isset($options['routeAbsolute']) ? $options['routeAbsolute'] : false;
50
            $options['uri'] = $this->generator->generate($options['route'], $params, $absolute);
51
            
52
            if (!isset($options['pattern']) || empty($options['pattern'])) {
53
                $options['pattern'] = $options['uri'];
54
            }
55
        }
56
        
57
        // Ajoute un dolar au pattern utilisé pour déterminer 
58
        // par regex s'il s'agit de l'item actuel
59
        if (!empty($options['pattern']) && isset($options['pattern_strict']) 
60
        && $options['pattern_strict'] === true) {
61
            $options['pattern'] .= '$';
62
        }
63
64
        $item
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Knp\Menu\ItemInterface as the method setPattern() does only exist in the following implementations of said interface: DP\Core\CoreBundle\Menu\MenuItem.

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...
65
            ->setUri($options['uri'])
66
            ->setLabel($options['label'])
67
            ->setAttributes($options['attributes'])
68
            ->setLinkAttributes($options['linkAttributes'])
69
            ->setChildrenAttributes($options['childrenAttributes'])
70
            ->setLabelAttributes($options['labelAttributes'])
71
            ->setExtras($options['extras'])
72
            ->setDisplay($options['display'])
73
            ->setDisplayChildren($options['displayChildren'])
74
            ->setPattern($options['pattern'])
75
        ;
76
77
        return $item;
78
    }
79
}
80