GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 64a210...2d6991 )
by Stan
02:42
created

Command::getArgsFromText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Teebot\Entity;
4
5
class Command extends AbstractEntity
6
{
7
    const ENTITY_TYPE             = 'Command';
8
9
    const PREFIX          = '/';
10
11
    const ARGS_SEPARATOR  = ' ';
12
13
    const PARTS_DELIMITER = '_';
14
15
    const PATTERN = '/^\/[a-zA-Z_]+.*$/';
16
17
    protected $text;
18
19
    protected $name;
20
21
    protected $args;
22
23
    public function __construct(array $data)
24
    {
25
        $command = null;
0 ignored issues
show
Unused Code introduced by
$command 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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $args    = null;
27
28
        if (isset($data['text'])) {
29
            $command = $this->getCommandFromText($data['text']);
30
31
            if ($command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $command of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32
                $args = $this->getArgsFromText($command, $data['text']);
33
            }
34
35
            $data = [
36
                'name' => $command,
37
                'args' => $args
38
            ];
39
        }
40
41
        parent::__construct($data);
42
    }
43
44
    /**
45
     * @return mixed
46
     */
47
    public function getName()
48
    {
49
        return $this->name;
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function getArgs()
56
    {
57
        return $this->args;
58
    }
59
60
    protected function getCommandFromText($text)
61
    {
62
        $command = null;
63
64
        if (!is_string($text) || !strlen($text) || $text[0] !== static::PREFIX) {
65
            return $command;
66
        }
67
68
        if (strpos($text, static::ARGS_SEPARATOR) !== false) {
69
            $parts = explode(static::ARGS_SEPARATOR, $text);
70
71
            $text = trim($parts[0]);
72
        }
73
74
        $command = trim(substr($text, 1, strlen($text) - 1));
75
76
        return $command;
77
    }
78
79
    protected function getArgsFromText($command, $text)
80
    {
81
        $length = strlen(static::PREFIX . $command);
82
83
        $argString = trim(substr($text, $length));
84
85
        if (strlen($argString)) {
86
            return $argString;
87
        }
88
89
        return null;
90
    }
91
}
92