Passed
Push — master ( fa352d...0eb7bd )
by BruceScrutinizer
02:09
created

BooleanMatchNameSpace   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 43
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A evaluate() 0 12 3
A stripQueueSlash() 0 6 1
A isMatchMeNameSpaceInCheckEntry() 0 16 4
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 $matchMeInData): MatchedResultInterface
18
    {
19
        foreach ($data as $item) {
20
            $currentEntry = \strtolower($this->stripQueueSlash($item));
21
            $matchMeLiteral = \strtolower($this->stripQueueSlash($matchMeInData->get()));
22
23
            if ($this->isMatchMeNameSpaceInCheckEntry($matchMeLiteral, $currentEntry)) {
24
                return new MatchedResult($item);
25
            }
26
        }
27
28
        return new EmptyMatchedResult();
29
    }
30
31
    private function isMatchMeNameSpaceInCheckEntry(string $matchMeInData, string $checkEntry): bool
32
    {
33
        if ($matchMeInData === $checkEntry) {
34
            return true;
35
        }
36
37
        $pos = \strpos($matchMeInData, $checkEntry);
38
        if ($pos === false) {
39
            return false;
40
        }
41
42
        if (\strlen($checkEntry) > \strlen($matchMeInData)) {
43
            return false;
44
        }
45
46
        return true;
47
    }
48
49
    private function stripQueueSlash(string $token): string
50
    {
51
        $tokenOne = ltrim($token, '\\');
52
        $tokenTwo = rtrim($tokenOne, '\\');
53
54
        return $tokenTwo;
55
    }
56
}
57