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

Relativize::relativizeUri()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.4285
cc 3
eloc 13
nc 2
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\Interfaces\Uri;
15
use League\Uri\Modifiers\Filters\Uri as UriFilter;
16
use Psr\Http\Message\UriInterface;
17
18
/**
19
 * Resolve an URI according to a base URI using
20
 * RFC3986 rules
21
 *
22
 * @package League.uri
23
 * @author  Ignace Nyamagana Butera <[email protected]>
24
 * @since   4.2.0
25
 */
26
class Relativize extends AbstractUriModifier
27
{
28
    use UriFilter;
29
30
    /**
31
     * New instance
32
     *
33
     * @param Uri|UriInterface $uri
34
     */
35 102
    public function __construct($uri)
36
    {
37 102
        $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...
38 102
    }
39
40
    /**
41
     * Return a Uri object modified according to the modifier
42
     *
43
     * @param Uri|UriInterface $payload
44
     *
45
     * @return Uri|UriInterface
46
     */
47 102
    public function __invoke($payload)
48
    {
49 102
        $this->assertUriObject($payload);
50 102
        if ($this->uri->getScheme() !== $payload->getScheme()
51 102
            || $this->uri->getAuthority() !== $payload->getAuthority()
52 68
        ) {
53 33
            return $payload;
54
        }
55
56
        return $payload
57 69
            ->withScheme('')
58 69
            ->withPort(null)
59 69
            ->withUserInfo('')
60 69
            ->withHost('')
61 69
            ->withPath($this->relativizePath($payload->getPath()));
62
    }
63
64
    /**
65
     * Relative the URI for a authority-less payload URI
66
     *
67
     * @param string $path
68
     *
69
     * @return string
70
     */
71 69
    protected function relativizePath($path)
72
    {
73 69
        $targetSegments = $this->getSegments($path);
74 69
        $basename = array_pop($targetSegments);
75 69
        $basePath = $this->uri->getPath();
76 69
        if ($basePath === $path) {
77 18
            return $this->formatPath($basename, $basePath);
78
        }
79
80 51
        $baseSegments = $this->getSegments($basePath);
81 51
        array_pop($baseSegments);
82 51
        foreach ($baseSegments as $offset => $segment) {
83 48
            if (!isset($targetSegments[$offset]) || $segment !== $targetSegments[$offset]) {
84 24
                break;
85
            }
86 33
            unset($baseSegments[$offset], $targetSegments[$offset]);
87 34
        }
88 51
        $targetSegments[] = $basename;
89 51
        $path = str_repeat('../', count($baseSegments)).implode('/', $targetSegments);
90
91 51
        return $this->formatPath($path, $basePath);
92
    }
93
94
    /**
95
     * returns the path segments
96
     *
97
     * @param string $path
98
     *
99
     * @return array
100
     */
101 69
    protected function getSegments($path)
102
    {
103 69
        if ('' !== $path && '/' === $path[0]) {
104 66
            $path = mb_substr($path, 1);
105 44
        }
106
107 69
        return explode('/', $path);
108
    }
109
110
    /**
111
     * Post formatting the path to keep a valid URI
112
     *
113
     * @param string $path
114
     * @param string $basePath
115
     *
116
     * @return string
117
     */
118 69
    protected function formatPath($path, $basePath)
119
    {
120 69
        if ('' === $path) {
121 9
            return in_array($basePath, ['', '/']) ? '/' : './';
122
        }
123
124 60
        if (false === ($colonPos = strpos($path, ':'))) {
125 54
            return $path;
126
        }
127
128 6
        $slashPos = strpos($path, '/');
129 6
        if (false === $slashPos || $colonPos < $slashPos) {
130 3
            return "./$path";
131
        }
132
133 3
        return $path;
134
    }
135
}
136