Completed
Push — master ( 1fb953...bffbed )
by ignace nyamagana
03:32
created

Resolve::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 9.2
cc 3
eloc 15
nc 3
nop 1
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\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
        if ('' !== $payload->getScheme()) {
52 6
            return $payload;
53
        }
54
55 144
        if ('' !== $payload->getAuthority()) {
56 3
            return $payload->withScheme($this->uri->getScheme());
57
        }
58
59 141
        $userInfo = explode(':', $this->uri->getUserInfo(), 2);
60 141
        $components = $this->resolvePathAndQuery($payload->getPath(), $payload->getQuery());
61
62
        return $payload
63 141
            ->withPath($this->formatPath($components['path']))
64 141
            ->withQuery($components['query'])
65 141
            ->withHost($this->uri->getHost())
66 141
            ->withPort($this->uri->getPort())
67 141
            ->withUserInfo((string) array_shift($userInfo), array_shift($userInfo))
68 141
            ->withScheme($this->uri->getScheme());
69
    }
70
71
    /**
72
     * Resolve the URI for a Authority-less payload URI
73
     *
74
     * @param string $path  the payload path component
75
     * @param string $query the payload query component
76
     *
77
     * @return string[]
78
     */
79 141
    protected function resolvePathAndQuery($path, $query)
80
    {
81 141
        $components = ['path' => $path, 'query' => $query];
82
83 141
        if ('' === $components['path']) {
84 9
            $components['path'] = $this->uri->getPath();
85 9
            if ('' === $components['query']) {
86 6
                $components['query'] = $this->uri->getQuery();
87 4
            }
88
89 9
            return $components;
90
        }
91
92 132
        if (0 !== strpos($components['path'], '/')) {
93 123
            $components['path'] = $this->mergePath($components['path']);
94 82
        }
95
96 132
        return $components;
97
    }
98
99
    /**
100
     * Merging Relative URI path with Base URI path
101
     *
102
     * @param string $path
103
     *
104
     * @return PathInterface
105
     */
106 123
    protected function mergePath($path)
107
    {
108 123
        $basePath = $this->uri->getPath();
109 123
        if ('' !== $this->uri->getAuthority() && '' === $basePath) {
110 3
            return (new Path($path))->withLeadingSlash()->__toString();
111
        }
112
113 120
        if ('' !== $basePath) {
114 120
            $segments = explode('/', $basePath);
115 120
            array_pop($segments);
116 120
            $path = implode('/', $segments).'/'.$path;
117 80
        }
118
119 120
        return $path;
120
    }
121
122
    /**
123
     * Format the resolved path
124
     *
125
     * @param string $path
126
     *
127
     * @return string
128
     */
129 141
    protected function formatPath($path)
130
    {
131 141
        $path = (new Path($path))->withoutDotSegments();
132 141
        if ('' !== $this->uri->getAuthority() && '' !== $path->__toString()) {
133 126
            $path = $path->withLeadingSlash();
134 84
        }
135
136 141
        return (string) $path;
137
    }
138
}
139