Completed
Pull Request — master (#64)
by ignace nyamagana
03:25
created

Relativize::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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.2.0
26
 */
27
class Relativize extends AbstractUriModifier
28
{
29
    use UriFilter;
30
31
    /**
32
     * New instance
33
     *
34
     * @param Uri|UriInterface $uri
35
     */
36 93
    public function __construct($uri)
37
    {
38 93
        $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 93
    }
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 93
    public function __invoke($payload)
49
    {
50 93
        $this->assertUriObject($payload);
51
52 93
        return $this->relativizeUri($payload);
53
    }
54
55
    /**
56
     * Resolve the payload URI
57
     *
58
     * @param Uri|UriInterface $payload
59
     *
60
     * @return Uri|UriInterface
61
     */
62 93
    protected function relativizeUri($payload)
63
    {
64 93
        if ($this->uri->getScheme() !== $payload->getScheme()
65 93
            || $this->uri->getAuthority() !== $payload->getAuthority()
66 62
        ) {
67 33
            return $payload;
68
        }
69
70 60
        $path = $this->relativizePath($payload->getPath());
71
72 60
        return $this->uri
73 60
            ->withScheme('')
74 60
            ->withPort(null)
75 60
            ->withUserInfo('')
76 60
            ->withHost('')
77 60
            ->withPath($path)
78 60
            ->withQuery($payload->getQuery())
79 60
            ->withFragment($payload->getFragment());
80
    }
81
82
    /**
83
     * Relative the URI for a authority-less payload URI
84
     *
85
     * @param string $targetPath
86
     *
87
     * @return string
88
     */
89 60
    protected function relativizePath($targetPath)
90
    {
91 60
        $targetSegments = $this->getSegments($targetPath);
92 60
        $basename = array_pop($targetSegments);
93 60
        $basePath = $this->uri->getPath();
94 60
        if ($basePath === $targetPath) {
95 15
            return $this->formatPath($basename);
96
        }
97
98 45
        $baseSegments = $this->getSegments($basePath);
99 45
        array_pop($baseSegments);
100
101 45
        foreach ($baseSegments as $offset => $segment) {
102 45
            if (!isset($targetSegments[$offset]) || $segment !== $targetSegments[$offset]) {
103 24
                break;
104
            }
105 30
            unset($baseSegments[$offset], $targetSegments[$offset]);
106 30
        }
107 45
        $targetSegments[] = $basename;
108 45
        $path = str_repeat('../', count($baseSegments)).implode('/', $targetSegments);
109
110 45
        return $this->formatPath($path);
111
    }
112
113
    /**
114
     * returns the path segments
115
     *
116
     * @param string $path
117
     *
118
     * @return array
119
     */
120 60
    protected function getSegments($path)
121
    {
122 60
        if ('' !== $path && '/' === $path[0]) {
123 60
            $path = mb_substr($path, 1);
124 40
        }
125
126 60
        return explode('/', $path);
127
    }
128
129
    /**
130
     * Post formatting the path to keep a valid URI
131
     *
132
     * @param string $path
133
     *
134
     * @return string
135
     */
136 60
    protected function formatPath($path)
137
    {
138 60
        if ('' === $path) {
139 3
            return './';
140
        }
141
142 57
        if (false === ($colonPos = strpos($path, ':'))) {
143 54
            return $path;
144
        }
145
146 3
        $slashPos = strpos($path, '/');
147 3
        if (false === $slashPos || $colonPos < $slashPos) {
148 3
            return "./$path";
149
        }
150
151
        return $path;
152
    }
153
}
154