Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

AnalyticsConfigRepository   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 203
Duplicated Lines 10.34 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 5
dl 21
loc 203
ccs 0
cts 117
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A findFirst() 0 17 4
A findDefaultOverviews() 0 10 1
A createConfig() 0 12 1
B flushConfig() 0 24 6
A setUpdated() 0 8 2
A saveToken() 0 8 2
A savePropertyId() 0 8 2
A saveAccountId() 0 8 2
A saveProfileId() 7 8 2
A saveConfigName() 7 8 2
A resetProfileId() 7 8 2
A resetPropertyId() 0 10 2

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 Kunstmaan\DashboardBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
7
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
8
9
/**
10
 * AnalyticsConfigRepository
11
 *
12
 * This class was generated by the Doctrine ORM. Add your own custom
13
 * repository methods below.
14
 */
15
class AnalyticsConfigRepository extends EntityRepository
16
{
17
    /**
18
     * Get the first config from the database, creates a new entry if the config doesn't exist yet
19
     *
20
     * @return AnalyticsConfig $config
21
     */
22
    public function findFirst($createNew = true)
23
    {
24
        // Backwards compatibility: select the first config, still used in the dashboard, specified config ids are set in the dashboard collection bundle
25
        $em = $this->getEntityManager();
26
        $query = $em->createQuery('SELECT c FROM KunstmaanDashboardBundle:AnalyticsConfig c');
27
        $result = $query->getResult();
28
        // if no configs exist, create a new one
29
        if (!$result && $createNew) {
30
            return $this->createConfig();
31
        }
32
33
        if ($result) {
34
            return $result[0];
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Get the default overviews for a config
42
     *
43
     * @return AnalyticsConfig $config
0 ignored issues
show
Documentation introduced by
Should the return type not be object[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function findDefaultOverviews($config)
46
    {
47
        $em = $this->getEntityManager();
48
49
        return $em->getRepository(AnalyticsOverview::class)
50
            ->findBy(array(
51
                    'config' => $config,
52
                    'segment' => null,
53
                ));
54
    }
55
56
    /**
57
     * Create a new config
58
     *
59
     * @return AnalyticsConfig
60
     */
61
    public function createConfig()
62
    {
63
        $em = $this->getEntityManager();
64
65
        $config = new AnalyticsConfig();
66
        $em->persist($config);
67
        $em->flush();
68
69
        $this->getEntityManager()->getRepository(AnalyticsOverview::class)->addOverviews($config);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method addOverviews() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...yticsOverviewRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
70
71
        return $config;
72
    }
73
74
    /**
75
     * Flush a config
76
     *
77
     * @param int $id the config id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
78
     */
79
    public function flushConfig($id = false)
80
    {
81
        $em = $this->getEntityManager();
82
83
        // Backward compatibilty to flush overviews without a config set
84
        if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
85
            $overviewRepository = $em->getRepository(AnalyticsOverview::class);
86
            foreach ($overviewRepository->findAll() as $overview) {
87
                $em->remove($overview);
88
            }
89
            $em->flush();
90
91
            return;
92
        }
93
94
        $config = $id ? $this->find($id) : $this->findFirst();
95
        foreach ($config->getOverviews() as $overview) {
96
            $em->remove($overview);
97
        }
98
        foreach ($config->getSegments() as $segment) {
99
            $em->remove($segment);
100
        }
101
        $em->flush();
102
    }
103
104
    /**
105
     * Update the timestamp when data is collected
106
     *
107
     * @param int id
108
     */
109
    public function setUpdated($id = false)
110
    {
111
        $em = $this->getEntityManager();
112
        $config = $id ? $this->find($id) : $this->findFirst();
113
        $config->setLastUpdate(new \DateTime());
114
        $em->persist($config);
115
        $em->flush();
116
    }
117
118
    /**
119
     * saves the token
120
     *
121
     * @param string $token
122
     */
123
    public function saveToken($token, $id = false)
124
    {
125
        $em = $this->getEntityManager();
126
        $config = $id ? $this->find($id) : $this->findFirst();
127
        $config->setToken($token);
128
        $em->persist($config);
129
        $em->flush();
130
    }
131
132
    /**
133
     * saves the property id
134
     *
135
     * @param string $propertyId
136
     */
137
    public function savePropertyId($propertyId, $id = false)
138
    {
139
        $em = $this->getEntityManager();
140
        $config = $id ? $this->find($id) : $this->findFirst();
141
        $config->setPropertyId($propertyId);
142
        $em->persist($config);
143
        $em->flush();
144
    }
145
146
    /**
147
     * saves the account id
148
     *
149
     * @param string $accountId
150
     */
151
    public function saveAccountId($accountId, $id = false)
152
    {
153
        $em = $this->getEntityManager();
154
        $config = $id ? $this->find($id) : $this->findFirst();
155
        $config->setAccountId($accountId);
156
        $em->persist($config);
157
        $em->flush();
158
    }
159
160
    /**
161
     * saves the profile id
162
     *
163
     * @param string $profileId
164
     */
165 View Code Duplication
    public function saveProfileId($profileId, $id = false)
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...
166
    {
167
        $em = $this->getEntityManager();
168
        $config = $id ? $this->find($id) : $this->findFirst();
169
        $config->setProfileId($profileId);
170
        $em->persist($config);
171
        $em->flush();
172
    }
173
174
    /**
175
     * saves the config name
176
     *
177
     * @param string $profileId
0 ignored issues
show
Bug introduced by
There is no parameter named $profileId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
     */
179 View Code Duplication
    public function saveConfigName($name, $id = false)
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...
180
    {
181
        $em = $this->getEntityManager();
182
        $config = $id ? $this->find($id) : $this->findFirst();
183
        $config->setName($name);
184
        $em->persist($config);
185
        $em->flush();
186
    }
187
188
    /**
189
     * Resets the profile id
190
     *
191
     * @param int id
192
     */
193 View Code Duplication
    public function resetProfileId($id = false)
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...
194
    {
195
        $em = $this->getEntityManager();
196
        $config = $id ? $this->find($id) : $this->findFirst();
197
        $config->setProfileId('');
198
        $em->persist($config);
199
        $em->flush();
200
    }
201
202
    /**
203
     * Resets the  account id, property id and profile id
204
     *
205
     * @param int id
206
     */
207
    public function resetPropertyId($id = false)
208
    {
209
        $em = $this->getEntityManager();
210
        $config = $id ? $this->find($id) : $this->findFirst();
211
        $config->setAccountId('');
212
        $config->setProfileId('');
213
        $config->setPropertyId('');
214
        $em->persist($config);
215
        $em->flush();
216
    }
217
}
218