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

isMatchMeNameSpaceInCheckEntry()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 16
rs 10
c 0
b 0
f 0
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