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

ProductParser::parseArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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