Passed
Push — develop ( 09057a...80c72e )
by Jens
12:16
created

CartSetCustomerGroupAction::ofCustomerGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Request\Carts\Command;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
10
use Commercetools\Core\Request\AbstractAction;
11
12
/**
13
 * @package Commercetools\Core\Request\Carts\Command
14
 *
15
 * @method string getAction()
16
 * @method CartSetCustomerGroupAction setAction(string $action = null)
17
 * @method CustomerGroupReference getCustomerGroup()
18
 * @method CartSetCustomerGroupAction setCustomerGroup(CustomerGroupReference $customerGroup = null)
19
 */
20 View Code Duplication
class CartSetCustomerGroupAction extends AbstractAction
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22 1
    public function fieldDefinitions()
23
    {
24
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('action' =>...roupReference::class)); (array<string,array>) is incompatible with the return type of the parent method Commercetools\Core\Reque...ction::fieldDefinitions of type array<string,array<string,string>>.

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...
25 1
            'action' => [static::TYPE => 'string'],
26 1
            'customerGroup' => [static::TYPE => CustomerGroupReference::class],
27
        ];
28
    }
29
30
    /**
31
     * @param string $customerGroup
32
     * @param Context|callable $context
33
     * @return CartSetCustomerGroupAction
34
     */
35
    public static function ofCustomerGroup($customerGroup, $context = null)
36
    {
37
        return static::of($context)->setCustomerGroup($customerGroup);
0 ignored issues
show
Documentation introduced by
$customerGroup is of type string, but the function expects a object<Commercetools\Cor...CustomerGroupReference>.

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...
38
    }
39
40
    /**
41
     * @param array $data
42
     * @param Context|callable $context
43
     */
44 1
    public function __construct(array $data = [], $context = null)
45
    {
46 1
        parent::__construct($data, $context);
47 1
        $this->setAction('setCustomerGroup');
48 1
    }
49
}
50