Completed
Push — master ( 4b1069...208aee )
by ignace nyamagana
08:28
created

Resolve::resolveDomain()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 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 132
    public function __construct($uri)
37
    {
38 132
        $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 132
    }
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 132
    public function __invoke($payload)
49
    {
50 132
        $this->assertUriObject($payload);
51 132
        $uri = $this->resolveUri($payload);
52
53 132
        $path = (new Path($uri->getPath()))->withoutDotSegments();
54 132
        if ('' !== $uri->getAuthority() && '' !== $path->__toString()) {
55 111
            $path = $path->withLeadingSlash();
56 74
        }
57
58 132
        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 132
    protected function resolveUri($payload)
69
    {
70 132
        if ('' === (string) $payload) {
71 3
            return $this->uri;
72
        }
73
74 129
        if ('' !== $payload->getScheme()) {
75 6
            return $payload;
76
        }
77
78 123
        if ('' !== $payload->getAuthority()) {
79 3
            return $payload->withScheme($this->uri->getScheme());
80
        }
81
82 80
        return $this
83 120
            ->resolveDomain($payload->getPath(), $payload->getQuery())
84 120
            ->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 120
    protected function resolveDomain($path, $query)
96
    {
97 120
        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 114
        if (0 === strpos($path, '/')) {
106 9
            return $this->uri->withPath($path)->withQuery($query);
107
        }
108
109 105
        return $this->uri
110 105
            ->withPath($this->mergePath($path)->__toString())
111 105
            ->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 105
    protected function mergePath($path)
122
    {
123 105
        $basePath = $this->uri->getPath();
124 105
        if ('' !== $this->uri->getAuthority() && '' === $basePath) {
125 3
            return (new Path($path))->withLeadingSlash();
126
        }
127
128 102
        if ('' !== $basePath) {
129 102
            $segments = explode('/', $basePath);
130 102
            array_pop($segments);
131 102
            $path = implode('/', $segments).'/'.$path;
132 68
        }
133
134 102
        return new Path($path);
135
    }
136
}
137