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

WhitelistManipulator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 17
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A mergeWhitelists() 0 26 6
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
}