CTCPAction::handleMessage()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: korvin
5
 * Date: 11/12/14
6
 * Time: 6:17 PM
7
 */
8
9
namespace Buttress\IRC\Action;
10
11
use Buttress\IRC\Connection\ConnectionInterface;
12
use Buttress\IRC\Message\GenericMessage;
13
use Buttress\IRC\Message\MessageInterface;
14
use Buttress\IRC\Message\PrivmsgMessage;
15
16
class CTCPAction extends AbstractAction
17
{
18
19
    protected $version;
20
21
    function __construct($version = 'buttress/irc v1.0.0')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        $this->version = $version;
24
    }
25
26
    /**
27
     * Handle messages
28
     *
29
     * @param MessageInterface $message
30
     * @return void
31
     */
32
    public function handleMessage(MessageInterface $message)
33
    {
34
        if ($message instanceof PrivmsgMessage) {
35
            $string = $message->getMessage();
36
            if (substr($string, 0, 1) === "\001") {
37
                $string = trim($string, " \001\n\t");
38
39
                list($type,) = array_pad(explode(' ', $string, 2), 2, '');
40
                list($nick, $user, $host) = $message->getUser();
41
42
                $message->getConnection()->log("CTCP \"{$type}\" from {$nick}!{$user}@{$host}", array($message));
43
                $this->handleCTCP($message, $type, $nick);
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param MessageInterface $message
50
     * @param string           $type
51
     * @param string           $nick
52
     */
53
    protected function handleCTCP($message, $type, $nick)
54
    {
55
        $params = array($nick);
56
        switch ($type) {
57
            case 'VERSION':
58
                $params[] = "\001VERSION {$this->version}\001";
59
                break;
60
            case 'PING':
61
                $params[] = $message->getMessage();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Buttress\IRC\Message\MessageInterface as the method getMessage() does only exist in the following implementations of said interface: Buttress\IRC\Message\PrivmsgMessage.

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...
62
                break;
63
            case 'TIME':
64
                $time = date(DATE_RFC1123);
65
                $params[] = "\001TIME {$time}\001";
66
                break;
67
        }
68
        if (count($params) > 1) {
69
            $response = new GenericMessage('notice', '', $params);
70
            $message->getConnection()->sendMessage($response);
71
        }
72
    }
73
74
}
75