Completed
Pull Request — master (#40)
by ignace nyamagana
11:26
created

UserInfo   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 1 Features 0
Metric Value
wmc 15
c 17
b 1
f 0
lcom 2
cbo 4
dl 0
loc 154
ccs 32
cts 32
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A getUser() 0 4 1
A getPass() 0 4 1
A __toString() 0 14 3
A getUriComponent() 0 9 2
A sameValueAs() 0 4 1
A withUser() 0 4 1
A withPass() 0 4 1
A __set_state() 0 4 1
A assertValidObject() 0 3 1
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.1.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 408
    public function __construct($user = null, $pass = null)
53
    {
54 408
        $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 408
        $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 408
        $this->assertValidObject();
57 408
    }
58
59
    /**
60
     * Retrieve the user component of the URI User Info part
61
     *
62 88
     * @return string
63
     */
64 88
    public function getUser()
65
    {
66
        return $this->user->__toString();
67
    }
68
69
    /**
70 84
     * Retrieve the pass component of the URI User Info part
71
     *
72 84
     * @return string
73
     */
74
    public function getPass()
75
    {
76
        return $this->pass->__toString();
77
    }
78 384
79
    /**
80 384
     * Returns the instance string representation; If the
81 384
     * instance is not defined an empty string is returned
82 332
     *
83
     * @return string
84
     */
85 82
    public function __toString()
86 82
    {
87 80
        $userInfo = $this->user->__toString();
88 80
        if ('' === $userInfo) {
89
            return $userInfo;
90 82
        }
91
92
        $pass = $this->pass->__toString();
93
        if ('' !== $pass) {
94
            $userInfo .= UserInfoInterface::SEPARATOR.$pass;
95
        }
96 372
97
        return $userInfo;
98 372
    }
99 372
100 78
    /**
101 78
     * Returns the instance string representation
102
     * with its optional URI delimiters
103 372
     *
104
     * @return string
105
     */
106
    public function getUriComponent()
107
    {
108
        $component = $this->__toString();
109 8
        if ('' !== $component) {
110
            $component .= UserInfoInterface::DELIMITER;
111 8
        }
112
113
        return $component;
114
    }
115
116
    /**
117 76
     * Returns whether two UriPart objects represent the same value
118
     * The comparison is based on the getUriComponent method
119 76
     *
120
     * @param UriPart $component
121
     *
122
     * @return bool
123
     */
124
    public function sameValueAs(UriPart $component)
125 76
    {
126
        return $this->getUriComponent() === $component->getUriComponent();
127 76
    }
128
129
    /**
130
     * Return an instance with the specified user.
131
     *
132
     * This method MUST retain the state of the current instance, and return
133 408
     * an instance that contains the specified user.
134
     *
135 408
     * An empty user is equivalent to removing the user information.
136
     *
137
     * @param string $user The user to use with the new instance.
138
     *
139
     * @throws \InvalidArgumentException for invalid user.
140
     *
141
     * @return static
142
     */
143
    public function withUser($user)
144
    {
145
        return $this->withProperty('user', $user);
146
    }
147
148
    /**
149
     * Return an instance with the specified password.
150
     *
151
     * This method MUST retain the state of the current instance, and return
152
     * an instance that contains the specified password.
153
     *
154
     * An empty password is equivalent to removing the password.
155
     *
156
     * @param string $pass The password to use with the new instance.
157
     *
158
     * @throws \InvalidArgumentException for invalid password.
159
     *
160
     * @return static
161
     */
162
    public function withPass($pass)
163
    {
164
        return $this->withProperty('pass', $pass);
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170
    public static function __set_state(array $properties)
171
    {
172
        return new static($properties['user'], $properties['pass']);
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    protected function assertValidObject()
179
    {
180
    }
181
}
182