Completed
Push — master ( 78f4d4...47ceaa )
by Michiel
02:25 queued 51s
created

ProfileSearchQuery::setIdentityId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2019 SURFnet B.V.
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
 */
18
19
namespace Surfnet\StepupMiddlewareClient\Identity\Dto;
20
21
use Assert;
22
use Surfnet\StepupMiddlewareClient\Dto\HttpQuery;
23
24
class ProfileSearchQuery implements HttpQuery
25
{
26
    /**
27
     * @var string
28
     */
29
    private $identityId;
30
31
    /**
32
     * @var string
33
     */
34
    private $actorId;
35
36
    /**
37
     * @param string $identityId
38
     * @param string $actorId
39
     */
40
    public function __construct($identityId, $actorId)
41
    {
42
        $this->identityId = $identityId;
43
        $this->actorId = $actorId;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getIdentityId()
50
    {
51
        return $this->identityId;
52
    }
53
54
    /**
55
     * @param string $actorId
56
     * @return ProfileSearchQuery
57
     */
58
    public function setActorId($actorId)
59
    {
60
        $this->assertNonEmptyString($actorId, 'institution');
61
62
        $this->actorId = $actorId;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $identityId
69
     * @return ProfileSearchQuery
70
     */
71
    public function setIdentityId($identityId)
72
    {
73
        $this->assertNonEmptyString($identityId, 'institutionId');
74
75
        $this->identityId = $identityId;
76
77
        return $this;
78
    }
79
80 View Code Duplication
    private function assertNonEmptyString($value, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $message = sprintf(
83
            '"%s" must be a non-empty string, "%s" given',
84
            $name,
85
            (is_object($value) ? get_class($value) : gettype($value))
86
        );
87
88
        Assert\that($value)->string($message)->notEmpty($message);
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function toHttpQuery()
95
    {
96
        if ($this->actorId) {
97
            $fields = ['actorId' => $this->actorId];
98
        }
99
100
        return '?' . http_build_query($fields);
0 ignored issues
show
Bug introduced by
The variable $fields does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
101
    }
102
}
103