m150917_193929_rbac   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 29 2
A down() 0 13 2
1
<?php
2
3
use yii\db\Migration;
4
5
class m150917_193929_rbac extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    public function up()
8
    {
9
        $auth = Yii::$app->authManager;
10
11
        if ($auth instanceof \yii\rbac\DbManager) {
12
            $guest = $auth->createRole('Public');
13
            $guest->description = 'Unauthenticated User';
14
            $auth->add($guest);
15
16
            $editor = $auth->createRole('Editor');
17
            $editor->description = 'prototype editor';
18
            $auth->add($editor);
19
20
            $permission = $auth->createPermission('backend_default');
21
            $permission->description = 'Backend Dashboard';
22
            $auth->add($permission);
23
24
            $permission = $auth->createPermission('app_site');
25
            $permission->description = 'Main Site Controller';
26
            $auth->add($permission);
27
28
            $auth->addChild($editor, $auth->getPermission('backend_default'));
0 ignored issues
show
Bug introduced by
It seems like $auth->getPermission('backend_default') can be null; however, addChild() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
29
            $auth->addChild($editor, $auth->getPermission('app_site'));
0 ignored issues
show
Bug introduced by
It seems like $auth->getPermission('app_site') can be null; however, addChild() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
30
31
            $auth->addChild($editor, $auth->getPermission('pages'));
0 ignored issues
show
Bug introduced by
It seems like $auth->getPermission('pages') can be null; however, addChild() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
32
        } else {
33
            throw new \yii\base\Exception('Application authManager must be an instance of \yii\rbac\DbManager');
34
        }
35
    }
36
37
    public function down()
38
    {
39
        $auth = Yii::$app->authManager;
40
41
        if ($auth instanceof \yii\rbac\DbManager) {
42
            $auth->remove($auth->getPermission('backend_default'));
0 ignored issues
show
Documentation introduced by
$auth->getPermission('backend_default') is of type object<yii\rbac\Item>|null, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

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...
43
            $auth->remove($auth->getPermission('app_site'));
0 ignored issues
show
Documentation introduced by
$auth->getPermission('app_site') is of type object<yii\rbac\Item>|null, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

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...
44
            $auth->remove($auth->getRole('Editor'));
0 ignored issues
show
Documentation introduced by
$auth->getRole('Editor') is of type object<yii\rbac\Item>|null, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

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...
45
            $auth->remove($auth->getRole('Public'));
0 ignored issues
show
Documentation introduced by
$auth->getRole('Public') is of type object<yii\rbac\Item>|null, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

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...
46
        } else {
47
            throw new \yii\base\Exception('Application authManager must be an instance of \yii\rbac\DbManager');
48
        }
49
    }
50
}
51