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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 7
loc 7
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
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