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

IsWithPrivateNamespace   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isInPublicConfiguredEntries() 0 3 1
A __construct() 0 8 1
A apply() 0 16 4
1
<?php declare(strict_types=1);
2
3
namespace NamespaceProtector\Rule;
4
5
use NamespaceProtector\Entry\Entry;
6
use NamespaceProtector\Config\Config;
7
use NamespaceProtector\Db\BooleanMatchNameSpace;
8
use NamespaceProtector\Db\MatchCollectionInterface;
9
use NamespaceProtector\EnvironmentDataLoaderInterface;
10
use NamespaceProtector\Parser\Node\MatchedResultInterface;
11
use NamespaceProtector\Parser\Node\Event\EventProcessNodeInterface;
12
13
class IsWithPrivateNamespace implements RuleInterface
14
{
15
    private EnvironmentDataLoaderInterface  $metadataLoader;
16
    private Config $config;
17
    private IsInConfigureComposerPsr4 $isInConfigureComposerPsr4;
18
19
    public function __construct(
20
        Config $config,
21
        EnvironmentDataLoaderInterface $metadataLoader,
22
        IsInConfigureComposerPsr4 $isInConfigureComposerPsr4
23
    ) {
24
        $this->config = $config;
25
        $this->metadataLoader = $metadataLoader;
26
        $this->isInConfigureComposerPsr4 = $isInConfigureComposerPsr4;
27
    }
28
29
    public function apply(Entry $entry, EventProcessNodeInterface $event): bool
30
    {
31
        if ($this->config->getMode() !== Config::MODE_MAKE_VENDOR_PRIVATE) {
32
            return false;
33
        }
34
35
        if ($this->isInPublicConfiguredEntries($entry, new BooleanMatchNameSpace())->matched()) {
36
            return true;
37
        }
38
39
        if ($this->isInConfigureComposerPsr4->apply($entry, $event)) {
40
            return true;
41
        }
42
43
        $event->foundError();
44
        return true;
45
    }
46
47
    private function isInPublicConfiguredEntries(Entry $currentNamespaceAccess, MatchCollectionInterface $macher): MatchedResultInterface
48
    {
49
        return $macher->evaluate($this->config->getPublicEntries(), $currentNamespaceAccess);
50
    }
51
}
52