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

isInPublicConfiguredEntries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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