Completed
Push — master ( 1563e2...9d8e98 )
by Marcel
09:03
created

ProxyDriver::typesAndWaits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace BotMan\BotMan\Drivers\Tests;
4
5
use BotMan\BotMan\Http\Curl;
6
use BotMan\BotMan\Drivers\NullDriver;
7
use BotMan\BotMan\Interfaces\DriverInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
10
11
/**
12
 * A driver that acts as a proxy for a global driver instance. Useful for mock/fake drivers in integration tests.
13
 */
14
final class ProxyDriver implements DriverInterface
15
{
16
    /**
17
     * @var DriverInterface
18
     */
19
    private static $instance;
20
21
    /**
22
     * Set driver instance to be used.
23
     *
24
     * @param DriverInterface $driver
25
     */
26
    public static function setInstance(DriverInterface $driver)
27
    {
28
        self::$instance = $driver;
29
    }
30
31
    /**
32
     * @return DriverInterface
33
     */
34
    private static function instance()
35
    {
36
        if (self::$instance === null) {
37
            self::$instance = new NullDriver(new Request, [], new Curl);
38
        }
39
40
        return self::$instance;
41
    }
42
43
    public function matchesRequest()
44
    {
45
        return self::instance()->matchesRequest();
46
    }
47
48
    public function hasMatchingEvent()
49
    {
50
        return self::instance()->hasMatchingEvent();
51
    }
52
53
    public function getMessages()
54
    {
55
        return self::instance()->getMessages();
56
    }
57
58
    public function isBot()
59
    {
60
        return self::instance()->isBot();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface BotMan\BotMan\Interfaces\DriverInterface as the method isBot() does only exist in the following implementations of said interface: BotMan\BotMan\Drivers\NullDriver, BotMan\BotMan\Drivers\Tests\FakeDriver, BotMan\BotMan\Drivers\Tests\ProxyDriver.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
    }
62
63
    public function isConfigured()
64
    {
65
        return self::instance()->isConfigured();
66
    }
67
68
    public function getUser(IncomingMessage $matchingMessage)
69
    {
70
        return self::instance()->getUser($matchingMessage);
71
    }
72
73
    public function getConversationAnswer(IncomingMessage $message)
74
    {
75
        return self::instance()->getConversationAnswer($message);
76
    }
77
78
    public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
79
    {
80
        return self::instance()->buildServicePayload($message, $matchingMessage, $additionalParameters);
81
    }
82
83
    public function sendPayload($payload)
84
    {
85
        return self::instance()->sendPayload($payload);
86
    }
87
88
    public function getName()
89
    {
90
        return self::instance()->getName();
91
    }
92
93
    public function types(IncomingMessage $matchingMessage)
94
    {
95
        return self::instance()->types($matchingMessage);
96
    }
97
98
    /**
99
     * Tells if the stored conversation callbacks are serialized.
100
     *
101
     * @return bool
102
     */
103
    public function serializesCallbacks()
104
    {
105
        return self::instance()->serializesCallbacks();
106
    }
107
108
    /**
109
     * Send a typing indicator and wait for the given amount of seconds.
110
     * @param IncomingMessage $matchingMessage
111
     * @param int $seconds
112
     * @return mixed
113
     */
114
    public function typesAndWaits(IncomingMessage $matchingMessage, int $seconds)
115
    {
116
        return self::instance()->typesAndWaits($matchingMessage, $seconds);
117
    }
118
}
119