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

PrizeCategory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 178
Duplicated Lines 10.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 4
dl 18
loc 178
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getActivePrizeCategories() 0 9 1
A getPrizeCategoryMapper() 0 8 2
A setPrizeCategoryMapper() 0 6 1
A setOptions() 0 6 1
A getOptions() 0 8 2
A getServiceManager() 0 4 1
A setServiceManager() 0 6 1
B create() 9 30 3
B edit() 9 28 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use Zend\ServiceManager\ServiceManager;
6
use ZfcBase\EventManager\EventProvider;
7
use PlaygroundGame\Options\ModuleOptions;
8
use PlaygroundGame\Mapper\PrizeCategory as PrizeCategoryMapper;
9
use Zend\Stdlib\ErrorHandler;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
class PrizeCategory extends EventProvider
13
{
14
    /**
15
     * @var prizeCategoryMapper
16
     */
17
    protected $prizeCategoryMapper;
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, $prizeCategory, $formClass)
49
    {
50
        $form  = $this->serviceLocator->get($formClass);
51
        $form->bind($prizeCategory);
52
53
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
54
        $media_url = $this->getOptions()->getMediaUrl() . '/';
55
56
        $form->setData($data);
57
58
        if (!$form->isValid()) {
59
            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\PrizeCategory::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...
60
        }
61
62
        $prizeCategory = $this->getPrizeCategoryMapper()->insert($prizeCategory);
63
64 View Code Duplication
        if (!empty($data['upload_picto']['tmp_name'])) {
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...
65
            ErrorHandler::start();
66
            move_uploaded_file(
67
                $data['upload_picto']['tmp_name'],
68
                $path . $prizeCategory->getId() . "-" . $data['upload_picto']['name']
69
            );
70
            $prizeCategory->setPicto($media_url . $prizeCategory->getId() . "-" . $data['upload_picto']['name']);
71
            ErrorHandler::stop(true);
72
        }
73
74
        $prizeCategory = $this->getPrizeCategoryMapper()->update($prizeCategory);
75
76
        return $prizeCategory;
77
    }
78
79
    /**
80
     *
81
     * @param  array                  $data
82
     * @param  string                 $formClass
83
     * @return \PlaygroundGame\Entity\Game
84
     */
85
    public function edit(array $data, $prizeCategory, $formClass)
86
    {
87
        $form  = $this->serviceLocator->get($formClass);
88
        $form->bind($prizeCategory);
89
90
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
91
        $media_url = $this->getOptions()->getMediaUrl() . '/';
92
93
        $form->setData($data);
94
95
        if (!$form->isValid()) {
96
            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\PrizeCategory::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...
97
        }
98
99 View Code Duplication
        if (!empty($data['upload_picto']['tmp_name'])) {
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...
100
            ErrorHandler::start();
101
            move_uploaded_file(
102
                $data['upload_picto']['tmp_name'],
103
                $path . $prizeCategory->getId() . "-" . $data['upload_picto']['name']
104
            );
105
            $prizeCategory->setPicto($media_url . $prizeCategory->getId() . "-" . $data['upload_picto']['name']);
106
            ErrorHandler::stop(true);
107
        }
108
109
        $prizeCategory = $this->getPrizeCategoryMapper()->update($prizeCategory);
110
111
        return $prizeCategory;
112
    }
113
114
    public function getActivePrizeCategories()
115
    {
116
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
117
118
        $query = $em->createQuery('SELECT p FROM PlaygroundGame\Entity\PrizeCategory p WHERE p.active = true');
119
        $categories = $query->getResult();
120
121
        return $categories;
122
    }
123
124
    /**
125
     * getPrizeCategoryMapper
126
     *
127
     * @return PrizeCategoryMapper
128
     */
129
    public function getPrizeCategoryMapper()
130
    {
131
        if (null === $this->prizeCategoryMapper) {
132
            $this->prizeCategoryMapper = $this->serviceLocator->get('playgroundgame_prizecategory_mapper');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->serviceLocator->g..._prizecategory_mapper') can also be of type array. However, the property $prizeCategoryMapper is declared as type object<PlaygroundGame\Se...ce\prizeCategoryMapper>. 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...
133
        }
134
135
        return $this->prizeCategoryMapper;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->prizeCategoryMapper; of type object|array adds the type array to the return on line 135 which is incompatible with the return type documented by PlaygroundGame\Service\P...:getPrizeCategoryMapper of type PlaygroundGame\Mapper\PrizeCategory.
Loading history...
136
    }
137
138
    /**
139
     * setPrizeCategoryMapper
140
     *
141
     * @param  PrizeCategoryMapper $prizeCategoryMapper
142
     * @return PrizeCategory
143
     */
144
    public function setPrizeCategoryMapper(PrizeCategoryMapper $prizeCategoryMapper)
145
    {
146
        $this->prizeCategoryMapper = $prizeCategoryMapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prizeCategoryMapper of type object<PlaygroundGame\Mapper\PrizeCategory> is incompatible with the declared type object<PlaygroundGame\Se...ce\prizeCategoryMapper> of property $prizeCategoryMapper.

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...
147
148
        return $this;
149
    }
150
151
    public function setOptions(ModuleOptions $options)
152
    {
153
        $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...
154
155
        return $this;
156
    }
157
158
    public function getOptions()
159
    {
160
        if (!$this->options instanceof ModuleOptions) {
161
            $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...
162
        }
163
164
        return $this->options;
165
    }
166
167
    /**
168
     * Retrieve service manager instance
169
     *
170
     * @return ServiceManager
171
     */
172
    public function getServiceManager()
173
    {
174
        return $this->serviceManager;
175
    }
176
177
    /**
178
     * Set service manager instance
179
     *
180
     * @param  ServiceManager $serviceManager
181
     * @return PrizeCategory
182
     */
183
    public function setServiceManager(ServiceManager $serviceManager)
184
    {
185
        $this->serviceManager = $serviceManager;
186
187
        return $this;
188
    }
189
}
190