Completed
Push — develop ( 8c855f...4c50ac )
by greg
03:25
created

PrizeCategoryController::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\View\Model\ViewModel;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
class PrizeCategoryController extends GameController
9
{
10
    protected $gameService;
11
12
    protected $prizeCategoryService;
13
14
    /**
15
     *
16
     * @var ServiceManager
17
     */
18
    protected $serviceLocator;
19
20
    public function __construct(ServiceLocatorInterface $locator)
21
    {
22
        $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...
23
    }
24
25
    public function getServiceLocator()
26
    {
27
        
28
        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 declared by the interface Zend\ServiceManager\Serv...face::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...
29
    }
30
    
31
    public function indexAction()
32
    {
33
        $identifier = $this->getEvent()->getRouteMatch()->getParam('id');
34
35
        if (!$identifier) {
36
            return $this->notFoundAction();
37
        }
38
39
        $prizeCategory = $this->getPrizeCategoryService()->getPrizeCategoryMapper()->findByIdentifier($identifier);
40
        $idPrizeCategory = $prizeCategory->getId();
41
42
        $games = $this->getGameService()->getPrizeCategoryGames($idPrizeCategory);
43
44
        if (!$games) {
45
            $this->flashMessenger()->addMessage('Il n\'y a aucun jeu disponible pour cette thématique');
46
        }
47
48
        if (is_array($games)) {
49
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($games));
50
            $paginator->setItemCountPerPage(7);
51
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
52
        } else {
53
            $paginator = $games;
54
        }
55
56
        $bitlyclient = $this->getOptions()->getBitlyUrl();
57
        $bitlyuser = $this->getOptions()->getBitlyUsername();
58
        $bitlykey = $this->getOptions()->getBitlyApiKey();
59
60
        $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient);
61
        $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser);
62
        $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey);
63
        
64
        $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...
65
            array(
66
                'breadcrumbTitle' => $prizeCategory->getTitle(),
67
                'currentPage' => array(
68
                    'pageGames' => 'games',
69
                    'pageWinners' => ''
70
                ),
71
                'headParams' => array(
72
                    'headTitle' => $prizeCategory->getTitle(),
73
                ),
74
            )
75
        );
76
77
        return new ViewModel(
78
            array(
79
                'games'            => $paginator,
80
                'prizeCategory'    => $prizeCategory,
81
                'identifier'        => $identifier,
82
                'flashMessages'        => $this->flashMessenger()->getMessages(),
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 getPrizeCategoryService()
97
    {
98
        if (!$this->prizeCategoryService) {
99
            $this->prizeCategoryService = $this->getServiceLocator()->get('playgroundgame_prizecategory_service');
100
        }
101
102
        return $this->prizeCategoryService;
103
    }
104
105
    public function setPrizeCategoryService(\PlaygroundGame\Service\PrizeCategory $prizeCategoryService)
106
    {
107
        $this->prizeCategoryService = $prizeCategoryService;
108
109
        return $this;
110
    }
111
}
112