Completed
Push — master ( be9e17...f9656a )
by BruceScrutinizer
02:33
created

BooleanMatchNameSpace   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A evaluate() 0 11 3
A isFullyQualifiedNamespaceValid() 0 17 3
1
<?php
2
declare(strict_types=1);
3
4
namespace NamespaceProtector\Db;
5
6
use NamespaceProtector\Entry\Entry;
7
8
final class BooleanMatchNameSpace implements MatchCollectionInterface
9
{
10
    /**
11
     * @param Iterable<mixed> $data
12
     */
13
    public function evaluate(iterable $data, Entry $matchMe): bool
14
    {
15
        foreach ($data as $item) {
16
            $currentEntry = \strtolower($item);
17
            $current = \strtolower($matchMe->get());
18
            if ($this->isFullyQualifiedNamespaceValid($current, $currentEntry)) {
19
                return true;
20
            }
21
        }
22
23
        return false;
24
    }
25
26
    private function isFullyQualifiedNamespaceValid(string $current, string $publicEntry): bool
27
    {
28
        if (strpos($current, $publicEntry) !== false) {
29
            $blockEntry = \explode('\\', $publicEntry);
30
            $blockCurrent = \explode('\\', $current);
31
32
            $endA = \end($blockEntry);
33
            $endB = \end($blockCurrent);
34
35
            if ($endA !== $endB) {
36
                return false;
37
            }
38
39
            return true;
40
        }
41
42
        return false;
43
    }
44
}
45