Completed
Push — master ( 5a1f4e...aa9408 )
by ignace nyamagana
12:29
created

UserInfo::withContent()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
nc 4
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 5
rs 8.7624
c 1
b 0
f 0
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Components;
13
14
use InvalidArgumentException;
15
use League\Uri\Interfaces\Pass as PassInterface;
16
use League\Uri\Interfaces\UriPart;
17
use League\Uri\Interfaces\User as UserInterface;
18
use League\Uri\Interfaces\UserInfo as UserInfoInterface;
19
use League\Uri\Types\ImmutablePropertyTrait;
20
use League\Uri\Types\ValidatorTrait;
21
22
/**
23
 * Value object representing the UserInfo part of an URI.
24
 *
25
 * @package League.uri
26
 * @author  Ignace Nyamagana Butera <[email protected]>
27
 * @since   4.0.0
28
 *
29
 */
30
class UserInfo implements UserInfoInterface
31
{
32
    use ImmutablePropertyTrait;
33
    use ValidatorTrait;
34
35
    /**
36
     * User Component
37
     *
38
     * @var User
39
     */
40
    protected $user;
41
42
    /**
43
     * Pass Component
44
     *
45
     * @var Pass
46
     */
47
    protected $pass;
48
49
    /**
50
     * Create a new instance of UserInfo
51
     *
52
     * @param UserInterface|string $user
53
     * @param PassInterface|string $pass
54
     */
55 817
    public function __construct($user = null, $pass = null)
56
    {
57 817
        $this->user = !$user instanceof UserInterface ? new User($user) : $user;
0 ignored issues
show
Documentation Bug introduced by
!$user instanceof \Leagu...nts\User($user) : $user is of type object<League\Uri\Interfaces\User>, but the property $user was declared to be of type object<League\Uri\Components\User>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
58 817
        $this->pass = !$pass instanceof PassInterface ? new Pass($pass) : $pass;
0 ignored issues
show
Documentation Bug introduced by
!$pass instanceof \Leagu...nts\Pass($pass) : $pass is of type object<League\Uri\Interfaces\Pass>, but the property $pass was declared to be of type object<League\Uri\Components\Pass>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
59 817
        $this->assertValidObject();
60 817
    }
61
62
    /**
63
     * Retrieve the user component of the URI User Info part
64
     *
65
     * @return string
66
     */
67 276
    public function getUser()
68
    {
69 276
        return $this->user->__toString();
70
    }
71
72
    /**
73
     * Retrieve the pass component of the URI User Info part
74
     *
75
     * @return string
76
     */
77 270
    public function getPass()
78
    {
79 270
        return $this->pass->__toString();
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 2
    public function __debugInfo()
86
    {
87 2
        return ['userInfo' => $this->getContent()];
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 12
    public static function __set_state(array $properties)
94
    {
95 12
        return new static($properties['user'], $properties['pass']);
96
    }
97
98
    /**
99
     * Returns the component literal value.
100
     *
101
     * @return null|string
102
     */
103 829
    public function getContent()
104
    {
105 829
        $userInfo = $this->user->__toString();
106 829
        if ('' === $userInfo) {
107 735
            return $this->user->getContent();
108
        }
109
110 154
        $pass = $this->pass->__toString();
111 154
        if ('' !== $pass) {
112 145
            $userInfo .= UserInfoInterface::SEPARATOR.$pass;
113 96
        }
114
115 154
        return $userInfo;
116
    }
117
118
    /**
119
     * Returns the instance string representation; If the
120
     * instance is not defined an empty string is returned
121
     *
122
     * @return string
123
     */
124 823
    public function __toString()
125
    {
126 823
        return (string) $this->getContent();
127
    }
128
129
    /**
130
     * Returns the instance string representation
131
     * with its optional URI delimiters
132
     *
133
     * @return string
134
     */
135 788
    public function getUriComponent()
136
    {
137 788
        $component = $this->__toString();
138 788
        if ('' !== $component) {
139 134
            $component .= UserInfoInterface::DELIMITER;
140 89
        }
141
142 788
        return $component;
143
    }
144
145
    /**
146
     * DEPRECATION WARNING! This method will be removed in the next major point release
147
     *
148
     * @deprecated deprecated since version 4.2
149
     *
150
     * Returns whether two UriPart objects represent the same value
151
     * The comparison is based on the getUriComponent method
152
     *
153
     * @param UriPart $component
154
     *
155
     * @return bool
156
     */
157
    public function sameValueAs(UriPart $component)
158
    {
159
        return $this->getUriComponent() === $component->getUriComponent();
160
    }
161
162
    /**
163
     * Return an instance with the specified user.
164
     *
165
     * This method MUST retain the state of the current instance, and return
166
     * an instance that contains the specified user.
167
     *
168
     * An empty user is equivalent to removing the user information.
169
     *
170
     * @param string $user The user to use with the new instance.
171
     *
172
     * @throws \InvalidArgumentException for invalid user.
173
     *
174
     * @return static
175
     */
176 243
    public function withUser($user)
177
    {
178 243
        return $this->withProperty('user', $user);
179
    }
180
181
    /**
182
     * Return an instance with the specified password.
183
     *
184
     * This method MUST retain the state of the current instance, and return
185
     * an instance that contains the specified password.
186
     *
187
     * An empty password is equivalent to removing the password.
188
     *
189
     * @param string $pass The password to use with the new instance.
190
     *
191
     * @throws \InvalidArgumentException for invalid password.
192
     *
193
     * @return static
194
     */
195 243
    public function withPass($pass)
196
    {
197 243
        return $this->withProperty('pass', $pass);
198
    }
199
200
    /**
201
     * Create a new instance from a string
202
     *
203
     * @param string $str
0 ignored issues
show
Bug introduced by
There is no parameter named $str. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
204
     *
205
     * @return static
206
     */
207 21
    public function withContent($content = null)
208
    {
209 21
        if ($content === $this->getContent()) {
210 3
            return $this;
211
        }
212
213 21
        if (null !== $content && !is_string($content)) {
214 3
            throw new InvalidArgumentException(sprintf(
215 3
                'Expected data to be a string or NULL; received "%s"',
216 2
                gettype($content)
217 2
            ));
218
        }
219
220 18
        $res = explode(UserInfoInterface::SEPARATOR, $content, 2);
221 18
        $new = new static(array_shift($res), array_shift($res));
222 18
        if ($new->getContent() === $this->getContent()) {
223 3
            return $this;
224
        }
225
226 15
        return $new;
227
    }
228
229
    /**
230
     * @inheritdoc
231
     */
232 817
    protected function assertValidObject()
233
    {
234 817
    }
235
}
236