Completed
Push — master ( e8b86c...279d13 )
by Sullivan
02:12
created

AbstractRequest::getParameters()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 16
nop 0
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
     * @param int $activity
29
     *
30
     * @return $this
31
     */
32
    public function setActivity($activity)
33
    {
34
        $this->activity = $activity;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param \DateTime|null $date
41
     *
42
     * @return $this
43
     */
44
    final public function setDate(\DateTime $date = null)
45
    {
46
        $this->date = $date;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param bool $showCountry
53
     */
54
    public function setShowCountry($showCountry)
55
    {
56
        $this->showCountry = $showCountry;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getParameters()
63
    {
64
        $parameters = [
65
            'ACTIVITE' => $this->activity,
66
            'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null,
67
        ];
68
69
        if (true === $this->showCountry) {
70
            $parameters['PAYS'] = '';
71
        }
72
73
        if (method_exists($this, 'getTransactionNumber')) {
74
            $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\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...
75
        }
76
        if (method_exists($this, 'getCallNumber')) {
77
            $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\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...
78
        }
79
80
        return $parameters;
81
    }
82
}
83