Completed
Pull Request — master (#38)
by Pádraic
02:50
created

DetectorVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace PHPMND\Visitor;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Const_;
7
use PhpParser\Node\Scalar\DNumber;
8
use PhpParser\Node\Scalar\LNumber;
9
use PhpParser\Node\Scalar\String_;
10
use PhpParser\NodeTraverser;
11
use PhpParser\NodeVisitorAbstract;
12
use PHPMND\Console\Option;
13
use PHPMND\Extension\Extension;
14
use PHPMND\Extension\FunctionAwareExtension;
15
use PHPMND\FileReport;
16
17
/**
18
 * Class DetectorVisitor
19
 *
20
 * @package PHPMND
21
 */
22
class DetectorVisitor extends NodeVisitorAbstract
23
{
24
    /**
25
     * @var FileReport
26
     */
27
    private $fileReport;
28
29
    /**
30
     * @var Option
31
     */
32
    private $option;
33
34
    /**
35
     * @param FileReport $fileReport
36
     * @param Option $option
37
     */
38
    public function __construct(FileReport $fileReport, Option $option)
39
    {
40
        $this->fileReport = $fileReport;
41
        $this->option = $option;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function enterNode(Node $node)
48
    {
49
        if ($node instanceof Const_) {
50
            return NodeTraverser::DONT_TRAVERSE_CHILDREN;
51
        }
52
53
        /** @var LNumber|DNumber|Node $node */
54
        if ($this->isNumber($node) || $this->isString($node)) {
55
            foreach ($this->option->getExtensions() as $extension) {
56
                if ($extension->extend($node) && false === $this->ignoreFunc($node, $extension)) {
57
                    $this->fileReport->addEntry($node->getLine(), $node->value);
58
59
                    return null;
60
                }
61
            }
62
        }
63
64
        return null;
65
    }
66
67
    /**
68
     * @param Node $node
69
     *
70
     * @return bool
71
     */
72
    private function isNumber(Node $node)
73
    {
74
        return ($node instanceof LNumber || $node instanceof DNumber) && false === $this->ignoreNumber($node);
75
    }
76
77
    /**
78
     * @param Node $node
79
     *
80
     * @return bool
81
     */
82
    private function isString(Node $node)
83
    {
84
        return $this->option->includeStrings() && $node instanceof String_ && false === $this->ignoreString($node);
85
    }
86
87
    /**
88
     * @param LNumber|DNumber|Node $node
89
     *
90
     * @return bool
91
     */
92
    private function ignoreNumber(Node $node)
93
    {
94
        return in_array($node->value, $this->option->getIgnoreNumbers(), true);
1 ignored issue
show
Bug introduced by
Accessing value on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
95
    }
96
97
    /**
98
     * @param String_|Node $node
99
     *
100
     * @return bool
101
     */
102
    private function ignoreString(Node $node)
103
    {
104
        return in_array($node->value, $this->option->getIgnoreStrings(), true);
105
    }
106
107
    /**
108
     * @param Node      $node
109
     * @param Extension $extension
110
     *
111
     * @return bool
112
     */
113
    private function ignoreFunc(Node $node, Extension $extension)
114
    {
115
        if ($extension instanceof FunctionAwareExtension) {
116
            return $extension->ignoreFunc($node, $this->option->getIgnoreFuncs());
117
        }
118
119
        return false;
120
    }
121
}
122