Completed
Push — master ( ea85f7...2cf7d5 )
by Leith
08:27
created

HasCardFields   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCardNumber() 0 4 1
A getCardExpiryYear() 0 5 1
A getCardExpiryMonth() 0 5 1
A getCardholderName() 0 4 1
A getCardType() 0 8 2
A getResponseField() 0 8 4
A convertExpiryDate() 0 8 2
1
<?php
2
3
namespace Omnipay\Paystation\Message;
4
5
/**
6
 * Provide additional information to look up card fields on a response object
7
 */
8
trait HasCardFields
9
{
10
    /** @var string|null  The property of $this->data to look up response fields by, or null if directly on $this->data */
11
    protected $lookupField;
12
13
    public function getCardNumber()
14
    {
15
        return $this->getResponseField('CardNo');
16
    }
17
18
    public function getCardExpiryYear()
19
    {
20
        $expiry = $this->convertExpiryDate($this->getResponseField('CardExpiry'));
21
        return $expiry[0];
22
    }
23
24
    public function getCardExpiryMonth()
25
    {
26
        $expiry = $this->convertExpiryDate($this->getResponseField('CardExpiry'));
27
        return $expiry[1];
28
    }
29
30
    public function getCardholderName()
31
    {
32
        return $this->getResponseField('CardholderName');
33
    }
34
35
    public function getCardType()
36
    {
37
        $type = $this->getResponseField('CardType');
38
        if ($type === null) {
39
            $type = $this->getResponseField('Cardtype');
40
        }
41
        return $type;
42
    }
43
44
    protected function getResponseField($key)
45
    {
46
        if (!$this->isSuccessful()) {
0 ignored issues
show
Bug introduced by
It seems like isSuccessful() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
47
            return null;
48
        }
49
        $lookup = $this->lookupField === null ? $this->data : $this->data->{$this->lookupField};
1 ignored issue
show
Bug introduced by
The property data 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 isset($lookup->$key) ? (string) $lookup->$key : null;
51
    }
52
53
    protected function convertExpiryDate($yymm)
54
    {
55
        if (preg_match('/([0-9]{2})([0-9]{2})/', $yymm, $match)) {
56
            return array((int) $match[1], (int) $match[2]);
57
        } else {
58
            return array(null, null);
59
        }
60
    }
61
}
62