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

Resolve::generate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 7
nc 3
nop 1
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->resolve($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 resolve($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 120
        return $this->resolvePath($payload)->withFragment($payload->getFragment());
83
    }
84
85
    /**
86
     * Resolve the URI for a Authority Less  payload URI
87
     *
88
     * @param Uri|UriInterface $payload
89
     *
90
     * @return Uri|UriInterface
91
     */
92 120
    protected function resolvePath($payload)
93
    {
94 120
        $path = $payload->getPath();
95 120
        $query = $payload->getQuery();
96 120
        if ('' === $path) {
97 6
            if ('' === $query) {
98 3
                $query = $this->uri->getQuery();
99 2
            }
100
101 6
            return $this->uri->withQuery($query);
102
        }
103
104 114
        if (0 === strpos($path, '/')) {
105 9
            return $this->uri->withPath($path)->withQuery($query);
106
        }
107
108 105
        return $this->uri
109 105
            ->withPath($this->mergePath($path)->__toString())
110 105
            ->withQuery($query);
111
    }
112
113
    /**
114
     * Merging Relative URI path with Base URI path
115
     *
116
     * @param string $path
117
     *
118
     * @return PathInterface
119
     */
120 105
    protected function mergePath($path)
121
    {
122 105
        $referencePath = $this->uri->getPath();
123 105
        if ('' !== $this->uri->getAuthority() && '' === $referencePath) {
124 3
            return (new Path($path))->withLeadingSlash();
125
        }
126
127 102
        if ('' !== $referencePath) {
128 102
            $segments = explode('/', $referencePath);
129 102
            array_pop($segments);
130 102
            $path = implode('/', $segments).'/'.$path;
131 68
        }
132
133 102
        return new Path($path);
134
    }
135
}
136