Assignment::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Model;
13
14
use Da\User\Traits\AuthManagerAwareTrait;
15
use Da\User\Validator\RbacItemsValidator;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\base\Model;
19
20
class Assignment extends Model
21
{
22
    use AuthManagerAwareTrait;
23
24
    public $items = [];
25
    public $user_id;
26
    public $updated = false;
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws InvalidConfigException
32
     */
33
    public function init()
34
    {
35
        parent::init();
36
37
        if ($this->user_id === null) {
38
            throw new InvalidConfigException('"user_id" must be set.');
39
        }
40
41
        $this->items = array_keys($this->getAuthManager()->getItemsByUser($this->user_id));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface yii\rbac\ManagerInterface as the method getItemsByUser() does only exist in the following implementations of said interface: Da\User\Component\AuthDbManagerComponent.

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
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function attributeLabels()
48
    {
49
        return [
50
            'items' => Yii::t('usuario', 'Items'),
51
        ];
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function rules()
58
    {
59
        return [
60
            ['user_id', 'required'],
61
            ['items', RbacItemsValidator::class],
62
            ['user_id', 'integer'],
63
        ];
64
    }
65
}
66