Completed
Push — master ( fa15a1...41f4c4 )
by Дмитрий
14:15
created

UseCast::visitPhpFunctionCall()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
ccs 0
cts 12
cp 0
rs 6.7272
cc 7
eloc 11
nc 6
nop 2
crap 56
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\FunctionCall;
7
8
use PhpParser\Node\Expr\FuncCall;
9
use PhpParser\Node\Name;
10
use PHPSA\Context;
11
12
class UseCast implements PassFunctionCallInterface
13
{
14
    protected $map = array(
15
        'boolval' => 'bool',
16
        'intval' => 'int',
17
        'floatval' => 'double',
18
        'doubleval' => 'double',
19
        'strval' => 'string'
20
    );
21
22
    public function visitPhpFunctionCall(FuncCall $funcCall, Context $context)
23
    {
24
        $name = false;
25
26
        if ($funcCall->name instanceof Name && !$funcCall->name->isFullyQualified()) {
27
            $name = $funcCall->name->getFirst();
28
        }
29
30
        if ($name && isset($this->map[$name])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false 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...
31
            /**
32
             * Exclusion via intval with 2 args intval($number, int $base = 10);
33
             */
34
            if ($name == 'intval' && count($funcCall->args) > 1) {
35
                return;
36
            }
37
38
            $context->notice(
39
                'fcall.cast',
40
                sprintf('Please use (%s) cast instead of function call.', $this->map[$name]),
41
                $funcCall
42
            );
43
        }
44
    }
45
}
46