Passed
Pull Request — master (#302)
by Théo
02:32
created

WhitelistManipulator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A mergeWhitelists() 0 28 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\PhpScoper;
16
17
use Assert\Assertion;
18
use Humbug\PhpScoper\Whitelist;
19
use PhpParser\Node\Name\FullyQualified;
20
21
/**
22
 * @private
23
 */
24
final class WhitelistManipulator
25
{
26
    public static function mergeWhitelists(Whitelist ...$whitelists): Whitelist
27
    {
28
        Assertion::greaterThan(count($whitelists), 0, 'Expected to have at least one whitelist, none given');
29
30
        /** @var Whitelist $whitelist */
31
        $whitelist = clone array_shift($whitelists);
32
33
        foreach ($whitelists as $whitelistToMerge) {
34
            $recordedWhitelistedClasses = $whitelistToMerge->getRecordedWhitelistedClasses();
35
36
            foreach ($recordedWhitelistedClasses as [$original, $alias]) {
37
                $whitelist->recordWhitelistedClass(
38
                    new FullyQualified($original),
39
                    new FullyQualified($alias)
40
                );
41
            }
42
43
            $recordedWhitelistedFunctions = $whitelistToMerge->getRecordedWhitelistedFunctions();
44
45
            foreach ($recordedWhitelistedFunctions as [$original, $alias]) {
46
                $whitelist->recordWhitelistedFunction(
47
                    new FullyQualified($original),
48
                    new FullyQualified($alias)
49
                );
50
            }
51
        }
52
53
        return $whitelist;
54
    }
55
}
56