Options::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 10
dl 0
loc 13
ccs 2
cts 2
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Pelmered\LaravelHttpOAuthHelper;
4
5
use Carbon\Carbon;
6
use Closure;
7
use Illuminate\Support\Facades\Validator;
8
use Illuminate\Validation\Rule;
9
10
class Options
11
{
12
    /**
13
     * @param  array<string>  $scopes
14
     */
15 32
    final public function __construct(
16
        public array $scopes = [],
17
        public string $authType = Credentials::AUTH_TYPE_BEARER,
18
        public string $grantType = Credentials::GRANT_TYPE_CLIENT_CREDENTIALS,
19
        public string $tokenType = AccessToken::TOKEN_TYPE_BEARER,
20
        public string $tokenName = 'token',
21
        public int|string|Closure|Carbon $expires = 3600,
22
        public string|Closure $accessToken = 'access_token',
23
        public ?Closure $tokenTypeCustomCallback = null,
24
        public ?string $cacheKey = null,
25
        public ?string $cacheDriver = null,
26
    ) {
27 32
        $this->validateOptions();
28
    }
29
30
    /**
31
     * @return array<string, string>
32
     */
33
    public function toArray(): array
34
    {
35
        return get_object_vars($this);
36
    }
37
38 32
    protected function validateOptions(): void
39
    {
40
        // Note: closures can't be checked at this point since we don't have access to the response objects
41 32
        Validator::make((array) $this, [
42 32
            'scopes.*' => 'string',
43 32
            'authType' => Rule::in([
44 32
                Credentials::AUTH_TYPE_BEARER,
45 32
                Credentials::AUTH_TYPE_BODY,
46 32
                Credentials::AUTH_TYPE_QUERY,
47 32
                Credentials::AUTH_TYPE_BASIC,
48 32
                Credentials::AUTH_TYPE_CUSTOM,
49 32
            ]),
50 32
            'grantType' => Rule::in([
51 32
                Credentials::GRANT_TYPE_CLIENT_CREDENTIALS,
52 32
                Credentials::GRANT_TYPE_PASSWORD_CREDENTIALS,
53 32
            ]),
54 32
            'tokenType' => Rule::in([
55 32
                AccessToken::TOKEN_TYPE_BEARER,
56 32
                AccessToken::TOKEN_TYPE_QUERY,
57 32
                AccessToken::TOKEN_TYPE_CUSTOM,
58 32
            ]),
59 32
            'tokenName' => 'string',
60 32
        ])->validate();
61
    }
62
63 25
    public function getScopes(): string
64
    {
65 25
        return implode(' ', $this->scopes);
66
    }
67
68
    /**
69
     * @param  array<string, mixed>  ...$parameters
70
     */
71 5
    public static function make(...$parameters): static
72
    {
73 5
        $defaults = static::getDefaults();
74 5
        $options  = array_merge($defaults, ...$parameters);
75
76 5
        return new static(...$options);
0 ignored issues
show
Bug introduced by
$options is expanded, but the parameter $scopes of Pelmered\LaravelHttpOAut...\Options::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

76
        return new static(/** @scrutinizer ignore-type */ ...$options);
Loading history...
77
    }
78
79
    /**
80
     * @return array<string, mixed>
81
     */
82 5
    protected static function getDefaults(): array
83
    {
84 5
        return [
85 5
            'scopes'      => [],
86 5
            'grantType'   => Credentials::GRANT_TYPE_CLIENT_CREDENTIALS,
87 5
            'tokenType'   => AccessToken::TOKEN_TYPE_BEARER,
88 5
            'authType'    => Credentials::AUTH_TYPE_BEARER,
89 5
            'expires'     => 3600,
90 5
            'accessToken' => 'access_token',
91 5
        ];
92
    }
93
}
94