MultiCharacterCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 77
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 4
A checkCommand() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Commands;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \addtogroup Commands
25
 * @{
26
 */
27
28
/** \class MultiCharacterCommand
29
 */
30
class MultiCharacterCommand extends BasicCommand
31
{
32
    /** @} */
33
34
    public static $type = 'message';
35
36
    public static $object_class = 'PhpBotFramework\Entities\Message';
37
38
    public static $priority = 1;
39
40
    private $command;
41
42
    private $length;
43
44
    /**
45
     * \brief Add a function that will be executed everytime a message contain the selected command
46
     * \details Use this syntax to create a command:
47
     *
48
     *     $help_command = new PhpBotFramework\Commands\MultiCharacterCommand("help",
49
     *         function ($bot, $message) {
50
     *             $bot->sendMessage("This is a help message.");
51
     *         }, '!', '.', '/'
52
     *     );
53
     *
54
     * Then you can add it to the bot's commands using <code>addCommand</code> method:
55
     *
56
     *     $bot->addCommand($help_command);
57
     *
58
     * @param string $command The command that will trigger this function (e.g. start)
59
     * @param callable $script The function that will be triggered by a command.
60
     * Must take an object(the bot) and an array(the message received).
61
     */
62
    public function __construct(string $command, callable $script, string ...$chars)
63
    {
64
        $chars_count = count($chars);
65
        if ( $chars_count === 0)
66
        {
67
            throw new BotException("No character given for matching the command");
68
        }
69
70
        if ($chars_count === 1)
71
        {
72
            // Build a regex using the only character
73
            $this->regex_rule = $chars[0] . $command;
0 ignored issues
show
Bug introduced by
The property regex_rule does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
        }
75
        // Build regex including all characters
76
        // Eg: (!|.)command
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
        else
78
        {
79
            $this->regex_rule = '(' . $chars[0];
80
            foreach($chars as $char)
81
            {
82
                $this->regex_rule .= '|' . $char;
83
            }
84
            $this->regex_rule .= ')' . $command;
85
        }
86
87
        $this->script = $script;
88
    }
89
90
    /**
91
     * @internal
92
     * \brief Process a message checking if it trigger any MessageCommand.
93
     * @param string $message Message to process.
94
     * @return bool True if the message trigger any command.
95
     */
96
    public function checkCommand(array $message) : bool
97
    {
98
        // Check the regex
99
        if (preg_match("/{$this->regex_rule}/", $message['text'])) {
100
                    // Return
101
                    return true;
102
        }
103
104
        return false;
105
    }
106
}
107