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

RelativePathGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 30 8
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