Test Failed
Push — master ( 15b6af...d076c2 )
by BruceScrutinizer
08:02
created

FalsePositive::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace NamespaceProtector\Rule;
4
5
use NamespaceProtector\Entry\Entry;
6
use NamespaceProtector\Db\BooleanMatchKey;
7
use NamespaceProtector\Db\BooleanMatchValue;
8
use NamespaceProtector\Db\DbKeyValueInterface;
9
use NamespaceProtector\Db\MatchCollectionInterface;
10
use NamespaceProtector\EnvironmentDataLoaderInterface;
11
use NamespaceProtector\Parser\Node\Event\EventProcessNodeInterface;
12
13
class FalsePositive implements RuleInterface
14
{
15
    private EnvironmentDataLoaderInterface  $metadataLoader;
16
17
    public function __construct(EnvironmentDataLoaderInterface $metadataLoader)
18
    {
19
        $this->metadataLoader = $metadataLoader;
20
    }
21
22
    public function apply(Entry $entry, EventProcessNodeInterface $event): bool
23
    {
24
        $entry = $this->stripFirstSlash($entry);
25
26
        if ($this->valueExist($this->metadataLoader->getCollectBaseConstants(), new BooleanMatchKey(), $entry)) {
27
            return true;
28
        }
29
30
        if ($this->valueExist($this->metadataLoader->getCollectBaseFunctions(), new  BooleanMatchValue(), $entry)) {
31
            return true;
32
        }
33
34
        if ($this->valueExist($this->metadataLoader->getCollectBaseInterfaces(), new  BooleanMatchValue(), $entry)) {
35
            return true;
36
        }
37
38
        if ($this->valueExist($this->metadataLoader->getCollectBaseClasses(), new  BooleanMatchValue(), $entry)) {
39
            return true;
40
        }
41
42
        return false;
43
    }
44
45
    private function valueExist(DbKeyValueInterface $collections, MatchCollectionInterface $matchCriteria, Entry $matchMe): bool
46
    {
47
        if ($collections->booleanSearch($matchCriteria, $matchMe)) {
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
    private function stripFirstSlash(Entry $token): Entry
55
    {
56
        if ($token->get()[0] === '\\') {
57
            return new Entry(substr($token->get(), 1, strlen($token->get())));
58
        }
59
60
        return $token;
61
    }
62
}
63