Completed
Push — master ( 68c2b9...8b636a )
by Alex
02:24
created

RelativePathGenerator::__invoke()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 10
nop 1
1
<?php
2
3
namespace AlexMasterov\TwigExtension;
4
5
use Psr\Http\Message\UriInterface;
6
7
final class RelativePathGenerator
8
{
9
    /**
10
     * @param UriInterface $uri
11
     *
12
     * @return string
13
     */
14
    public function __invoke(UriInterface $uri)
15
    {
16
        $basePath = $uri->getPath();
17
        if ($path === $basePath) {
0 ignored issues
show
Bug introduced by
The variable $path seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
18
            return '';
19
        }
20
21
        $baseParts = explode('/', $basePath, -1);
22
        $pathParts = explode('/', $path);
0 ignored issues
show
Bug introduced by
The variable $path seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
23
24
        foreach ($baseParts as $i => $segment) {
25
            if (isset($pathParts[$i]) && $segment === $pathParts[$i]) {
26
                unset($baseParts[$i], $pathParts[$i]);
27
            } else {
28
                break;
29
            }
30
        }
31
32
        $path = str_repeat('../', count($baseParts)) . implode('/', $pathParts);
33
34
        if (empty($path)) {
35
            return './';
36
        }
37
38
        if (empty($baseParts) && false !== strpos(current($pathParts), ':')) {
39
            $path = "./{$path}";
40
        }
41
42
        return $path;
43
    }
44
}
45