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

Prize::getServiceManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator;
6
use Zend\ServiceManager\ServiceManager;
7
use ZfcBase\EventManager\EventProvider;
8
use PlaygroundGame\Options\ModuleOptions;
9
use PlaygroundGame\Mapper\Prize as PrizeMapper;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
class Prize extends EventProvider
13
{
14
    /**
15
     * @var prizeMapper
16
     */
17
    protected $prizeMapper;
18
19
    /**
20
     * @var ServiceManager
21
     */
22
    protected $serviceManager;
23
24
    /**
25
     * @var UserServiceOptionsInterface
26
     */
27
    protected $options;
28
29
    /**
30
     *
31
     * @var ServiceManager
32
     */
33
    protected $serviceLocator;
34
35
    public function __construct(ServiceLocatorInterface $locator)
36
    {
37
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
38
    }
39
40
    /**
41
     *
42
     * This service is ready for all types of games
43
     *
44
     * @param  array                  $data
45
     * @param  string                 $formClass
46
     * @return \PlaygroundGame\Entity\Game
47
     */
48
    public function create(array $data, $prize, $formClass)
49
    {
50
        $form  = $this->serviceLocator->get($formClass);
51
        $form->bind($prize);
52
53
        // If the identifier has not been set, I use the title to create one.
54 View Code Duplication
        if (empty($data['identifier']) && !empty($data['title'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $data['identifier'] = $data['title'];
56
        }
57
        
58
        $form->setData($data);
59
60
        if (!$form->isValid()) {
61
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\Prize::create of type PlaygroundGame\Entity\Game.

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...
62
        }
63
64
        $prize = $this->getPrizeMapper()->insert($prize);
65
66
        return $prize;
67
    }
68
69
    /**
70
     *
71
     * @param  array                  $data
72
     * @param  string                 $formClass
73
     * @return \PlaygroundGame\Entity\Game
74
     */
75
    public function edit(array $data, $prize, $formClass)
76
    {
77
        $entityManager = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
78
        $form  = $this->serviceLocator->get($formClass);
79
        $form->bind($prize);
80
        
81
        $identifierInput = $form->getInputFilter()->get('identifier');
82
        $noObjectExistsValidator = new NoObjectExistsValidator(array(
83
            'object_repository' => $entityManager->getRepository('PlaygroundGame\Entity\Prize'),
84
            'fields'            => 'identifier',
85
            'messages'          => array('objectFound' => 'This url already exists !')
86
        ));
87
        
88
        if ($prize->getIdentifier() != $data['identifier']) {
89
            $identifierInput->getValidatorChain()->addValidator($noObjectExistsValidator);
90
        }
91
92
        $form->setData($data);
93
94
        if (!$form->isValid()) {
95
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\Prize::edit of type PlaygroundGame\Entity\Game.

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...
96
        }
97
98
        $prize = $this->getPrizeMapper()->update($prize);
99
100
        return $prize;
101
    }
102
103
104
    /**
105
     * getPrizeMapper
106
     *
107
     * @return PrizeMapper
108
     */
109 View Code Duplication
    public function getPrizeMapper()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if (null === $this->prizeMapper) {
112
            $this->prizeMapper = $this->serviceLocator->get('playgroundgame_prize_mapper');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->serviceLocator->g...oundgame_prize_mapper') can also be of type array. However, the property $prizeMapper is declared as type object<PlaygroundGame\Service\prizeMapper>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
113
        }
114
115
        return $this->prizeMapper;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->prizeMapper; of type object|array adds the type array to the return on line 115 which is incompatible with the return type documented by PlaygroundGame\Service\Prize::getPrizeMapper of type PlaygroundGame\Mapper\Prize.
Loading history...
116
    }
117
118
    /**
119
     * setPrizeMapper
120
     *
121
     * @param  PrizeMapper $prizeMapper
122
     * @return Prize
123
     */
124
    public function setPrizeMapper(PrizeMapper $prizeMapper)
125
    {
126
        $this->prizeMapper = $prizeMapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prizeMapper of type object<PlaygroundGame\Mapper\Prize> is incompatible with the declared type object<PlaygroundGame\Service\prizeMapper> of property $prizeMapper.

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...
127
128
        return $this;
129
    }
130
131
    public function setOptions(ModuleOptions $options)
132
    {
133
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type object<PlaygroundGame\Options\ModuleOptions> is incompatible with the declared type object<PlaygroundGame\Se...erviceOptionsInterface> of property $options.

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...
134
135
        return $this;
136
    }
137
138
    public function getOptions()
139
    {
140
        if (!$this->options instanceof ModuleOptions) {
141
            $this->setOptions($this->serviceLocator->get('playgroundgame_module_options'));
0 ignored issues
show
Documentation introduced by
$this->serviceLocator->g...ndgame_module_options') is of type object|array, but the function expects a object<PlaygroundGame\Options\ModuleOptions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
        }
143
144
        return $this->options;
145
    }
146
147
    /**
148
     * Retrieve service manager instance
149
     *
150
     * @return ServiceManager
151
     */
152
    public function getServiceManager()
153
    {
154
        return $this->serviceManager;
155
    }
156
157
    /**
158
     * Set service manager instance
159
     *
160
     * @param  ServiceManager $serviceManager
161
     * @return Prize
162
     */
163
    public function setServiceManager(ServiceManager $serviceManager)
164
    {
165
        $this->serviceManager = $serviceManager;
166
167
        return $this;
168
    }
169
}
170