PurchaseRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 3
dl 0
loc 78
ccs 43
cts 45
cp 0.9556
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEndpoint() 0 4 1
A sendData() 0 8 1
B getData() 0 51 4
1
<?php
2
3
namespace Omnipay\Emp\Message;
4
5
/**
6
 * @author    Ivan Kerin <[email protected]>
7
 * @copyright 2014, Clippings Ltd.
8
 * @license   http://spdx.org/licenses/BSD-3-Clause
9
 */
10
class PurchaseRequest extends AbstractRequest
11
{
12
    /**
13
     * @return string
14
     */
15 1
    public function getEndpoint()
16
    {
17 1
        return 'https://my.emerchantpay.com/service/order/submit';
18
    }
19
20
    /**
21
     * @param  mixed $data
22
     * @return \Omnipay\Emp\Message\PurchaseResponse
23
     */
24 1
    public function sendData($data)
25
    {
26 1
        $responseData = parent::sendData($data);
27
28 1
        $this->response = new PurchaseResponse($this, $responseData);
29
30 1
        return $this->response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->response; (Omnipay\Emp\Message\PurchaseResponse) is incompatible with the return type of the parent method Omnipay\Emp\Message\AbstractRequest::sendData of type array.

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...
31
    }
32
33
    /**
34
     * @return array
35
     */
36 1
    public function getData()
37
    {
38 1
        $data = parent::getData();
39
40 1
        $this->validate('transactionReference', 'currency', 'clientIp', 'card', 'items');
41
42 1
        $card = $this->getCard();
43 1
        $items = $this->getItems();
44 1
        $currency = $this->getCurrency();
45
46 1
        $data['customer_first_name']    = $card->getFirstName();
47 1
        $data['customer_last_name']     = $card->getLastName();
48 1
        $data['customer_address']       = $card->getAddress1();
49 1
        $data['customer_address2']      = $card->getAddress2();
50 1
        $data['customer_city']          = $card->getCity();
51 1
        $data['customer_country']       = $card->getCountry();
52 1
        $data['customer_postcode']      = $card->getPostcode();
53 1
        $data['customer_email']         = $card->getEmail();
54 1
        $data['customer_phone']         = $card->getPhone();
55
56 1
        $data['card_holder_name']       = $card->getName();
57 1
        $data['card_number']            = $card->getNumber();
58 1
        $data['exp_month']              = str_pad($card->getExpiryMonth(), 2, '0', STR_PAD_LEFT);
59 1
        $data['exp_year']               = substr($card->getExpiryYear(), 2);
60 1
        $data['cvv']                    = $card->getCvv();
61
62 1
        $data['order_reference']        = $this->getTransactionReference();
63 1
        $data['order_currency']         = $currency;
64 1
        $data['payment_method']         = 'creditcard';
65 1
        $data['credit_card_trans_type'] = 'sale';
66 1
        $data['ip_address']             = $this->getClientIp();
67
68 1
        foreach ($items as $index => $item) {
0 ignored issues
show
Bug introduced by
The expression $items of type object<Omnipay\Common\ItemBag>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
69
70 1
            $i = $index + 1;
71
72 1
            $data["item_{$i}_predefined"]             = '0';
73 1
            $data["item_{$i}_digital"]                = '0';
74 1
            $data["item_{$i}_code"]                   = $item->getName();
75 1
            $data["item_{$i}_qty"]                    = $item->getQuantity();
76 1
            $data["item_{$i}_discount"]               = $item->getPrice() < 0 ? '1' : '0';
77 1
            $data["item_{$i}_name"]                   = $item->getDescription();
78 1
            $data["item_{$i}_unit_price_{$currency}"] = $item->getPrice();
79 1
        }
80
81 1
        if ($this->getThreatmetrix()) {
82
            $data['thm_session_id'] = $this->getThreatmetrix()->getSessionId();
83
        }
84
85 1
        return $data;
86
    }
87
}
88