Completed
Push — master ( 89bfcf...4ab803 )
by Vladimir
02:41
created

BaseUrlFunction::buildPermalink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
ccs 6
cts 6
cp 1
cc 3
eloc 6
nc 3
nop 0
crap 3
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Twig;
9
10
use Twig_Environment;
11
12
class BaseUrlFunction implements StakxTwigFilter
13
{
14
    private $site;
15
16 18
    public function __invoke(Twig_Environment $env, $assetPath, $absolute = false)
17
    {
18 18
        $globals = $env->getGlobals();
19 18
        $this->site = $globals['site'];
20
21 18
        $url = $this->getUrl($absolute);
22 18
        $baseURL = $this->getBaseUrl();
23 18
        $permalink = $this->guessAssetPath($assetPath);
24
25 18
        return $this->buildPermalink($url, $baseURL, $permalink);
26
    }
27
28 2
    public static function get()
29
    {
30 2
        return new \Twig_SimpleFunction('url', new self(), array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Twig_SimpleF...environment' => true)); (Twig_SimpleFunction) is incompatible with the return type declared by the interface allejo\stakx\Twig\StakxTwigFilter::get of type Twig_SimpleFilter.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
31 2
            'needs_environment' => true,
32
        ));
33
    }
34
35 18
    private function getUrl($absolute)
36
    {
37 18
        $url = '/';
38
39 18
        if (!$absolute)
40
        {
41 16
            return $url;
42
        }
43
44 2
        if (isset($this->site['url']))
45
        {
46 2
            $url = $this->site['url'];
47
        }
48
49 2
        return ltrim($url, '/');
50
    }
51
52 18
    private function getBaseUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
    {
54 18
        $base = '';
55
56 18
        if (isset($this->site['baseurl']))
57
        {
58 15
            $base = $this->site['baseurl'];
59
        }
60 3
        elseif (isset($this->site['base']))
61
        {
62
            $base = $this->site['base'];
63
        }
64
65 18
        return $base;
66
    }
67
68 18
    private function guessAssetPath($assetPath)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
69
    {
70 18
        if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess))
71
        {
72 4
            return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/';
73
        }
74 14
        elseif (is_null($assetPath))
75
        {
76 1
            return '/';
77
        }
78
79 13
        return $assetPath;
80
    }
81
82
    /**
83
     * @link   https://stackoverflow.com/a/15575293
84
     * @return string
85
     */
86 18
    private function buildPermalink()
87
    {
88 18
        $paths = array();
89
90 18
        foreach (func_get_args() as $arg)
91
        {
92 18
            if ($arg !== '')
93
            {
94 18
                $paths[] = $arg;
95
            }
96
        }
97
98 18
        return preg_replace('#(?<!:)/+#','/', join('/', $paths));
99
    }
100
}
101