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

WhitelistManipulator::mergeWhitelists()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.1111
c 0
b 0
f 0
cc 6
nc 17
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KevinGH\Box\PhpScoper;
6
7
8
use Humbug\PhpScoper\Whitelist;
9
10
final class WhitelistManipulator
11
{
12
    public static function mergeWhitelists(Whitelist ...$whitelists): Whitelist
13
    {
14
        $whitelistGlobalConstants = true;
15
        $whitelistGlobalClasses = true;
16
        $whitelistGlobalFunctions = true;
17
        $elements = [];
18
19
        foreach ($whitelists as $whitelist) {
20
            $whitelistGlobalConstants = $whitelistGlobalConstants && $whitelist->whitelistGlobalConstants();
21
            $whitelistGlobalClasses = $whitelistGlobalClasses && $whitelist->whitelistGlobalClasses();
22
            $whitelistGlobalFunctions = $whitelistGlobalFunctions && $whitelist->whitelistGlobalFunctions();
23
24
            $whitelistElements = $whitelist->toArray();
25
26
            foreach ($whitelistElements as $whitelistElement) {
27
                // Do not rely on array_merge here since it can be quite slow. Indeed array_merge copies the two arrays
28
                // to merge into a new one, done in a loop like here it can be quite taxing.
29
                $elements[] = $whitelistElement;
30
            }
31
        }
32
33
        return Whitelist::create(
34
            $whitelistGlobalConstants,
35
            $whitelistGlobalClasses,
36
            $whitelistGlobalFunctions,
37
            ...$elements
38
        );
39
    }
40
}