Completed
Push — master ( 92f2fd...10a92d )
by Paweł
20s
created

PutOptionBasedConfigurableItemToCartRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 48
loc 48
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A fromArray() 4 4 1
A fromRequest() 4 4 1
A getCommand() 4 4 1

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 Sylius\ShopApiPlugin\Request;
4
5
use Sylius\ShopApiPlugin\Command\PutOptionBasedConfigurableItemToCart;
6
use Symfony\Component\HttpFoundation\Request;
7
8 View Code Duplication
final class PutOptionBasedConfigurableItemToCartRequest
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...
9
{
10
    /**
11
     * @var string
12
     */
13
    private $token;
14
15
    /**
16
     * @var string
17
     */
18
    private $productCode;
19
20
    /**
21
     * @var array|null
22
     */
23
    private $options;
24
25
    /**
26
     * @var int
27
     */
28
    private $quantity;
29
30
    private function __construct($token, $productCode, $options, $quantity)
31
    {
32
        $this->token = $token;
33
        $this->productCode = $productCode;
34
        $this->options = $options;
35
        $this->quantity = $quantity;
36
    }
37
38
    public static function fromArray(array $item)
39
    {
40
        return new self($item['token'] ?? null, $item['productCode'] ?? null, $item['options'] ?? null, $item['quantity'] ?? null);
41
    }
42
43
    public static function fromRequest(Request $request)
44
    {
45
        return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('options'), $request->request->get('quantity'));
46
    }
47
48
    /**
49
     * @return PutOptionBasedConfigurableItemToCart
50
     */
51
    public function getCommand()
52
    {
53
        return new PutOptionBasedConfigurableItemToCart($this->token, $this->productCode, $this->options, $this->quantity);
0 ignored issues
show
Bug introduced by
It seems like $this->options can also be of type null; however, Sylius\ShopApiPlugin\Com...emToCart::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
    }
55
}
56