Completed
Push — master ( b0bce0...c1c6ff )
by ignace nyamagana
04:32
created

src/Components/UserInfo.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 League\Uri\Interfaces\Pass as PassInterface;
15
use League\Uri\Interfaces\UriPart;
16
use League\Uri\Interfaces\User as UserInterface;
17
use League\Uri\Interfaces\UserInfo as UserInfoInterface;
18
use League\Uri\Types\ImmutablePropertyTrait;
19
use League\Uri\Types\ValidatorTrait;
20
21
/**
22
 * Value object representing the UserInfo part of an URI.
23
 *
24
 * @package League.uri
25
 * @author  Ignace Nyamagana Butera <[email protected]>
26
 * @since   4.0.0
27
 *
28
 */
29
class UserInfo implements UserInfoInterface
30
{
31
    use ImmutablePropertyTrait;
32
    use ValidatorTrait;
33
34
    /**
35
     * User Component
36
     *
37
     * @var User
38
     */
39
    protected $user;
40
41
    /**
42
     * Pass Component
43
     *
44
     * @var Pass
45
     */
46
    protected $pass;
47
48
    /**
49
     * Create a new instance from a string
50
     *
51
     * @param string $str
52
     *
53
     * @return static
54
     */
55 15
    public static function createFromString($str)
56
    {
57 15
        $res = explode(UserInfoInterface::SEPARATOR, self::validateString($str), 2);
58
59 15
        return new static(
60 15
            new User(array_shift($res)),
61 15
            new Pass(array_shift($res))
62 10
        );
63
    }
64
65
    /**
66
     * Create a new instance of UserInfo
67
     *
68
     * @param UserInterface|string $user
69
     * @param PassInterface|string $pass
70
     */
71 766
    public function __construct($user = null, $pass = null)
72
    {
73 766
        $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...
74 766
        $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...
75 766
        $this->assertValidObject();
76 766
    }
77
78
    /**
79
     * Retrieve the user component of the URI User Info part
80
     *
81
     * @return string
82
     */
83 246
    public function getUser()
84
    {
85 246
        return $this->user->__toString();
86
    }
87
88
    /**
89
     * Retrieve the pass component of the URI User Info part
90
     *
91
     * @return string
92
     */
93 240
    public function getPass()
94
    {
95 240
        return $this->pass->__toString();
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 2
    public function __debugInfo()
102
    {
103 2
        return ['userInfo' => $this->getContent()];
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 12
    public static function __set_state(array $properties)
110
    {
111 12
        return new static($properties['user'], $properties['pass']);
112
    }
113
114
    /**
115
     * Returns the component literal value.
116
     *
117
     * @return null|string
118
     */
119 760
    public function getContent()
120
    {
121 760
        $userInfo = $this->user->__toString();
122 760
        if ('' === $userInfo) {
123 657
            return $this->user->getContent();
124
        }
125
126 151
        $pass = $this->pass->__toString();
127 151
        if ('' !== $pass) {
128 142
            $userInfo .= UserInfoInterface::SEPARATOR.$pass;
129 94
        }
130
131 151
        return $userInfo;
132
    }
133
134
    /**
135
     * Returns the instance string representation; If the
136
     * instance is not defined an empty string is returned
137
     *
138
     * @return string
139
     */
140 760
    public function __toString()
141
    {
142 760
        return (string) $this->getContent();
143
    }
144
145
    /**
146
     * Returns the instance string representation
147
     * with its optional URI delimiters
148
     *
149
     * @return string
150
     */
151 725
    public function getUriComponent()
152
    {
153 725
        $component = $this->__toString();
154 725
        if ('' !== $component) {
155 134
            $component .= UserInfoInterface::DELIMITER;
156 89
        }
157
158 725
        return $component;
159
    }
160
161
    /**
162
     * DEPRECATION WARNING! This method will be removed in the next major point release
163
     *
164
     * @deprecated deprecated since version 4.2
165
     *
166
     * Returns whether two UriPart objects represent the same value
167
     * The comparison is based on the getUriComponent method
168
     *
169
     * @param UriPart $component
170
     *
171
     * @return bool
172
     */
173
    public function sameValueAs(UriPart $component)
174
    {
175
        return $this->getUriComponent() === $component->getUriComponent();
176
    }
177
178
    /**
179
     * Return an instance with the specified user.
180
     *
181
     * This method MUST retain the state of the current instance, and return
182
     * an instance that contains the specified user.
183
     *
184
     * An empty user is equivalent to removing the user information.
185
     *
186
     * @param string $user The user to use with the new instance.
187
     *
188
     * @throws \InvalidArgumentException for invalid user.
189
     *
190
     * @return static
191
     */
192 213
    public function withUser($user)
193
    {
194 213
        return $this->withProperty('user', $user);
195
    }
196
197
    /**
198
     * Return an instance with the specified password.
199
     *
200
     * This method MUST retain the state of the current instance, and return
201
     * an instance that contains the specified password.
202
     *
203
     * An empty password is equivalent to removing the password.
204
     *
205
     * @param string $pass The password to use with the new instance.
206
     *
207
     * @throws \InvalidArgumentException for invalid password.
208
     *
209
     * @return static
210
     */
211 213
    public function withPass($pass)
212
    {
213 213
        return $this->withProperty('pass', $pass);
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219 766
    protected function assertValidObject()
220
    {
221 766
    }
222
}
223