Passed
Push — master ( 5e8abe...818453 )
by Jeroen
02:12 queued 13s
created

IrcMessage::parse()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 8
nop 1
dl 0
loc 29
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace Jerodev\PhpIrcClient\Messages;
4
5
use Jerodev\PhpIrcClient\Helpers\Event;
6
use Jerodev\PhpIrcClient\IrcClient;
7
8
class IrcMessage
9
{
10
    /** @var string */
11
    private $rawMessage;
12
13
    /** @var string */
14
    protected $command;
15
16
    /** @var string */
17
    protected $commandsuffix;
18
19
    /** @var bool */
20
    protected $handled;
21
22
    /** @var string */
23
    protected $payload;
24
25
    /** @var string */
26
    protected $source;
27
28
    public function __construct(string $command)
29
    {
30
        $this->handled = false;
31
        $this->parse($command);
32
    }
33
34
    /**
35
     *  This function is always called after the message is parsed.
36
     *  The handle will only be executed once unless forced.
37
     *
38
     *  @param IrcClient $client A reference to the irc client object
39
     *  @param bool $force Force handling this message even if already handled.
40
     */
41
    public function handle(IrcClient $client, bool $force = false): void
0 ignored issues
show
Unused Code introduced by
The parameter $client is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    public function handle(/** @scrutinizer ignore-unused */ IrcClient $client, bool $force = false): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        if ($this->handled && !$force) {
44
            return;
45
        }
46
    }
47
    
48
    /**
49
     *  Get the events that should be invoked for this message.
50
     *
51
     *  @return Event[]
52
     */
53
    public function getEvents(): array
54
    {
55
        return [];
56
    }
57
    
58
    /**
59
     *  Parse the irc command string to local properties
60
     *
61
     *  @param string $command
62
     */
63
    private function parse(string $command): void
64
    {
65
        $command = trim($command);
66
        $this->rawMessage = $command;
67
        $i = 0;
68
        
69
        if ($command[0] === ':') {
70
            $i = strpos($command, ' ');
71
            $this->source = substr($command, 1, $i - 1);
72
            
73
            $i++;
74
        }
75
        
76
        $j = strpos($command, ' ', $i);
77
        if (is_numeric($j)) {
0 ignored issues
show
introduced by
The condition is_numeric($j) is always true.
Loading history...
78
            $this->command = substr($command, $i, $j - $i);
79
        } else {
80
            $this->command = substr($command, $i);
81
            return;
82
        }
83
        
84
        $i = strpos($command, ':', $j);
85
        if (is_numeric($i)) {
0 ignored issues
show
introduced by
The condition is_numeric($i) is always true.
Loading history...
86
            if ($i !== $j + 1) {
87
                $this->commandsuffix = substr($command, $j + 1, $i - $j - 2);
88
            }
89
            $this->payload = substr($command, $i + 1);
90
        } else {
91
            $this->commandsuffix = substr($command, $j + 1);
92
        }
93
    }
94
}
95