ConfigHelper   D
last analyzed

Complexity

Total Complexity 59

Size/Duplication

Total Lines 394
Duplicated Lines 14.21 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 59
lcom 1
cbo 7
dl 56
loc 394
ccs 0
cts 220
cp 0
rs 4.08
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A init() 0 17 4
A getToken() 14 14 4
A saveToken() 0 5 1
A tokenIsSet() 0 4 2
A getAccounts() 0 15 2
A getAccountId() 14 14 4
A saveAccountId() 0 5 1
A accountIsSet() 0 4 2
B getProperties() 0 26 6
A getPropertyId() 14 14 4
A savePropertyId() 0 5 1
A propertyIsSet() 0 4 2
B getProfiles() 0 33 9
A getProfileId() 14 14 4
A saveProfileId() 0 5 1
A profileIsSet() 0 4 2
A getActiveProfile() 0 15 4
A getProfileSegments() 0 27 3
A saveConfigName() 0 4 1
A getAuthUrl() 0 8 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ConfigHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConfigHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Kunstmaan\DashboardBundle\Helper\Google\Analytics;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
7
8
class ConfigHelper
9
{
10
    /** @var ServiceHelper */
11
    private $serviceHelper;
12
13
    /** @var string */
14
    private $token = false;
15
16
    /** @var string */
17
    private $propertyId = false;
18
19
    /** @var string */
20
    private $accountId = false;
21
22
    /** @var string */
23
    private $profileId = false;
24
25
    /** @var EntityManager */
26
    private $em;
27
28
    /**
29
     * constructor
30
     *
31
     * @param ServiceHelper $serviceHelper
32
     * @param EntityManager $em
33
     */
34
    public function __construct(ServiceHelper $serviceHelper, EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
35
    {
36
        $this->serviceHelper = $serviceHelper;
37
        $this->em = $em;
38
        $this->init();
39
    }
40
41
    /**
42
     * Tries to initialise the Client object
43
     *
44
     * @param int $configId
0 ignored issues
show
Documentation introduced by
Should the type for parameter $configId 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...
45
     */
46
    public function init($configId = false)
47
    {
48
        // if token is already saved in the database
49
        if ($this->getToken($configId) && '' !== $this->getToken($configId)) {
0 ignored issues
show
Bug introduced by
It seems like $configId defined by parameter $configId on line 46 can also be of type integer; however, Kunstmaan\DashboardBundl...onfigHelper::getToken() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
            $this
51
                ->serviceHelper
52
                ->getClientHelper()
53
                ->getClient()
54
                ->setAccessToken($this->getToken($configId));
0 ignored issues
show
Bug introduced by
It seems like $configId defined by parameter $configId on line 46 can also be of type integer; however, Kunstmaan\DashboardBundl...onfigHelper::getToken() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
        }
56
57
        if ($configId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $configId of type false|integer is loosely compared to true; 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...
58
            $this->getAccountId($configId);
0 ignored issues
show
Documentation introduced by
$configId is of type integer, but the function expects a boolean.

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...
59
            $this->getPropertyId($configId);
0 ignored issues
show
Documentation introduced by
$configId is of type integer, but the function expects a boolean.

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...
60
            $this->getProfileId($configId);
0 ignored issues
show
Documentation introduced by
$configId is of type integer, but the function expects a boolean.

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...
61
        }
62
    }
63
64
    /* =============================== TOKEN =============================== */
65
66
    /**
67
     * Get the token from the database
68
     *
69
     * @return string $token
70
     */
71 View Code Duplication
    private function getToken($configId = 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...
72
    {
73
        if (!$this->token || $configId) {
74
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
75
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
76
            if ($configId) {
77
                $this->token = $analyticsConfigRepository->find($configId)->getToken();
78
            } else {
79
                $this->token = $analyticsConfigRepository->findFirst()->getToken();
80
            }
81
        }
82
83
        return $this->token;
84
    }
85
86
    /**
87
     * Save the token to the database
88
     */
89
    public function saveToken($token, $configId = false)
90
    {
91
        $this->token = $token;
92
        $this->em->getRepository(AnalyticsConfig::class)->saveToken($token, $configId);
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 saveToken() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
93
    }
94
95
    /**
96
     * Check if token is set
97
     *
98
     * @return bool $result
99
     */
100
    public function tokenIsSet()
101
    {
102
        return $this->getToken() && '' !== $this->getToken();
103
    }
104
105
    /* =============================== ACCOUNT =============================== */
106
107
    /**
108
     * Get a list of all available accounts
109
     *
110
     * @return array $data A list of all properties
111
     */
112
    public function getAccounts()
113
    {
114
        $accounts = $this->serviceHelper->getService()->management_accounts->listManagementAccounts()->getItems();
115
        $data = array();
116
117
        foreach ($accounts as $account) {
118
            $data[$account->getName()] = array(
119
                    'accountId' => $account->getId(),
120
                    'accountName' => $account->getName(),
121
                );
122
        }
123
        ksort($data);
124
125
        return $data;
126
    }
127
128
    /**
129
     * Get the accountId from the database
130
     *
131
     * @return string $accountId
132
     */
133 View Code Duplication
    public function getAccountId($configId = 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...
134
    {
135
        if (!$this->accountId || $configId) {
136
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
137
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
138
            if ($configId) {
139
                $this->accountId = $analyticsConfigRepository->find($configId)->getAccountId();
140
            } else {
141
                $this->accountId = $analyticsConfigRepository->findFirst()->getAccountId();
142
            }
143
        }
144
145
        return $this->accountId;
146
    }
147
148
    /**
149
     * Save the accountId to the database
150
     */
151
    public function saveAccountId($accountId, $configId = false)
152
    {
153
        $this->accountId = $accountId;
154
        $this->em->getRepository(AnalyticsConfig::class)->saveAccountId($accountId, $configId);
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 saveAccountId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
155
    }
156
157
    /**
158
     * Check if token is set
159
     *
160
     * @return bool $result
161
     */
162
    public function accountIsSet()
163
    {
164
        return $this->getAccountId() && '' !== $this->getAccountId();
165
    }
166
167
    /* =============================== PROPERTY =============================== */
168
169
    /**
170
     * Get a list of all available properties
171
     *
172
     * @return array $data A list of all properties
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
173
     */
174
    public function getProperties($accountId = false)
175
    {
176
        if (!$this->getAccountId() && !$accountId) {
177
            return false;
178
        }
179
180
        if ($accountId) {
181
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($accountId);
182
        } else {
183
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($this->getAccountId());
184
        }
185
        $data = array();
186
187
        foreach ($webproperties->getItems() as $property) {
188
            $profiles = $this->getProfiles($accountId, $property->getId());
189
            if (\count($profiles) > 0) {
190
                $data[$property->getName()] = array(
191
                        'propertyId' => $property->getId(),
192
                        'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')',
193
                    );
194
            }
195
        }
196
        ksort($data);
197
198
        return $data;
199
    }
200
201
    /**
202
     * Get the propertyId from the database
203
     *
204
     * @return string $propertyId
205
     */
206 View Code Duplication
    public function getPropertyId($configId = 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...
207
    {
208
        if (!$this->propertyId || $configId) {
209
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
210
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
211
            if ($configId) {
212
                $this->propertyId = $analyticsConfigRepository->find($configId)->getPropertyId();
213
            } else {
214
                $this->propertyId = $analyticsConfigRepository->findFirst()->getPropertyId();
215
            }
216
        }
217
218
        return $this->propertyId;
219
    }
220
221
    /**
222
     * Save the propertyId to the database
223
     */
224
    public function savePropertyId($propertyId, $configId = false)
225
    {
226
        $this->propertyId = $propertyId;
227
        $this->em->getRepository(AnalyticsConfig::class)->savePropertyId($propertyId, $configId);
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 savePropertyId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
228
    }
229
230
    /**
231
     * Check if propertyId is set
232
     *
233
     * @return bool $result
234
     */
235
    public function propertyIsSet()
236
    {
237
        return null !== $this->getPropertyId() && '' !== $this->getPropertyId();
238
    }
239
240
    /* =============================== PROFILE =============================== */
241
242
    /**
243
     * Get a list of all available profiles
244
     *
245
     * @return array $data A list of all properties
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
246
     */
247
    public function getProfiles($accountId = false, $propertyId = false)
248
    {
249
        if ((!$this->getAccountId() && !$accountId) || (!$this->getPropertyId() && !$propertyId)) {
250
            return false;
251
        }
252
253
        // get views
254
        if ($accountId && $propertyId) {
255
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
256
                    $accountId,
257
                    $propertyId
258
                );
259
        } else {
260
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
261
                    $this->getAccountId(),
262
                    $this->getPropertyId()
263
                );
264
        }
265
266
        $data = array();
267
        if (\is_array($profiles->getItems())) {
268
            foreach ($profiles->getItems() as $profile) {
269
                $data[$profile->name] = array(
270
                            'profileId' => $profile->id,
271
                            'profileName' => $profile->name,
272
                            'created' => $profile->created,
273
                        );
274
            }
275
        }
276
        ksort($data);
277
278
        return $data;
279
    }
280
281
    /**
282
     * Get the propertyId from the database
283
     *
284
     * @return string $propertyId
285
     */
286 View Code Duplication
    public function getProfileId($configId = 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...
287
    {
288
        if (!$this->profileId || $configId) {
289
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
290
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
291
            if ($configId) {
292
                $this->profileId = $analyticsConfigRepository->find($configId)->getProfileId();
293
            } else {
294
                $this->profileId = $analyticsConfigRepository->findFirst()->getProfileId();
295
            }
296
        }
297
298
        return $this->profileId;
299
    }
300
301
    /**
302
     * Save the profileId to the database
303
     */
304
    public function saveProfileId($profileId, $configId = false)
305
    {
306
        $this->profileId = $profileId;
307
        $this->em->getRepository(AnalyticsConfig::class)->saveProfileId($profileId, $configId);
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 saveProfileId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
308
    }
309
310
    /**
311
     * Check if token is set
312
     *
313
     * @return bool $result
314
     */
315
    public function profileIsSet()
316
    {
317
        return null !== $this->getProfileId() && '' !== $this->getProfileId();
318
    }
319
320
    /**
321
     * Get the active profile
322
     *
323
     * @return the profile
324
     */
325
    public function getActiveProfile()
326
    {
327
        $profiles = $this->getProfiles();
328
        $profileId = $this->getProfileId();
329
330
        if (!\is_array($profiles)) {
331
            throw new \Exception('<fg=red>The config is invalid</fg=red>');
332
        }
333
334
        foreach ($profiles as $profile) {
335
            if ($profile['profileId'] == $profileId) {
336
                return $profile;
337
            }
338
        }
339
    }
340
341
    /* =============================== PROFILE SEGMENTS =============================== */
342
343
    /**
344
     * get all segments for the saved profile
345
     *
346
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
347
     */
348
    public function getProfileSegments()
349
    {
350
        $profileSegments = $this
351
                    ->serviceHelper
352
                    ->getService()
353
                    ->management_segments
354
                    ->listManagementSegments()
355
                    ->items;
356
357
        $builtin = array();
358
        $own = array();
359
        foreach ($profileSegments as $segment) {
360
            if ($segment->type == 'BUILT_IN') {
361
                $builtin[] = array(
362
                        'name' => $segment->name,
363
                        'query' => $segment->segmentId,
364
                    );
365
            } else {
366
                $own[] = array(
367
                        'name' => $segment->name,
368
                        'query' => $segment->segmentId,
369
                    );
370
            }
371
        }
372
373
        return array('builtin' => $builtin, 'own' => $own);
374
    }
375
376
    /* =============================== CONFIG =============================== */
377
378
    /**
379
     * Save the config to the database
380
     */
381
    public function saveConfigName($configName, $configId = false)
382
    {
383
        $this->em->getRepository(AnalyticsConfig::class)->saveConfigName($configName, $configId);
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 saveConfigName() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
384
    }
385
386
    /* =============================== AUTH URL =============================== */
387
388
    /**
389
     * get the authUrl
390
     *
391
     * @return string $authUrl
392
     */
393
    public function getAuthUrl()
394
    {
395
        return $this
396
                ->serviceHelper
397
                ->getClientHelper()
398
                ->getClient()
399
                ->createAuthUrl();
400
    }
401
}
402