Completed
Pull Request — master (#7)
by aguevaraIL
09:19
created

Modular::createRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\roa\urlRules;
4
5
use Yii;
6
use yii\{base\InvalidConfigException, web\NotFoundHttpException};
7
8
/**
9
 * Url Rule to handle modules implementing the `UrlRuleCreator` interface.
10
 *
11
 * @author Angel (Faryshta) Guevara <[email protected]>
12
 */
13
class Modular extends Composite
14
{
15
    /**
16
     * @inheritdoc
17
     *
18
     * can accept parameters.
19
     *
20
     * - {moduleId}: the unique module id associated to this rule.
21
     */
22
    public $notFoundMessage = 'Unknown route for module `{moduleId}`.';
23
24
    /**
25
     * @var string unique id to grab the module from the application that will
26
     * parse the rules.
27
     */
28
    public $moduleId;
29
30
    /**
31
     * @var UrlRuleCreator
32
     */
33 21
    protected $module;
34
35
    /**
36 21
     * @inheritdoc
37
     */
38
    protected function isApplicable(string $route): bool
39
    {
40
        // only parse rules which start with the module id
41
        return 0 === strpos($route, $this->moduleId);
42 21
    }
43
44 21
    /**
45
     * @inheritdoc
46
47 21
     */
48 21
    protected function createRules()
49
    {
50
        return $this->module->createUrlRules($this);
51
    }
52
53
    /**
54
     * @inheritdoc
55 21
     */
56
    protected function createNotFoundException(): NotFoundHttpException
57
    {
58
        return new NotFoundHttpException(
59
            strtr($this->notFoundMessage, ['{moduleId}' => $this->moduleId])
60
        );
61
    }
62
63
    protected function ensureRules()
64
    {
65
        if (empty($this->moduleId)) {
66
            throw new InvalidConfigException('`moduleId` must be set.');
67
        }
68
        $this->module = Yii::$app->getModule($this->moduleId);
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->getModule($this->moduleId) can also be of type yii\base\Module. However, the property $module is declared as type roaresearch\yii2\roa\urlRules\UrlRuleCreator. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        if (!$this->module instanceof UrlRuleCreator) {
70
            throw new InvalidConfigException(
71
                "Module `{$this->moduleId}` must implement "
72
                    . UrlRuleCreator::class
73
            );
74
        }
75
        $this->module->initCreator($this);
76
77
        parent::ensureRules();
78
    }
79
}
80