Xhgui_Twig_Extension   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 123
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getFunctions() 0 10 1
A getFilters() 0 11 1
A _getBase() 0 8 2
A truncate() 0 7 2
A url() 0 8 2
A staticUrl() 0 11 2
A formatBytes() 0 4 1
A formatTime() 0 4 1
A formatDiff() 0 13 3
A makePercent() 0 5 2
A formatPercent() 0 4 1
1
<?php
2
3
class Xhgui_Twig_Extension extends Twig_Extension
4
{
5
    protected $_app;
6
7
    public function __construct($app)
8
    {
9
        $this->_app = $app;
10
    }
11
12
    public function getName()
13
    {
14
        return 'xhgui';
15
    }
16
17
    public function getFunctions()
18
    {
19
        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...
20
            '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...
21
            '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...
22
            '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...
23
                'is_safe' => array('html')
24
            )),
25
        );
26
    }
27
28
    public function getFilters()
29
    {
30
        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...
31
            '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...
32
            '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...
33
            '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...
34
            '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...
35
            '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...
36
            '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...
37
        );
38
    }
39
40
    protected function _getBase()
41
    {
42
        $base = dirname($_SERVER['PHP_SELF']);
43
        if ($base == '/') {
44
            return '';
45
        }
46
        return $base;
47
    }
48
49
    public function truncate($input, $length = 50)
50
    {
51
        if (strlen($input) < $length) {
52
            return $input;
53
        }
54
        return substr($input, 0, $length) . "\xe2\x80\xa6";
55
    }
56
57
    /**
58
     * Get a URL for xhgui.
59
     *
60
     * @param string $path The file/path you want a link to
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
     * @param array $queryarg Additional querystring arguments.
0 ignored issues
show
Documentation introduced by
There is no parameter named $queryarg. Did you maybe mean $queryargs?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
62
     * @return string url.
63
     */
64
    public function url($name, $queryargs = array())
65
    {
66
        $query = '';
67
        if (!empty($queryargs)) {
68
            $query = '?' . http_build_query($queryargs);
69
        }
70
        return $this->_app->urlFor($name)  . $query;
71
    }
72
73
    /**
74
     * Get the URL for static content relative to webroot
75
     *
76
     * @param string $path The file/path you want a link to
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     * @return string url.
78
     */
79
    public function staticUrl($url)
80
    {
81
        $rootUri = $this->_app->request()->getRootUri();
82
83
        // Get URL part prepending index.php
84
        $indexPos = strpos($rootUri, 'index.php');
85
        if ($indexPos > 0) {
86
            return substr($rootUri, 0, $indexPos) . $url;
87
        }
88
        return $rootUri . '/' . $url;
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 = 'diff-same';
0 ignored issues
show
Unused Code introduced by
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
        $class = $value > 0 ? 'diff-up' : 'diff-down';
105
        if ($value == 0) {
106
            $class = 'diff-same';
107
        }
108
        return sprintf(
109
            '<span class="%s">%s</span>',
110
            $class,
111
            number_format((float)$value)
112
        );
113
    }
114
115
    public function makePercent($value, $total)
116
    {
117
        $value = (false === empty($total)) ? $value / $total : 0;
118
        return $this->formatPercent($value);
119
    }
120
121
    public function formatPercent($value)
122
    {
123
        return number_format((float)$value * 100, 0) . ' <span class="units">%</span>';
124
    }
125
}
126