Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

BaseUrlFunction::getBaseUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3.072
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 1
    public static function get()
29
    {
30 1
        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 1
            'needs_environment' => true,
32 1
        ));
33
    }
34
35 18
    private function getUrl($absolute)
36
    {
37 18
        $url = '/';
38
39 18
        if (!$absolute)
40 18
        {
41 16
            return $url;
42
        }
43
44 2
        if (isset($this->site['url']))
45 2
        {
46 2
            $url = $this->site['url'];
47 2
        }
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 18
        {
58 15
            $base = $this->site['baseurl'];
59 15
        }
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 18
        {
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 View Code Duplication
    private function buildPermalink()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88 18
        $paths = array();
89
90 18
        foreach (func_get_args() as $arg)
91
        {
92 18
            if ($arg !== '')
93 18
            {
94 18
                $paths[] = $arg;
95 18
            }
96 18
        }
97
98 18
        return preg_replace('#(?<!:)/+#','/', join('/', $paths));
99
    }
100
}
101