GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( df80e7...7471e3 )
by Borut
06:04
created

FormExtension::getFunctions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Application\Twig;
4
5
use Silex\Application;
6
7
/**
8
 * @author Borut Balažek <[email protected]>
9
 */
10
class FormExtension extends \Twig_Extension
11
{
12
    /**
13
     * @return string
14
     */
15
    public function getName()
16
    {
17
        return 'application/form';
18
    }
19
20
    /**
21
     * @return array
22
     */
23
    public function getFunctions()
24
    {
25
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('form_has_e...e' => array('html')))); (array<string,Twig_Function_Method>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_SimpleFunction[].

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...
26
            'form_has_errors' => new \Twig_Function_Method(
27
                $this,
28
                'formHasErrors',
29
                array(
30
                    'is_safe' => array('html'),
31
                )
32
            ),
33
            'form_value' => new \Twig_Function_Method(
34
                $this,
35
                'formValue',
36
                array(
37
                    'is_safe' => array('html'),
38
                )
39
            ),
40
            'form_checkbox_value' => new \Twig_Function_Method(
41
                $this,
42
                'formCheckboxValue',
43
                array(
44
                    'is_safe' => array('html'),
45
                )
46
            ),
47
        );
48
    }
49
50
    /**
51
     * @param boolean
52
     */
53
    public function formHasErrors($form)
54
    {
55
        return count($form->vars['errors']) > 0;
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    public function formValue($form, $fallback = null)
62
    {
63
        return $form->vars['value']
64
            ? $form->vars['value']
65
            : $fallback
66
        ;
67
    }
68
69
    /**
70
     * @return boolean
71
     */
72
    public function formCheckboxValue($form)
73
    {
74
        return $form->vars['checked']
75
            ? true
76
            : false
77
        ;
78
    }
79
}
80