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

UseCast   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 7
c 3
b 0
f 2
lcom 1
cbo 3
dl 0
loc 34
ccs 0
cts 12
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C visitPhpFunctionCall() 0 23 7
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