Completed
Push — master ( be4b54...4b1069 )
by ignace nyamagana
04:15
created

UserInfo::getContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
crap 3
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
20
/**
21
 * Value object representing the UserInfo part of an URI.
22
 *
23
 * @package League.uri
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 * @since   4.0.0
26
 *
27
 */
28
class UserInfo implements UserInfoInterface
29
{
30
    use ImmutablePropertyTrait;
31
32
    /**
33
     * User Component
34
     *
35
     * @var User
36
     */
37
    protected $user;
38
39
    /**
40
     * Pass Component
41
     *
42
     * @var Pass
43
     */
44
    protected $pass;
45
46
    /**
47
     * Create a new instance of UserInfo
48
     *
49
     * @param UserInterface|string $user
50
     * @param PassInterface|string $pass
51
     */
52 655
    public function __construct($user = null, $pass = null)
53
    {
54 655
        $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...
55 655
        $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...
56 655
        $this->assertValidObject();
57 655
    }
58
59
    /**
60
     * Retrieve the user component of the URI User Info part
61
     *
62
     * @return string
63
     */
64 27
    public function getUser()
65
    {
66 27
        return $this->user->__toString();
67
    }
68
69
    /**
70
     * Retrieve the pass component of the URI User Info part
71
     *
72
     * @return string
73
     */
74 24
    public function getPass()
75
    {
76 24
        return $this->pass->__toString();
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 2
    public function __debugInfo()
83
    {
84 2
        return ['userInfo' => $this->__toString()];
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 12
    public static function __set_state(array $properties)
91
    {
92 12
        return new static($properties['user'], $properties['pass']);
93
    }
94
95
    /**
96
     * Returns the component literal value.
97
     *
98
     * @return null|string
99
     */
100 649
    public function getContent()
101
    {
102 649
        $userInfo = $this->user->__toString();
103 649
        if ('' === $userInfo) {
104 555
            return $this->user->getContent();
105
        }
106
107 148
        $pass = $this->pass->__toString();
108 148
        if ('' !== $pass) {
109 145
            $userInfo .= UserInfoInterface::SEPARATOR.$pass;
110 96
        }
111
112 148
        return $userInfo;
113
    }
114
115
    /**
116
     * Returns the instance string representation; If the
117
     * instance is not defined an empty string is returned
118
     *
119
     * @return string
120
     */
121 649
    public function __toString()
122
    {
123 649
        return (string) $this->getContent();
124
    }
125
126
    /**
127
     * Returns the instance string representation
128
     * with its optional URI delimiters
129
     *
130
     * @return string
131
     */
132 629
    public function getUriComponent()
133
    {
134 629
        $component = $this->__toString();
135 629
        if ('' !== $component) {
136 140
            $component .= UserInfoInterface::DELIMITER;
137 93
        }
138
139 629
        return $component;
140
    }
141
142
    /**
143
     * Returns whether two UriPart objects represent the same value
144
     * The comparison is based on the getUriComponent method
145
     *
146
     * @param UriPart $component
147
     *
148
     * @return bool
149
     */
150 12
    public function sameValueAs(UriPart $component)
151
    {
152 12
        return $this->getUriComponent() === $component->getUriComponent();
153
    }
154
155
    /**
156
     * Return an instance with the specified user.
157
     *
158
     * This method MUST retain the state of the current instance, and return
159
     * an instance that contains the specified user.
160
     *
161
     * An empty user is equivalent to removing the user information.
162
     *
163
     * @param string $user The user to use with the new instance.
164
     *
165
     * @throws \InvalidArgumentException for invalid user.
166
     *
167
     * @return static
168
     */
169 9
    public function withUser($user)
170
    {
171 9
        return $this->withProperty('user', $user);
172
    }
173
174
    /**
175
     * Return an instance with the specified password.
176
     *
177
     * This method MUST retain the state of the current instance, and return
178
     * an instance that contains the specified password.
179
     *
180
     * An empty password is equivalent to removing the password.
181
     *
182
     * @param string $pass The password to use with the new instance.
183
     *
184
     * @throws \InvalidArgumentException for invalid password.
185
     *
186
     * @return static
187
     */
188 9
    public function withPass($pass)
189
    {
190 9
        return $this->withProperty('pass', $pass);
191
    }
192
193
    /**
194
     * @inheritdoc
195
     */
196 655
    protected function assertValidObject()
197
    {
198 655
    }
199
}
200