Completed
Push — master ( 8aa9dc...da896c )
by BruceScrutinizer
06:41
created

BooleanMatchNameSpace::evaluate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NamespaceProtector\Db;
6
7
use NamespaceProtector\Entry\Entry;
8
use NamespaceProtector\Parser\Node\EmptyMatchedResult;
9
use NamespaceProtector\Parser\Node\MatchedResult;
10
use NamespaceProtector\Parser\Node\MatchedResultInterface;
11
12
final class BooleanMatchNameSpace implements MatchCollectionInterface
13
{
14
    /**
15
     * @param Iterable<mixed> $data
16
     */
17
    public function evaluate(iterable $data, Entry $matchMe): MatchedResultInterface
18
    {
19
20
        foreach ($data as $item) {
21
            $currentEntry = \strtolower($item);
22
            $current = \strtolower($matchMe->get());
23
24
            if ($this->isCurrentNamespaceInsideAPublicNamespace($current, $currentEntry)) {
25
                return new MatchedResult($item);
26
            }
27
        }
28
29
        return new EmptyMatchedResult();
30
    }
31
32
    private function isCurrentNamespaceInsideAPublicNamespace(string $current, string $publicEntry): bool
33
    {        
34
35
        if ($current === $publicEntry) {
36
            return true;
37
        }
38
39
        $blockEntry = \explode('\\', $publicEntry);
40
        $blockCurrent = \explode('\\', $current);
41
42
        foreach ($blockCurrent as $tokenCurrent) {
43
            if ($tokenCurrent === '') {
44
                continue;
45
            }
46
47
            if (!\in_array($tokenCurrent, $blockEntry, true)) {
48
                return false;
49
            }
50
        }
51
52
        return \true;
53
    }
54
}
55