__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 10
dl 0
loc 16
rs 10
c 1
b 0
f 0

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
/**
4
 * Copyright 2020 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\Stepup\Identity\Event;
20
21
use Surfnet\Stepup\Identity\AuditLog\Metadata;
22
use Surfnet\Stepup\Identity\Value\CommonName;
23
use Surfnet\Stepup\Identity\Value\DocumentNumber;
24
use Surfnet\Stepup\Identity\Value\Email;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\Stepup\Identity\Value\Locale;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\Value\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Surfnet\Stepup\Identity\Value\NameId;
29
use Surfnet\Stepup\Identity\Value\SecondFactorId;
30
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
31
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory;
32
use Surfnet\Stepup\Identity\Value\UnknownVettingType;
33
use Surfnet\Stepup\Identity\Value\VettingType;
34
use Surfnet\StepupBundle\Value\SecondFactorType;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
37
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
38
39
/**
40
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
42
class SecondFactorVettedWithoutTokenProofOfPossession extends IdentityEvent implements
43
    Forgettable,
44
    RightToObtainDataInterface
45
{
46
    /**
47
     * @var string[]
48
     */
49
    private array $allowlist = [
50
        'identity_id',
51
        'name_id',
52
        'identity_institution',
53
        'second_factor_id',
54
        'second_factor_type',
55
        'preferred_locale',
56
        'email',
57
        'common_name',
58
        'second_factor_identifier',
59
        'vetting_type',
60
    ];
61
62
    /**
63
     * @var DocumentNumber
64
     */
65
    public DocumentNumber $documentNumber;
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $identityId should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $nameId should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $institution should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secondFactorId should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secondFactorType should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secondFactorIdentifier should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $commonName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $email should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $preferredLocale should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $vettingType should have a doc-comment as per coding-style.
Loading history...
68
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
69
     */
70
    public function __construct(
71
        IdentityId $identityId,
72
        public NameId $nameId,
73
        Institution $institution,
74
        public SecondFactorId $secondFactorId,
75
        public SecondFactorType $secondFactorType,
76
        public SecondFactorIdentifier $secondFactorIdentifier,
77
        public CommonName $commonName,
78
        public Email $email,
79
        /**
80
         * @var Locale Eg. "en_GB"
81
         */
82
        public Locale $preferredLocale,
83
        public ?VettingType $vettingType,
84
    ) {
85
        parent::__construct($identityId, $institution);
86
    }
87
88
    public function getAuditLogMetadata(): Metadata
89
    {
90
        $metadata = new Metadata();
91
        $metadata->identityId = $this->identityId;
92
        $metadata->identityInstitution = $this->identityInstitution;
93
        $metadata->secondFactorId = $this->secondFactorId;
94
        $metadata->secondFactorType = $this->secondFactorType;
95
        $metadata->secondFactorIdentifier = $this->secondFactorIdentifier;
96
        $metadata->vettingType = $this->vettingType;
97
98
        return $metadata;
99
    }
100
101
    public static function deserialize(array $data): self
102
    {
103
        $secondFactorType = new SecondFactorType($data['second_factor_type']);
104
        return new self(
105
            new IdentityId($data['identity_id']),
106
            new NameId($data['name_id']),
107
            new Institution($data['identity_institution']),
108
            new SecondFactorId($data['second_factor_id']),
109
            $secondFactorType,
110
            SecondFactorIdentifierFactory::unknownForType($secondFactorType),
111
            CommonName::unknown(),
112
            Email::unknown(),
113
            new Locale($data['preferred_locale']),
114
            new UnknownVettingType(),
115
        );
116
    }
117
118
    /**
119
     * The data ending up in the event_stream, be careful not to include sensitive data here!
120
     *
121
     * @return array<string, mixed>
122
     */
123
    public function serialize(): array
124
    {
125
        return [
126
            'identity_id' => (string)$this->identityId,
127
            'name_id' => (string)$this->nameId,
128
            'identity_institution' => (string)$this->identityInstitution,
129
            'second_factor_id' => (string)$this->secondFactorId,
130
            'second_factor_type' => (string)$this->secondFactorType,
131
            'preferred_locale' => (string)$this->preferredLocale,
132
        ];
133
    }
134
135
    public function getSensitiveData(): SensitiveData
136
    {
137
        return (new SensitiveData)
138
            ->withCommonName($this->commonName)
139
            ->withEmail($this->email)
140
            ->withSecondFactorIdentifier($this->secondFactorIdentifier, $this->secondFactorType)
141
            ->withVettingType($this->vettingType);
0 ignored issues
show
Bug introduced by
It seems like $this->vettingType can also be of type null; however, parameter $vettingType of Surfnet\StepupMiddleware...Data::withVettingType() does only seem to accept Surfnet\Stepup\Identity\Value\VettingType, 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

141
            ->withVettingType(/** @scrutinizer ignore-type */ $this->vettingType);
Loading history...
142
    }
143
144
    public function setSensitiveData(SensitiveData $sensitiveData): void
145
    {
146
        $this->email = $sensitiveData->getEmail();
147
        $this->commonName = $sensitiveData->getCommonName();
148
        $this->secondFactorIdentifier = $sensitiveData->getSecondFactorIdentifier();
149
        $this->vettingType = $sensitiveData->getVettingType();
150
    }
151
152
    public function obtainUserData(): array
153
    {
154
        $serializedPublicUserData = $this->serialize();
155
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
156
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
157
    }
158
159
    /**
160
     * @return string[]
161
     */
162
    public function getAllowlist(): array
163
    {
164
        return $this->allowlist;
165
    }
166
}
167