ZfcUserLoginWidget   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoginForm() 0 4 1
A setLoginForm() 0 5 1
A setViewTemplate() 0 5 1
A __invoke() 0 20 2
1
<?php
2
3
namespace ZfcUser\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
use ZfcUser\Form\Login as LoginForm;
7
use Zend\View\Model\ViewModel;
8
9
class ZfcUserLoginWidget extends AbstractHelper
10
{
11
    /**
12
     * Login Form
13
     * @var LoginForm
14
     */
15
    protected $loginForm;
16
17
    /**
18
     * $var string template used for view
19
     */
20
    protected $viewTemplate;
21
    /**
22
     * __invoke
23
     *
24
     * @access public
25
     * @param array $options array of options
26
     * @return string
27
     */
28
    public function __invoke($options = array())
29
    {
30
        $options += array(
31
            'render' => true,
32
            'redirect' => false,
33
            'enableRegistration' => false,
34
        );
35
36
        $vm = new ViewModel(array(
37
            'loginForm' => $this->getLoginForm(),
38
            'redirect'  => $options['redirect'],
39
            'enableRegistration'  => $options['enableRegistration'],
40
        ));
41
        $vm->setTemplate($this->viewTemplate);
42
        if ($options['render']) {
43
            return $this->getView()->render($vm);
44
        }
45
46
        return $vm;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $vm; (Zend\View\Model\ViewModel) is incompatible with the return type documented by ZfcUser\View\Helper\ZfcUserLoginWidget::__invoke of type string.

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...
47
    }
48
49
    /**
50
     * Retrieve Login Form Object
51
     * @return LoginForm
52
     */
53
    public function getLoginForm()
54
    {
55
        return $this->loginForm;
56
    }
57
58
    /**
59
     * Inject Login Form Object
60
     * @param LoginForm $loginForm
61
     * @return ZfcUserLoginWidget
62
     */
63
    public function setLoginForm(LoginForm $loginForm)
64
    {
65
        $this->loginForm = $loginForm;
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $viewTemplate
71
     * @return ZfcUserLoginWidget
72
     */
73
    public function setViewTemplate($viewTemplate)
74
    {
75
        $this->viewTemplate = $viewTemplate;
76
        return $this;
77
    }
78
}
79