Completed
Push — master ( 85473c...1fb953 )
by ignace nyamagana
04:10
created

Resolve   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 16
c 8
b 2
f 0
lcom 1
cbo 4
dl 0
loc 110
ccs 42
cts 42
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 12 3
A resolveUri() 0 18 4
A mergePath() 0 15 4
A resolvePathAndQuery() 0 18 4
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\Modifiers;
13
14
use League\Uri\Components\Path;
15
use League\Uri\Interfaces\Uri;
16
use League\Uri\Modifiers\Filters\Uri as UriFilter;
17
use Psr\Http\Message\UriInterface;
18
19
/**
20
 * Resolve an URI according to a base URI using
21
 * RFC3986 rules
22
 *
23
 * @package League.uri
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 * @since   4.0.0
26
 */
27
class Resolve extends AbstractUriModifier
28
{
29
    use UriFilter;
30
31
    /**
32
     * New instance
33
     *
34
     * @param Uri|UriInterface $uri
35
     */
36 150
    public function __construct($uri)
37
    {
38 150
        $this->uri = $this->filterUri($uri);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->filterUri($uri) can also be of type object<League\Uri\Interfaces\Uri>. However, the property $uri is declared as type object<Psr\Http\Message\UriInterface>. 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...
39 150
    }
40
41
    /**
42
     * Return a Uri object modified according to the modifier
43
     *
44
     * @param Uri|UriInterface $payload
45
     *
46
     * @return Uri|UriInterface
47
     */
48 150
    public function __invoke($payload)
49
    {
50 150
        $this->assertUriObject($payload);
51 150
        $uri = $this->resolveUri($payload);
52
53 150
        $path = (new Path($uri->getPath()))->withoutDotSegments();
54 150
        if ('' !== $uri->getAuthority() && '' !== $path->__toString()) {
55 129
            $path = $path->withLeadingSlash();
56 86
        }
57
58 150
        return $uri->withPath((string) $path);
59
    }
60
61
    /**
62
     * Resolve the payload URI
63
     *
64
     * @param Uri|UriInterface $payload
65
     *
66
     * @return Uri|UriInterface
67
     */
68 150
    protected function resolveUri($payload)
69
    {
70 150
        if ('' === (string) $payload) {
71 3
            return $this->uri;
72
        }
73
74 147
        if ('' !== $payload->getScheme()) {
75 6
            return $payload;
76
        }
77
78 141
        if ('' !== $payload->getAuthority()) {
79 3
            return $payload->withScheme($this->uri->getScheme());
80
        }
81
82 92
        return $this
83 138
            ->resolvePathAndQuery($payload->getPath(), $payload->getQuery())
84 138
            ->withFragment($payload->getFragment());
85
    }
86
87
    /**
88
     * Resolve the URI for a Authority-less payload URI
89
     *
90
     * @param string $path
91
     * @param string $query
92
     *
93
     * @return Uri|UriInterface
94
     */
95 138
    protected function resolvePathAndQuery($path, $query)
96
    {
97 138
        if ('' === $path) {
98 6
            if ('' === $query) {
99 3
                $query = $this->uri->getQuery();
100 2
            }
101
102 6
            return $this->uri->withQuery($query);
103
        }
104
105 132
        if (0 === strpos($path, '/')) {
106 9
            return $this->uri->withPath($path)->withQuery($query);
107
        }
108
109 123
        return $this->uri
110 123
            ->withPath($this->mergePath($path)->__toString())
111 123
            ->withQuery($query);
112
    }
113
114
    /**
115
     * Merging Relative URI path with Base URI path
116
     *
117
     * @param string $path
118
     *
119
     * @return PathInterface
120
     */
121 123
    protected function mergePath($path)
122
    {
123 123
        $basePath = $this->uri->getPath();
124 123
        if ('' !== $this->uri->getAuthority() && '' === $basePath) {
125 3
            return (new Path($path))->withLeadingSlash();
126
        }
127
128 120
        if ('' !== $basePath) {
129 120
            $segments = explode('/', $basePath);
130 120
            array_pop($segments);
131 120
            $path = implode('/', $segments).'/'.$path;
132 80
        }
133
134 120
        return new Path($path);
135
    }
136
}
137