Passed
Push — master ( 36be2d...32b990 )
by Carlos
02:58
created

AuthForm::rules()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 35
nc 1
nop 0
dl 0
loc 57
rs 9.36
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace roaresearch\yii2\oauth2server\models;
4
5
use Yii;
6
use yii\db\IntegrityException;
7
8
class AuthForm extends \yii\base\Model
9
{
10
    public ?bool $authorized = null;
11
    public ?string $client_id = null;
12
    public ?string $scopes = null;
13
    public ?string $response_type = null;
14
    public ?string $state = null;
15
    public ?string $redirect_uri = null;
16
17
    protected ?OauthClients $clientModel = null;
18
    protected array $scopesList = [];
19
20
    public function rules()
21
    {
22
        $validatedClient = fn () => !$this->hasErrors('client_id');
23
24
        return [
25
            [
26
                [
27
                    'authorized',
28
                    'client_id',
29
                    'response_type',
30
                    'state',
31
                    'redirect_uri',
32
                ],
33
                'required',
34
            ],
35
            [['authorized'], 'boolean'],
36
            [['redirect_uri'], 'url'],
37
            [
38
                [
39
                    'client_id',
40
                    'scopes',
41
                    'response_type',
42
                    'state',
43
                    'redirect_uri',
44
                ],
45
                'string',
46
            ],
47
            [
48
                ['client_id'],
49
                'exist',
50
                'targetClass' => OauthClients::class,
51
            ],
52
            [
53
                ['scopes'],
54
                function ($attribute) {
55
                    try {
56
                         $this->getScopesList();
57
                    } catch (IntegrityException $e) {
58
                         $this->addError($atribute, $e->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $atribute does not exist. Did you maybe mean $attribute?
Loading history...
59
                    }
60
                },
61
                'when' => $validatedClient,
62
            ],
63
            [
64
                ['redirect_uri'],
65
                function ($attribute) {
66
                    if (
67
                        !$this->getClientModel()
68
                            ->validateUri($this->redirect_uri)
0 ignored issues
show
Bug introduced by
It seems like $this->redirect_uri can also be of type null; however, parameter $uri of roaresearch\yii2\oauth2s...hClients::validateUri() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
                            ->validateUri(/** @scrutinizer ignore-type */ $this->redirect_uri)
Loading history...
69
                    ) {
70
                        $this->addError(
71
                            $attribute,
72
                            "Redirection URI not recognized by client."
73
                        );
74
                    }
75
                },
76
                'when' => $validatedClient,
77
            ],
78
        ];
79
    }
80
81
    public function getClientModel(): ?OauthClients
82
    {
83
        if (empty($this->client_id) || isset($this->clientModel)) {
84
            return $this->clientModel;
85
        }
86
87
        $this->clientModel = OauthClients::findOne($this->client_id)
88
            ?: throw new IntegrityException(
89
                "Unknown client '{$this->client_id}'"
90
            );
91
92
        return $this->clientModel;
93
    }
94
95
    public function getScopesList(): array
96
    {
97
        if (empty($this->scopes) || !empty($this->scopesList)) {
98
            return $this->scopesList;
99
        }
100
101
        $clientModel = $this->getClientModel();
102
        foreach (explode(' ', $this->scopes) as $scope) {
103
            $this->scopesList[$scope] = $clientModel->assureScope($scope);
104
        }
105
106
        return $this->scopesList;
107
    }
108
}
109