Completed
Pull Request — master (#8)
by Povilas
03:23 queued 01:26
created

DetectorVisitor::ignoreFunc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Povils\PHPMND\Visitor;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Const_;
7
use PhpParser\Node\Scalar;
8
use PhpParser\Node\Scalar\DNumber;
9
use PhpParser\Node\Scalar\LNumber;
10
use PhpParser\NodeTraverser;
11
use PhpParser\NodeVisitorAbstract;
12
use Povils\PHPMND\Console\Option;
13
use Povils\PHPMND\Extension\Extension;
14
use Povils\PHPMND\Extension\FunctionAwareExtension;
15
use Povils\PHPMND\FileReport;
16
17
/**
18
 * Class DetectorVisitor
19
 *
20
 * @package Povils\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 $node */
54
        if ($this->isNumber($node) && false === $this->ignoreNumber($node)) {
55
            foreach ($this->option->getExtensions() as $extension) {
56
                if ($extension->extend($node) && false == $this->ignoreFunc($node, $extension)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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
    protected function isNumber(Node $node)
73
    {
74
        return $node instanceof LNumber || $node instanceof DNumber;
75
    }
76
77
    /**
78
     * @param LNumber|DNumber|Scalar $node
79
     *
80
     * @return bool
81
     */
82
    private function ignoreNumber(Scalar $node)
83
    {
84
        return in_array($node->value, $this->option->getIgnoreNumbers(), true);
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in PhpParser\Node\Scalar.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
    }
86
87
    /**
88
     * @param Node      $node
89
     * @param Extension $extension
90
     *
91
     * @return bool
92
     */
93
    private function ignoreFunc(Node $node, Extension $extension)
94
    {
95
        if ($extension instanceof FunctionAwareExtension) {
96
            return $extension->ignoreFunc($node, $this->option->getIgnoreFuncs());
97
        }
98
99
        return false;
100
    }
101
}
102