Completed
Push — master ( bead5b...d9133b )
by Mark
25s queued 11s
created

Xhgui_Twig_Extension::pathPrefix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
use Slim\Router;
4
use Slim\Slim;
5
6
class Xhgui_Twig_Extension extends Twig_Extension
7
{
8
    /** @var Slim */
9
    protected $_app;
10
    /** @var Router */
11
    private $router;
12
    /** @var string */
13
    private $pathPrefix;
14
15
    public function __construct(Slim $app)
16
    {
17
        $this->_app = $app;
18
        $this->router = $app->router();
19
        $this->pathPrefix = $app->config('path.prefix');
20
    }
21
22
    public function getName()
23
    {
24
        return 'xhgui';
25
    }
26
27
    public function getFunctions()
28
    {
29
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('url' => ne...e' => array('html')))); (array<string,Twig_Function_Method>) is incompatible with the return type declared by the interface Twig\Extension\ExtensionInterface::getFunctions of type Twig\TwigFunction[].

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...
30
            'url' => new Twig_Function_Method($this, 'url'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31
            'static' => new Twig_Function_Method($this, 'staticUrl'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32
            'percent' => new Twig_Function_Method($this, 'makePercent', array(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33
                'is_safe' => array('html')
34
            )),
35
        );
36
    }
37
38
    public function getFilters()
39
    {
40
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('simple_url...od($this, 'truncate')); (array<string,Twig_Filter...ion|Twig_Filter_Method>) is incompatible with the return type declared by the interface Twig\Extension\ExtensionInterface::getFilters of type Twig\TwigFilter[].

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...
41
            'simple_url' => new Twig_Filter_Function('Xhgui_Util::simpleUrl'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Function has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
42
            'as_bytes' => new Twig_Filter_Method($this, 'formatBytes', array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
43
            'as_time' => new Twig_Filter_Method($this, 'formatTime', array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
44
            'as_diff' => new Twig_Filter_Method($this, 'formatDiff', array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
45
            'as_percent' => new Twig_Filter_Method($this, 'formatPercent', array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
46
            'truncate' => new Twig_Filter_Method($this, 'truncate'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
47
        );
48
    }
49
50
    public function truncate($input, $length = 50)
51
    {
52
        if (strlen($input) < $length) {
53
            return $input;
54
        }
55
        return substr($input, 0, $length) . "\xe2\x80\xa6";
56
    }
57
58
    /**
59
     * Get a URL for xhgui.
60
     *
61
     * @param string $name The file/path you want a link to
62
     * @param array $queryargs Additional querystring arguments.
63
     * @return string url.
64
     */
65
    public function url($name, $queryargs = array())
66
    {
67
        $query = '';
68
        if (!empty($queryargs)) {
69
            $query = '?' . http_build_query($queryargs);
70
        }
71
72
        // this is copy of \Slim\Slim::urlFor() to mix path prefix in
73
        // \Slim\Slim::urlFor
74
75
        return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query;
76
    }
77
78
    /**
79
     * Get the URL for static content relative to webroot
80
     *
81
     * @param string $path The file/path you want a link to
82
     * @return string url.
83
     */
84
    public function staticUrl($path)
85
    {
86
        $rootUri = $this->pathPrefix();
87
88
        return rtrim($rootUri, '/') . '/' . $path;
89
    }
90
91
    public function formatBytes($value)
92
    {
93
        return number_format((float)$value) . '&nbsp;<span class="units">bytes</span>';
94
    }
95
96
    public function formatTime($value)
97
    {
98
        return number_format((float)$value) . '&nbsp;<span class="units">µs</span>';
99
    }
100
101
    public function formatDiff($value)
102
    {
103
        $class = $value > 0 ? 'diff-up' : 'diff-down';
104
        if ($value == 0) {
105
            $class = 'diff-same';
106
        }
107
        return sprintf(
108
            '<span class="%s">%s</span>',
109
            $class,
110
            number_format((float)$value)
111
        );
112
    }
113
114
    public function makePercent($value, $total)
115
    {
116
        $value = (false === empty($total)) ? $value / $total : 0;
117
        return $this->formatPercent($value);
118
    }
119
120
    public function formatPercent($value)
121
    {
122
        return number_format((float)$value * 100, 0) . ' <span class="units">%</span>';
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    private function pathPrefix()
129
    {
130
        if ($this->pathPrefix !== null) {
131
            return $this->pathPrefix;
132
        }
133
134
        $request = $this->_app->request();
135
        $rootUri = $request->getRootUri();
136
137
        // Get URL part prepending index.php
138
        $indexPos = strpos($rootUri, 'index.php');
139
        if ($indexPos > 0) {
140
            return substr($rootUri, 0, $indexPos);
141
        }
142
143
        return $rootUri;
144
    }
145
}
146