Completed
Pull Request — master (#7)
by Angel
08:45 queued 01:09
created

Modular   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 58.81%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 65
ccs 10
cts 17
cp 0.5881
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

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