Modular   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 63
ccs 10
cts 13
cp 0.7692
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isApplicable() 0 4 1
A ensureRules() 0 6 1
A createNotFoundException() 0 4 1
A createRules() 0 3 1
A init() 0 2 1
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 string $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 string $moduleId;
29
30
    /**
31
     * @var UrlRuleCreator
32
     */
33
    protected UrlRuleCreator $module;
34
35
    /**
36
     * @inheritdoc
37
     */    
38 21
    public function init()
39
    {
40 21
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 21
    protected function isApplicable(string $route): bool
46
    {
47
        // only parse rules which start with the module id
48 21
        return 0 === strpos($route, $this->moduleId);
49
    }
50
51
    /**
52
     * @inheritdoc
53
54
     */
55 21
    protected function createRules()
56
    {
57 21
        return $this->module->createUrlRules($this);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    protected function createNotFoundException(): NotFoundHttpException
64
    {
65
        return new NotFoundHttpException(
66
            strtr($this->notFoundMessage, ['{moduleId}' => $this->moduleId])
67
        );
68
    }
69
70 21
    protected function ensureRules()
71
    {
72 21
        $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...
73 21
        $this->module->initCreator($this);
74
75 21
        parent::ensureRules();
76
    }
77
}
78