Md5Extension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 3
c 3
b 1
f 1
lcom 0
cbo 2
dl 0
loc 20
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 5 1
A md5() 0 3 1
A getName() 0 3 1
1
<?php
2
namespace Mykees\CommentBundle\Twig\Extension;
3
4
class Md5Extension extends \Twig_Extension
5
{
6
7
8
	public function getFilters() {
9
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('md5' => ne..._Method($this, 'md5')); (array<string,Twig_Filter_Method>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFilters 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...
10
			'md5' => new \Twig_Filter_Method($this, 'md5')
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...
11
		);
12
	}
13
14
15
	public function md5 ( $val ){
16
		return md5($val);
17
	}
18
19
	public function getName() {
20
		return 'md5';
21
	}
22
23
}
24