Passed
Pull Request — master (#2978)
by Rafael
29:26 queued 26:35
created

UrlHelper::setPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 10
cc 4
nc 4
nop 1
crap 20
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Url;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Http\Uri;
18
use TYPO3\CMS\Core\Utility\MathUtility;
19
20
/**
21
 * Class UrlHelper
22
 *
23
 * @author Timo Hund <[email protected]>
24
 */
25
class UrlHelper extends Uri
26
{
27
    /**
28
     * @param string $host
29
     * @return UrlHelper
30
     * @deprecated Will be removed with v12. Use withHost instead.
31
     * @see Uri::withHost()
32
     */
33
    public function setHost(string $host)
34
    {
35
        $this->host = $host;
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $port
41
     * @return UrlHelper
42
     * @deprecated Will be removed with v12. Use withPort instead.
43
     * @see Uri::withPort()
44
     */
45
    public function setPort(string $port)
46
    {
47
        if ($port !== '') {
48
            if (MathUtility::canBeInterpretedAsInteger($port) === false) {
49
                $argumentType = is_object($port) ? get_class($port) : gettype($port);
0 ignored issues
show
introduced by
The condition is_object($port) is always false.
Loading history...
50
                throw new \InvalidArgumentException('Invalid port "' . $argumentType . '" specified, must be an integer.', 1436717324);
51
            }
52
53
            $port = (int)$port;
54
            if ($port < 1 || $port > 65535) {
55
                throw new \InvalidArgumentException('Invalid port "' . $port . '" specified, must be a valid TCP/UDP port.', 1436717326);
56
            }
57
        }
58
59
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
It seems like $port can also be of type string. However, the property $port is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $scheme
65
     * @return UrlHelper
66
     * @deprecated Will be removed with v12. Use Uri::withScheme instead.
67
     * @see Uri::withScheme()
68
     */
69
    public function setScheme(string $scheme)
70
    {
71
        $this->scheme = $this->sanitizeScheme($scheme);
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $path
77
     * @return UrlHelper
78
     * @deprecated Will be removed with v12. Use withPath instead.
79
     * @see Uri::withPath()
80
     */
81
    public function setPath($path)
82
    {
83
        if (!is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
84
            throw new \InvalidArgumentException('Invalid path provided. Must be of type string.', 1436717328);
85
        }
86
87
        if (strpos($path, '?') !== false) {
88
            throw new \InvalidArgumentException('Invalid path provided. Must not contain a query string.', 1436717330);
89
        }
90
91
        if (strpos($path, '#') !== false) {
92
            throw new \InvalidArgumentException('Invalid path provided; must not contain a URI fragment', 1436717332);
93
        }
94
        $this->path = $this->sanitizePath($path);
95
        return $this;
96
    }
97
98
    /**
99
     * Remove a given parameter from the query and create a new instance.
100
     *
101
     * @param string $parameterName
102
     * @return UrlHelper
103
     */
104
    public function withoutQueryParameter(string $parameterName): UrlHelper
105
    {
106
        parse_str($this->query, $parameters);
107
        if (isset($parameters[$parameterName])) {
108
            unset($parameters[$parameterName]);
109
        }
110
        $query = '';
111
        if (!empty($parameters)) {
112
            $query = http_build_query($parameters);
113
        }
114
        $query = $this->sanitizeQuery($query);
115
        $clonedObject = clone $this;
116
        $clonedObject->query = $query;
117
        return $clonedObject;
118
    }
119
120
    /**
121
     * @param string $parameterName
122
     * @throws \InvalidArgumentException
123
     * @return UrlHelper
124
     * @deprecated Will be removed with v12. Use withoutQueryParameter instead.
125
     */
126 36
    public function removeQueryParameter(string $parameterName): UrlHelper
127
    {
128 36
        parse_str($this->query, $parameters);
129 36
        if (isset($parameters[$parameterName])) {
130
            unset($parameters[$parameterName]);
131
        }
132 36
        $query = '';
133 36
        if (!empty($parameters)) {
134 36
            $query = http_build_query($parameters);
135
        }
136 36
        $this->query = $this->sanitizeQuery($query);
137
138 36
        return $this;
139
    }
140
141
    /**
142
     * Add a given parameter with value to the query and create a new instance.
143
     *
144
     * @param string $parameterName
145
     * @param mixed $value
146
     * @return UrlHelper
147
     */
148
    public function withQueryParameter(string $parameterName, $value): UrlHelper
149
    {
150
        parse_str($this->query, $parameters);
151
        $parameters[$parameterName] = $value;
152
        $query = '';
153
        if (!empty($parameters)) {
154
            $query = http_build_query($parameters);
155
        }
156
        $query = $this->sanitizeQuery($query);
157
        $clonedObject = clone $this;
158
        $clonedObject->query = $query;
159
        return $clonedObject;
160
    }
161
162
    /**
163
     * @param string $parameterName
164
     * @param mixed $value
165
     * @throws \InvalidArgumentException
166
     * @return UrlHelper
167
     * @deprecated Will be removed with v12. Use withQueryParameter instead.
168
     */
169
    public function addQueryParameter(string $parameterName, $value): UrlHelper
170
    {
171
        $parameters = $this->query;
172
        parse_str($this->query, $parameters);
0 ignored issues
show
Bug introduced by
$parameters of type string is incompatible with the type array expected by parameter $result of parse_str(). ( Ignorable by Annotation )

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

172
        parse_str($this->query, /** @scrutinizer ignore-type */ $parameters);
Loading history...
173
        if (empty($parameters)) {
174
            $parameters = [];
175
        }
176
        $parameters[$parameterName] = $value;
177
        $query = '';
178
        if (!empty($parameters)) {
179
            $query = http_build_query($parameters);
180
        }
181
        $this->query = $this->sanitizeQuery($query);
182
        return $this;
183
    }
184
185
    /**
186
     * @return string
187
     * @deprecated Will be removed with v12. Use __toString() instead.
188
     */
189 36
    public function getUrl(): string
190
    {
191 36
        return $this->__toString();
192
    }
193
}
194