Completed
Pull Request — master (#7)
by Rafael
03:49
created

PostPayment   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 96
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A assign() 0 14 1
A getCustomerMapping() 0 5 2
A setCustomerMapping() 0 5 1
A getAddressMapping() 0 5 2
A setAddressMapping() 0 5 1
A getItemMapping() 0 5 2
A setItemMapping() 0 5 1
A map() 0 15 2
1
<?php
2
3
namespace Iris\Mapping;
4
5
class PostPayment extends Base
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10
    public function assign(array $externalData)
11
    {
12
        $order = new \Iris\Transfer\Sales\Order(array(
13
            'grand_total'       => ($externalData['total_amount'] + $externalData['freight_amount']),
14
            'created_at'        => date('Y-m-d H:i:s', strtotime($externalData['purchase_date'])),
15
            'shipping_amount'   => $externalData['freight_amount'],
16
            'address'           => $this->getAddressMapping()->assign($externalData['customer'], true),
0 ignored issues
show
Unused Code introduced by
The call to OrderAddress::assign() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
17
            'item_collection'   => $this->getItemMapping()->assign($externalData),
18
            'freight_cost'      => $externalData['freight_cost'],
19
            'customer'          => $this->getCustomerMapping()->assign($externalData['customer'])
20
        ));
21
22
        return $order;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $order; (Iris\Transfer\Sales\Order) 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
     * @return \Iris\Mapping\OrderCustomer
27
     */
28
    public function getCustomerMapping()
29
    {
30
        $this->customerMapping || $this->setCustomerMapping(OrderCustomer::getInstance());
0 ignored issues
show
Bug introduced by
The property customerMapping does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        return $this->customerMapping;
32
    }
33
34
    /**
35
     * @param \Iris\Mapping\OrderCustomer $map
36
     * @return \Iris\Mapping\Order
37
     */
38
    public function setCustomerMapping(OrderCustomer $map)
39
    {
40
        $this->customerMapping = $map;
41
        return $this;
42
    }
43
44
    /**
45
     * @return \Iris\Mapping\OrderAddress
46
     */
47
    public function getAddressMapping()
48
    {
49
        $this->addressMapping || $this->setAddressMapping(OrderAddress::getInstance());
0 ignored issues
show
Bug introduced by
The property addressMapping does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
        return $this->addressMapping;
51
    }
52
53
    /**
54
     * @param \Iris\Mapping\OrderAddress $map
55
     * @return \Iris\Mapping\Order
56
     */
57
    public function setAddressMapping(OrderAddress $map)
58
    {
59
        $this->addressMapping = $map;
60
        return $this;
61
    }
62
63
    /**
64
     * @return \Iris\Mapping\OrderItem
65
     */
66
    public function getItemMapping()
67
    {
68
        $this->itemMapping || $this->setItemMapping(OrderItem::getInstance());
0 ignored issues
show
Bug introduced by
The property itemMapping does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
        return $this->itemMapping;
70
    }
71
72
    /**
73
     * @param \Iris\Mapping\OrderItem $map
74
     * @return \Iris\Mapping\Order
75
     */
76
    public function setItemMapping(OrderItem $map)
77
    {
78
        $this->itemMapping = $map;
79
        return $this;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function map($internalData)
86
    {
87
        $externalData = array();
88
        $externalData['marketplaceOrderId'] = $internalData->getOrderNr();
89
90
        $externalData['items'] = array();
91
        foreach ($internalData->getItemCollection() as $salesOrderItem) {
92
            $externalData['items'][] = $this->getItemMapping()->map($salesOrderItem);
93
        }
94
95
        $externalData['clientProfileData'] = $this->getCustomerMapping()->map($internalData);
96
        $externalData['shippingData'] = $this->getAddressMapping()->map($internalData);
97
98
        return $externalData;
99
    }
100
}
101