Completed
Push — master ( 981259...d281bf )
by Дмитрий
02:51
created

UseCast   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
ccs 0
cts 20
cp 0
rs 10
wmc 6
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B pass() 0 31 6
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
7
8
use PhpParser\Node\Expr\FuncCall;
9
use PhpParser\Node\Name;
10
use PHPSA\Compiler\Expression;
11
use PHPSA\Context;
12
13
class UseCast implements PassFunctionCallInterface
14
{
15
    protected $map = array(
16
        'boolval' => 'bool',
17
        'intval' => 'int',
18
        'floatval' => 'double',
19
        'doubleval' => 'double',
20
        'strval' => 'string'
21
    );
22
23
    public function pass(FuncCall $funcCall, Context $context)
24
    {
25
        $compiler = $context->getExpressionCompiler();
26
        $funcNameCompiledExpression = $compiler->compile($funcCall->name);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $funcNameCompiledExpression exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
27
28
        if ($funcNameCompiledExpression->isString() && $funcNameCompiledExpression->isCorrectValue()) {
29
            $name = $funcNameCompiledExpression->getValue();
30
        } else {
31
            $context->debug(
32
                'Unexpected function name type ' . $funcNameCompiledExpression->getType(),
33
                $funcCall->name
34
            );
35
36
            return false;
37
        }
38
39
        if (isset($this->map[$name])) {
40
            /**
41
             * Exclusion via intval with 2 args intval($number, int $base = 10);
42
             */
43
            if ($name == 'intval' && count($funcCall->args) > 1) {
44
                return;
45
            }
46
47
            $context->notice(
48
                'fcall.cast',
49
                sprintf('Please use (%s) cast instead of function call.', $this->map[$name]),
50
                $funcCall
51
            );
52
        }
53
    }
54
}
55