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

LocalizedSuggestionCollection::get()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 5.4042

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 14
loc 14
ccs 5
cts 9
cp 0.5556
rs 9.2
cc 4
eloc 9
nc 6
nop 1
crap 5.4042
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