CommentExtension::commentForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 4 Features 1
Metric Value
c 5
b 4
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Rafidion Michael
5
 * Date: 03/09/2015
6
 * Time: 12:18
7
 */
8
9
namespace Mykees\CommentBundle\Twig\Extension;
10
11
12
use Symfony\Component\Validator\Constraints\DateTime;
13
14
class CommentExtension extends \Twig_Extension{
15
16
	/**
17
	 * Returns a list of functions to add to the existing list.
18
	 *
19
	 * @return array An array of functions
20
	 */
21
	public function getFunctions()
22
	{
23
		return array(
24
			new \Twig_SimpleFunction('helper_comment_form', [$this, 'commentForm'], [
25
				'is_safe'=>array('html'),
26
				'needs_environment'=>true
27
			]),
28
			new \Twig_SimpleFunction('helper_comments_list', [$this, 'commentsList'], [
29
				'is_safe'=>['html'],
30
				'needs_environment'=>true
31
			]),
32
			new \Twig_SimpleFunction('date_interval', [$this, 'dateInterval'], [
33
				'is_safe'=>array('html')
34
			]),
35
		);
36
	}
37
38
	public function getFilters() {
39
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('url_decode...d($this, 'urlDecode')); (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...
40
			'url_decode' => new \Twig_Filter_Method($this, 'urlDecode')
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...
41
		);
42
	}
43
44
45
	public function commentForm(\Twig_Environment $env, $form, $ajax=false, $options=[])
46
	{
47
		return $env->render('MykeesCommentBundle:Comments:form.html.twig',[
48
			'form'=>$form,
49
			'ajax'=>$ajax,
50
			'options'=>$options
51
		]);
52
	}
53
54
	/**
55
	 * @param \Twig_Environment $env
56
	 * @param $comments
57
	 * @param array $options
58
	 * @param bool $canAdminComment
59
	 * @return string
60
	 */
61
	public function commentsList(\Twig_Environment $env,$comments, $canAdminComment=false, $options=[])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
	{
63
		return $env->render('MykeesCommentBundle:Comments:comments.html.twig',[
64
			'comments'=>$comments,
65
			"canAdminComment"=>$canAdminComment
66
		]);
67
	}
68
69
70
	public function dateInterval($date,$locale)
71
	{
72
		$comment_date = new \DateTime($date);
73
		$dateNow = new \DateTime('NOW');
74
		$interval = $dateNow->diff($comment_date);
75
76
		$periodes = [
77
			$interval->format('%y'),
78
			$interval->format('%m'),
79
			$interval->format('%d'),
80
			$interval->format('%h'),
81
			$interval->format('%i')
82
		];
83
		$unity_en = [
84
			['year','years'],
85
			['month','months'],
86
			['day','days'],
87
			['hour','hours'],
88
			['minute','minutes']
89
		];
90
		$unity_fr = [
91
			['an','ans'],
92
			['mois','mois'],
93
			['jour','jours'],
94
			['heure','heures'],
95
			['minute','minutes']
96
		];
97
98
		$periodesLength = count($periodes);
99
100
		for($i=0; $i < $periodesLength; $i++)
101
		{
102
			if(intval($periodes[$i]) >= 1 && $i < count($periodes))
103
			{
104
				$locale_unity = $locale == "fr" ? $unity_fr : $unity_en;
105
				$u = $periodes[$i] > 1 ? $locale_unity[$i][1] : $locale_unity[$i][0];
106
107
				return $periodes[$i].' '.$u;
108
			}
109
		}
110
		if($locale == 'fr')
111
		{
112
			return "moins d'une minute";
113
		}else{
114
			return 'less than a minute';
115
		}
116
	}
117
118
	/**
119
	 * Returns the name of the extension.
120
	 *
121
	 * @return string The extension name
122
	 */
123
	public function getName()
124
	{
125
		return 'mykees_helper_comment';
126
	}
127
}
128