These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | |||
3 | use Teebot\Exception\Fatal; |
||
4 | use Teebot\Exception\Output; |
||
5 | |||
6 | class BotMother |
||
0 ignored issues
–
show
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.
You can fix this by adding a namespace to your class: namespace YourVendor;
class YourClass { }
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries. ![]() |
|||
7 | { |
||
8 | protected $cliOptions = [ |
||
9 | 'short' => 'r:b:c:t:d:h:', |
||
10 | 'long' => ['run', 'bot', 'command', 'token', 'dir', 'help'] |
||
11 | ]; |
||
12 | |||
13 | protected $commandMethodMapper = [ |
||
14 | 'create-bot' => ['commandBotCreate', ['b:bot', 't:token', 'd:dir']], |
||
15 | 'help' => ['help', []], |
||
16 | ]; |
||
17 | |||
18 | public function __construct() |
||
19 | { |
||
20 | $opts = getopt($this->cliOptions['short'], $this->cliOptions['long']); |
||
21 | |||
22 | $help = $opts['h'] ?? $opts['help'] ?? null; |
||
23 | |||
24 | if ($help || empty($opts)) { |
||
25 | $this->help(); |
||
26 | |||
27 | exit; |
||
28 | } |
||
29 | |||
30 | $command = $opts['r'] ?? $opts['run'] ?? null; |
||
31 | |||
32 | try { |
||
33 | if (!$command) { |
||
34 | throw new Fatal('Command not specified!'); |
||
35 | } |
||
36 | |||
37 | $method = $this->getMethod($command); |
||
38 | |||
39 | if (!$method) { |
||
40 | throw new Fatal('Unknown command!'); |
||
41 | } |
||
42 | |||
43 | $args = $this->getMethodArgs($method); |
||
0 ignored issues
–
show
Are you sure the assignment to
$args is correct as $this->getMethodArgs($method) (which targets BotMother::getMethodArgs() ) seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() $args is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
44 | |||
45 | call_user_func_array([$this, $method], ['1', '2', '3']); |
||
46 | } catch (Fatal $e) { |
||
47 | Output::log($e); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | protected function help() |
||
52 | { |
||
53 | echo " |
||
54 | Bot mother CLI utility will help you easily and quickly create Bot or Command for existing bot. Please use |
||
55 | the following arguments: |
||
56 | |||
57 | -r, --run [command_name] : Command name - required parameter. Could have the following values: create-bot, create-command |
||
58 | -b, --bot [bot_name] : The name of the bot that you want to create |
||
59 | -c, --command [name] : The name of the command that you want to create |
||
60 | -t, --token [value] : Token for the bot creation |
||
61 | -d, --dir [dir] : Bot's root directory (absolute path)\n\n"; |
||
62 | } |
||
63 | |||
64 | protected function getMethod($command) |
||
65 | { |
||
66 | $method = $this->commandMethodMapper[$command] ?? null; |
||
67 | |||
68 | if (!$method || !method_exists($this, $method)) { |
||
69 | return null; |
||
70 | } |
||
71 | |||
72 | return $method; |
||
73 | } |
||
74 | |||
75 | protected function getMethodArgs($methodName) |
||
0 ignored issues
–
show
|
|||
76 | { |
||
77 | } |
||
78 | |||
79 | protected function commandBotCreate($botName, $token, $dir) |
||
80 | { |
||
81 | echo "\nCreating a new bot.\n"; |
||
82 | |||
83 | echo $botName . " - " . $token . " - " . $dir; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | define('ROOT_DIR', realpath(__DIR__)); |
||
88 | require_once ROOT_DIR . '/vendor/autoload.php'; |
||
89 | |||
90 | $BotMother = new BotMother(); |
||
91 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.