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

ProductParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseArray() 0 10 2
B parse() 0 18 12
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