Passed
Push — master ( 9ff768...b736eb )
by Laurens
02:33
created

ProductParser::parse()   B

Complexity

Conditions 12
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 7.4259
c 0
b 0
f 0
cc 12
eloc 14
nc 1
nop 2
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client\Purchase;
6
7
use LauLamanApps\IzettleApi\API\Image;
8
use LauLamanApps\IzettleApi\API\Purchase\Product;
9
use Money\Currency;
10
use Money\Money;
11
use Ramsey\Uuid\Uuid;
12
13
final class ProductParser
14
{
15
    /**
16
     * @return Product[]
17
     */
18 3
    public static function parseArray(array $products, Currency $currency): array
19
    {
20 3
        $data = [];
21
22 3
        foreach ($products as $product) {
23 1
            $data[] = self::parse($product, $currency);
24
        }
25
26 3
        return $data;
27
    }
28
29 1
    private static function parse(array $product, Currency $currency): Product
30
    {
31 1
        extract($product);
32
33 1
        return new Product(
34 1
            isset($productUuid) ? Uuid::fromString($productUuid) : null,
35 1
            isset($variantUuid) ? Uuid::fromString($variantUuid) : null,
36 1
            isset($name) ? $name : null,
37 1
            isset($variantName) ? $variantName : null,
38 1
            isset($quantity) ? (int) $quantity : null,
39 1
            isset($unitPrice) ? new Money($unitPrice, $currency) : null,
0 ignored issues
show
Bug introduced by
It seems like isset($unitPrice) ? new ...rice, $currency) : null can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40 1
            isset($vatPercentage) ? $vatPercentage : null,
41 1
            isset($rowTaxableAmount) ? new Money($rowTaxableAmount, $currency) : null,
0 ignored issues
show
Bug introduced by
It seems like isset($rowTaxableAmount)...ount, $currency) : null can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42 1
            isset($imageLookupKey) ? new Image($imageLookupKey) : null,
43 1
            isset($autoGenerated) ? $autoGenerated : false,
44 1
            isset($libraryProduct) ? $libraryProduct : false
45
        );
46
    }
47
}
48