Completed
Push — master ( bce6d9...3df90d )
by Vladimir
02:55
created

BaseUrlFunction   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 15.05 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 14
loc 93
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 1
A get() 0 6 1
A getUrl() 0 16 3
A getBaseUrl() 0 15 3
B guessAssetPath() 0 17 6
A buildPermalink() 14 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 allejo\stakx\Service;
11
use Twig_Environment;
12
13
class BaseUrlFunction implements StakxTwigFilter
14
{
15
    private $site;
16
17 18
    public function __invoke(Twig_Environment $env, $assetPath, $absolute = false)
18
    {
19 18
        $globals = $env->getGlobals();
20 18
        $this->site = $globals['site'];
21
22 18
        $url = $this->getUrl($absolute);
23 18
        $baseURL = $this->getBaseUrl();
24 18
        $permalink = $this->guessAssetPath($assetPath);
25
26 18
        return $this->buildPermalink($url, $baseURL, $permalink);
27
    }
28
29 6
    public static function get()
30
    {
31 6
        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...
32 6
            'needs_environment' => true,
33
        ));
34
    }
35
36 18
    private function getUrl($absolute)
37
    {
38 18
        $url = '/';
39
40 18
        if (!$absolute)
41
        {
42 16
            return $url;
43
        }
44
45 2
        if (isset($this->site['url']))
46
        {
47 2
            $url = $this->site['url'];
48
        }
49
50 2
        return ltrim($url, '/');
51
    }
52
53 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...
54
    {
55 18
        $base = '';
56
57 18
        if (isset($this->site['baseurl']))
58
        {
59 15
            $base = $this->site['baseurl'];
60
        }
61 3
        elseif (isset($this->site['base']))
62
        {
63
            $base = $this->site['base'];
64
        }
65
66 18
        return $base;
67
    }
68
69 18
    private function guessAssetPath($assetPath)
70
    {
71 18
        if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess))
72
        {
73 4
            return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/';
74
        }
75 14
        elseif (is_null($assetPath))
76
        {
77 1
            return '/';
78
        }
79 13
        elseif ($assetPath instanceof \SplFileInfo)
80
        {
81
            return str_replace(Service::getWorkingDirectory(), '', $assetPath);
82
        }
83
84 13
        return $assetPath;
85
    }
86
87
    /**
88
     * @link   https://stackoverflow.com/a/15575293
89
     * @return string
90
     */
91 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...
92
    {
93 18
        $paths = array();
94
95 18
        foreach (func_get_args() as $arg)
96
        {
97 18
            if ($arg !== '')
98
            {
99 18
                $paths[] = $arg;
100
            }
101
        }
102
103 18
        return preg_replace('#(?<!:)/+#','/', join('/', $paths));
104
    }
105
}
106