Completed
Pull Request — master (#32)
by Sullivan
03:15
created

AbstractRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setActivity() 0 6 1
A setDate() 0 6 1
A setShowCountry() 0 6 1
A setShowSha1() 0 6 1
A getSubscriberRef() 0 4 1
C getParameters() 0 28 7
1
<?php
2
3
namespace Nexy\PayboxDirect\Request;
4
5
use Nexy\PayboxDirect\Enum\Activity;
6
7
/**
8
 * @author Sullivan Senechal <[email protected]>
9
 */
10
abstract class AbstractRequest implements RequestInterface
11
{
12
    /**
13
     * @var int
14
     */
15
    private $activity = Activity::WEB_REQUEST;
16
17
    /**
18
     * @var \DateTime
19
     */
20
    private $date = null;
21
22
    /**
23
     * @var bool
24
     */
25
    private $showCountry = false;
26
27
    /**
28
     * @var bool
29
     */
30
    private $showSha1 = false;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $subscriberRef = null;
36
37
    /**
38
     * @param null|string $subscriberRef
39
     */
40
    public function __construct($subscriberRef = null)
41
    {
42
        $this->subscriberRef = $subscriberRef;
43
    }
44
45
    /**
46
     * @param int $activity
47
     *
48
     * @return $this
49
     */
50
    final public function setActivity($activity)
51
    {
52
        $this->activity = $activity;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param \DateTime|null $date
59
     *
60
     * @return $this
61
     */
62
    final public function setDate(\DateTime $date = null)
63
    {
64
        $this->date = $date;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param bool $showCountry
71
     *
72
     * @return $this
73
     */
74
    final public function setShowCountry($showCountry)
75
    {
76
        $this->showCountry = $showCountry;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param bool $showSha1
83
     *
84
     * @return $this
85
     */
86
    final public function setShowSha1($showSha1)
87
    {
88
        $this->showSha1 = $showSha1;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return null|string
95
     */
96
    final protected function getSubscriberRef()
97
    {
98
        return $this->subscriberRef;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getParameters()
105
    {
106
        $parameters = [
107
            'ACTIVITE' => $this->activity,
108
            'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null,
109
        ];
110
111
        if ($this->showCountry) {
112
            $parameters['PAYS'] = '';
113
        }
114
        if ($this->showSha1) {
115
            $parameters['SHA-1'] = '';
116
        }
117
118
        if (method_exists($this, 'getTransactionNumber')) {
119
            $parameters['NUMTRANS'] = $this->getTransactionNumber();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nexy\PayboxDirect\Request\AbstractRequest as the method getTransactionNumber() does only exist in the following sub-classes of Nexy\PayboxDirect\Request\AbstractRequest: Nexy\PayboxDirect\Reques...encedTransactionRequest, Nexy\PayboxDirect\Reques...beredTransactionRequest, Nexy\PayboxDirect\Request\CancelRequest, Nexy\PayboxDirect\Request\DebitRequest, Nexy\PayboxDirect\Request\InquiryRequest, Nexy\PayboxDirect\Request\RefundRequest, Nexy\PayboxDirect\Reques...ancelTransactionRequest, Nexy\PayboxDirect\Request\UpdateAmountRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
120
        }
121
        if (method_exists($this, 'getCallNumber')) {
122
            $parameters['NUMAPPEL'] = $this->getCallNumber();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nexy\PayboxDirect\Request\AbstractRequest as the method getCallNumber() does only exist in the following sub-classes of Nexy\PayboxDirect\Request\AbstractRequest: Nexy\PayboxDirect\Reques...encedTransactionRequest, Nexy\PayboxDirect\Reques...beredTransactionRequest, Nexy\PayboxDirect\Request\CancelRequest, Nexy\PayboxDirect\Request\DebitRequest, Nexy\PayboxDirect\Request\InquiryRequest, Nexy\PayboxDirect\Request\RefundRequest, Nexy\PayboxDirect\Reques...ancelTransactionRequest, Nexy\PayboxDirect\Request\UpdateAmountRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
123
        }
124
125
        // Direct Plus requests special case.
126
        if (null !== $this->getSubscriberRef()) {
127
            $parameters['REFABONNE'] = $this->getSubscriberRef();
128
        }
129
130
        return $parameters;
131
    }
132
}
133