Completed
Push — master ( c8e891...facd1d )
by Jens
08:13
created

LocalizedSuggestionCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 62.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
c 1
b 1
f 0
lcom 1
cbo 4
dl 32
loc 51
ccs 17
cts 21
cp 0.8095
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 7 7 1
A getLanguage() 11 11 3
A get() 14 14 4

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
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Product;
7
8
use Commercetools\Core\Error\InvalidArgumentException;
9
use Commercetools\Core\Error\Message;
10
use Commercetools\Core\Model\Common\Collection;
11
use Commercetools\Core\Model\Common\Context;
12
13
/**
14
 * @package Commercetools\Core\Model\Product
15
 * @link https://dev.commercetools.com/http-api-projects-products-search.html#representations
16
 * @method SuggestionCollection current()
17
 * @method LocalizedSuggestionCollection add(SuggestionCollection $element)
18
 * @method SuggestionCollection getAt($offset)
19
 */
20
class LocalizedSuggestionCollection extends Collection
21
{
22
    protected $type = '\Commercetools\Core\Model\Product\SuggestionCollection';
23
24
    /**
25
     * @param $locale
26
     * @return SuggestionCollection
27
     */
28 1 View Code Duplication
    public function __get($locale)
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...
29
    {
30 1
        $context = new Context();
31 1
        $context->setGraceful($this->getContext()->isGraceful())
0 ignored issues
show
Unused Code introduced by
The call to the method Commercetools\Core\Model...Context::setLanguages() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
32 1
            ->setLanguages([$locale]);
33 1
        return $this->get($context);
34
    }
35
36
    /**
37
     * @param Context $context
38
     * @return string
39
     */
40 1 View Code Duplication
    protected function getLanguage(Context $context)
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...
41
    {
42 1
        $locale = null;
43 1
        foreach ($context->getLanguages() as $language) {
44 1
            if (isset($this[$language])) {
45 1
                $locale = $language;
46 1
                break;
47
            }
48
        }
49 1
        return $locale;
50
    }
51
52
    /**
53
     * @param Context $context
54
     * @return SuggestionCollection
55
     */
56 1 View Code Duplication
    public function get(Context $context = null)
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...
57
    {
58 1
        if (is_null($context)) {
59
            $context = $this->getContext();
60
        }
61 1
        $locale = $this->getLanguage($context);
62 1
        if (!isset($this[$locale])) {
63
            if (!$context->isGraceful()) {
64
                throw new InvalidArgumentException(Message::NO_VALUE_FOR_LOCALE);
65
            }
66
            return new SuggestionCollection();
67
        }
68 1
        return $this->getAt($locale);
69
    }
70
}
71