IdentitySearchQuery::setEmail()   A
last analyzed

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 2014 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
 */
18
19
namespace Surfnet\StepupMiddlewareClient\Identity\Dto;
20
21
use Assert;
22
use Surfnet\StepupMiddlewareClient\Dto\HttpQuery;
23
24
class IdentitySearchQuery implements HttpQuery
25
{
26
    /**
27
     * @var string
28
     */
29
    private $nameId;
30
31
    /**
32
     * @var string
33
     */
34
    private $institution;
35
36
    /**
37
     * @var string
38
     */
39
    private $email;
40
41
    /**
42
     * @var string
43
     */
44
    private $commonName;
45
46
    /**
47
     * @param string $institution
48
     * @return IdentitySearchQuery
49
     */
50
    public function setInstitution($institution)
51
    {
52
        $this->assertNonEmptyString($institution, 'institution');
53
54
        $this->institution = $institution;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $commonName
61
     * @return IdentitySearchQuery
62
     */
63
    public function setCommonName($commonName)
64
    {
65
        $this->assertNonEmptyString($commonName, 'commonName');
66
67
        $this->commonName = $commonName;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param string $email
74
     * @return IdentitySearchQuery
75
     */
76
    public function setEmail($email)
77
    {
78
        $this->assertNonEmptyString($email, 'email');
79
80
        $this->email = $email;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $nameId
87
     * @return IdentitySearchQuery
88
     */
89
    public function setNameId($nameId)
90
    {
91
        $this->assertNonEmptyString($nameId, 'nameId');
92
93
        $this->nameId = $nameId;
94
95
        return $this;
96
    }
97
98
    private function assertNonEmptyString($value, $name)
99
    {
100
        $message = sprintf(
101
            '"%s" must be a non-empty string, "%s" given',
102
            $name,
103
            (is_object($value) ? get_class($value) : gettype($value))
104
        );
105
106
        Assert\that($value)->string($message)->notEmpty($message);
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function toHttpQuery()
113
    {
114
        if ($this->institution) {
115
            $fields = ['institution' => $this->institution];
116
        }
117
118
        if ($this->commonName) {
119
            $fields['commonName'] = $this->commonName;
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...
120
        }
121
122
        if ($this->email) {
123
            $fields['email'] = $this->email;
124
        }
125
126
        if ($this->nameId) {
127
            $fields['NameID'] = $this->nameId;
128
        }
129
130
        return '?' . http_build_query($fields);
131
    }
132
}
133