OrderItem   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 6
c 4
b 1
f 1
lcom 0
cbo 3
dl 0
loc 57
ccs 30
cts 30
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assign() 0 14 3
A map() 0 13 1
A createSalesOrderItem() 0 19 2
1
<?php
2
3
namespace Iris\Mapping;
4
5
class OrderItem extends Base
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10 2
    public function assign(array $externalData)
11
    {
12 2
        if (!$externalData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $externalData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
13 1
            throw new \Exception('No order items sent.');
14
        }
15
16 1
        $itemCollection = new \Iris\Transfer\Sales\Order\ItemCollection();
17
18 1
        foreach ($externalData['order_items'] as $orderItem) {
19 1
            $itemCollection[] = $this->createSalesOrderItem($orderItem, $externalData);
20 1
        }
21
22 1
        return $itemCollection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $itemCollection; (Iris\Transfer\Sales\Order\ItemCollection) is incompatible with the return type declared by the abstract method Iris\Mapping\Base::assign of type Iris\Transfer\Catalog\ConfigCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function map($internalData)
29
    {
30
        $externalData = array(
31 1
            'id'                => $internalData->getIdSalesOrderItem(),
32 1
            'sku'               => $internalData->getSku(),
33 1
            'sku_supplier'      => $internalData->getSkuSupplierSimple(),
34 1
            'quantity'          => $internalData->getQuantity(),
35 1
            'price'             => $internalData->getPaidPrice(),
36 1
            'taxable_price'     => $internalData->getTaxablePrice()
37 1
        );
38
39 1
        return $externalData;
40
    }
41
42 1
    private function createSalesOrderItem($orderItem, $externalData)
43
    {
44 1
        $shippingAmount = ($externalData['freight_amount'] > 0)
45 1
            ? ($externalData['freight_amount'] / count($externalData['order_items']))
46 1
            : $externalData['freight_amount'];
47
48
        $data = array(
49 1
            'sku'                                   => $orderItem['sku'],
50 1
            'unit_price'                            => $orderItem['price'],
51 1
            'paid_price'                            => $orderItem['price'],
52 1
            'carrier_id'                            => $orderItem['carrier_id'],
53 1
            'shipment_carrier_delivery_time'        => $orderItem['carrier_delivery_time'],
54 1
            'shipment_warehouse_processing_time'    => $orderItem['warehouse_time'],
55 1
            'shipment_total_delivery_time'          => $orderItem['freight_time'],
56
            'shipping_amount'                       => $shippingAmount
57 1
        );
58
59 1
        return new \Iris\Transfer\Sales\Order\Item($data);
60
    }
61
}
62