Completed
Push — master ( 437856...5fb154 )
by greg
03:04
created

HomeController::getServiceLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundGame\Controller\Frontend;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\View\Model\ViewModel;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
9
class HomeController extends AbstractActionController
10
{
11
    /**
12
     * @var \PlaygroundGame\Service\GameService
13
     */
14
    protected $gameService;
15
    
16
    /**
17
     * @var \PlaygroundCms\Service\Page
18
     */
19
    protected $pageService;
20
    
21
    /**
22
     *
23
     * @var ServiceManager
24
     */
25
    protected $serviceLocator;
26
27
    public function __construct(ServiceLocatorInterface $locator)
28
    {
29
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $locator of type object<Zend\ServiceManag...erviceLocatorInterface> is incompatible with the declared type object<PlaygroundGame\Co...rontend\ServiceManager> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
    }
31
32
    public function getServiceLocator()
33
    {
34
        
35
        return $this->serviceLocator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serviceLocator; (PlaygroundGame\Controller\Frontend\ServiceManager) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...ller::getServiceLocator of type Zend\ServiceManager\ServiceLocatorInterface.

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...
36
    }
37
    
38
    public function indexAction()
39
    {
40
        $layoutViewModel = $this->layout();
41
42
        $slider = new ViewModel();
43
        $slider->setTemplate('playground-game/common/top_promo');
44
45
        $sliderGames = $this->getGameService()->getActiveSliderGames();
46
        $sliderPages = $this->getPageService()->getActiveSliderPages();
47
48
        // I merge both types of articles and sort them in reverse order of their key
49
        // And as their key is some sort of... date !, It means I sort it in date reverse order ;)
50
        $sliderItems = array_merge($sliderGames, $sliderPages);
51
52
        krsort($sliderItems);
53
54
        $slider->setVariables(array('sliderItems' => $sliderItems));
55
56
        $layoutViewModel->addChild($slider, 'slider');
0 ignored issues
show
Bug introduced by
The method addChild does only exist in Zend\View\Model\ModelInterface, but not in Zend\Mvc\Controller\Plugin\Layout.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
58
        $games = $this->getGameService()->getActiveGames(true, '', '');
59
        $pages = $this->getPageService()->getActivePages();
60
61
        // I merge both types of articles and sort them in reverse order of their key
62
        // And as their key is some sort of... date !, It means I sort it in date reverse order ;)
63
        $items = array_merge($games, $pages);
64
        krsort($items);
65
66
        if (is_array($items)) {
67
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($items));
68
            $paginator->setItemCountPerPage(7);
69
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
70
        } else {
71
            $paginator = $items;
72
        }
73
74
        $this->layout()->setVariables(
0 ignored issues
show
Bug introduced by
The method setVariables does only exist in Zend\View\Model\ModelInterface, but not in Zend\Mvc\Controller\Plugin\Layout.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
            array(
76
                'sliderItems'    => $sliderItems,
77
            )
78
        );
79
80
        return new ViewModel(
81
            array(
82
                'items'    => $paginator,
83
               )
84
        );
85
    }
86
    
87
    public function getGameService()
88
    {
89
        if (!$this->gameService) {
90
            $this->gameService = $this->getServiceLocator()->get('playgroundgame_game_service');
91
        }
92
    
93
        return $this->gameService;
94
    }
95
    
96
    public function setGameService(\PlaygroundGame\Service\Game $gameService)
97
    {
98
        $this->gameService = $gameService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gameService of type object<PlaygroundGame\Service\Game> is incompatible with the declared type object<PlaygroundGame\Service\GameService> of property $gameService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
    
100
        return $this;
101
    }
102
    
103
    public function getPageService()
104
    {
105
        if (!$this->pageService) {
106
            $this->pageService = $this->getServiceLocator()->get('playgroundcms_page_service');
107
        }
108
    
109
        return $this->pageService;
110
    }
111
    
112
    public function setPageService(\PlaygroundCms\Service\Page $pageService)
113
    {
114
        $this->pageService = $pageService;
115
    
116
        return $this;
117
    }
118
}
119