Completed
Push — master ( 51e253...53c186 )
by Andrii
05:27
created

UserMenu   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 39
ccs 0
cts 26
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A widget() 0 9 1
B items() 0 26 1
1
<?php
2
3
namespace hipanel\menus;
4
5
use hiqdev\thememanager\widgets\UserMenu as UserMenuWidget;
6
use Yii;
7
8
class UserMenu extends \hiqdev\menumanager\Menu
9
{
10
    public function widget($options = [])
11
    {
12
        $defaultOptions = [
13
            'class' => UserMenuWidget::class,
14
            'options' => ['class' => 'sidebar-menu'],
15
        ];
16
17
        return parent::widget(array_merge($defaultOptions, $options));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class hiqdev\menumanager\Menu as the method widget() does only exist in the following sub-classes of hiqdev\menumanager\Menu: hipanel\menus\SidebarMenu, hipanel\menus\UserMenu. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
18
    }
19
20
    public function items()
21
    {
22
        return [
23
            'header' => [
24
                'label' => $this->render('userMenuHeader'),
25
            ],
26
            'profile' => [
27
                'label' => Yii::t('hipanel', 'Profile'),
28
                'url'   => ['/site/profile'],
29
            ],
30
            'logout' => [
31
                'label' => Yii::t('hipanel', 'Sign out'),
32
                'url'   => ['/site/logout'],
33
            ],
34
35
            'deposit' => [
36
                'label' => Yii::t('hipanel', 'Recharge account'),
37
                'url'   => ['@pay/deposit'],
38
                'visible' => Yii::$app->user->can('deposit'),
39
            ],
40
            'ticket' => [
41
                'label' => Yii::t('hipanel', 'Create ticket'),
42
                'url'   => ['@ticket/create'],
43
            ],
44
        ];
45
    }
46
}
47