Function parse
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function parse()
{
$op = [
'and' => ['op', 'and'],
'or' => ['op', 'or'],
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Missing class import via use statement (line '53', column '15'). Open
throw new VTParseFailed();
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
The method getTokens uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$tokenType = 'sym';
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
The method parse uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$left = $this->cond();
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Reference to undeclared property \VTConditionalParser->pos
Open
$this->pos = 0;
- Exclude checks
Reference to undeclared property \VTConditionalParser->pos
Open
return $this->tokens[$this->pos];
- Exclude checks
Reference to undeclared property \VTConditionalParser->tokens
Open
return $this->tokens[$this->pos];
- Exclude checks
Reference to undeclared property \VTConditionalParser->tokens
Open
$this->tokens = $this->getTokens($expr);
- Exclude checks
Reference to undeclared property \VTConditionalParser->pos
Open
++$this->pos;
- Exclude checks
Reference to undeclared property \VTConditionalParser->pos
Open
return $this->tokens[$this->pos - 1];
- Exclude checks
Reference to undeclared property \VTConditionalParser->pos
Open
if (\count($this->tokens) > $this->pos && \in_array($this->peek(), [$op['and'], $op['or']])) {
- Exclude checks
Reference to undeclared property \VTConditionalParser->tokens
Open
if (\count($this->tokens) > $this->pos && \in_array($this->peek(), [$op['and'], $op['or']])) {
- Exclude checks
Reference to undeclared property \VTConditionalParser->tokens
Open
return $this->tokens[$this->pos - 1];
- Exclude checks
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class VTConditionalParser
- Exclude checks
Avoid variables with short names like $nt. Configured minimum length is 3. Open
$nt = $this->nextToken();
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Avoid variables with short names like $op. Configured minimum length is 3. Open
$op = [
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->tokens = $this->getTokens($expr);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokens = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function getTokens($expression)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenVal = $arr[0];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array($tokenVal, ['and', 'or', '=', '(', ')'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function __construct($expr)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->pos = 0;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($matches as $arr) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
preg_match_all('/and|or|\\d+|=|\\w+|\\(|\\)/', $expression, $matches, PREG_SET_ORDER);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$left = $this->cond();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\count($this->tokens) > $this->pos && \in_array($this->peek(), [$op['and'], $op['or']])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function nextToken()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($this->peek() == $op['(']) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $left;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$operator = $this->nextToken();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function cond()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function parse()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'and' => ['op', 'and'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$nt = $this->nextToken();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return [$nt[1], $left, $this->parse()];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokens[] = [$tokenType, $tokenVal];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$left = $this->nextToken();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (is_numeric($tokenVal)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenType = 'num';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'=' => ['op', '='],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'(' => ['op', '('],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->tokens[$this->pos];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$left = $this->parse();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
++$this->pos;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$op = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenType = 'op';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->nextToken();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($this->nextToken() != $op[')']) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return [$operator[1], $left, $right];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenType = 'sym';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $tokens;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function peek()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->tokens[$this->pos - 1];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'or' => ['op', 'or'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
')' => ['op', ')'], ];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
throw new VTParseFailed();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$right = $this->nextToken();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks