Passed
Push — master ( c6d404...885f37 )
by Théo
02:15
created

Compactors::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
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\Compactor;
16
17
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
18
use function array_reduce;
19
use function count;
20
use Countable;
21
use Humbug\PhpScoper\Whitelist;
22
use KevinGH\Box\PhpScoper\Scoper;
23
24
/**
25
 * @private
26
 */
27
final class Compactors implements Compactor, Countable
28
{
29
    private $compactors;
30
    private $scoperCompactor;
31
32
    public function __construct(Compactor ...$compactors)
33
    {
34
        $this->compactors = $compactors;
35
36
        foreach ($compactors as $compactor) {
37
            if ($compactor instanceof PhpScoper) {
38
                $this->scoperCompactor = $compactor;
39
40
                break;
41
            }
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function compact(string $file, string $contents): string
49
    {
50
        return (string) array_reduce(
51
            $this->compactors,
52
            static function (string $contents, Compactor $compactor) use ($file): string {
53
                return $compactor->compact($file, $contents);
54
            },
55
            $contents
56
        );
57
    }
58
59
    public function getScoper(): ?Scoper
60
    {
61
        return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper() : null;
62
    }
63
64
    public function getScoperSymbolsRegistry(): ?SymbolsRegistry
65
    {
66
        return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper()->getSymbolsRegistry() : null;
67
    }
68
69
    public function registerSymbolsRegistry(SymbolsRegistry $symbolsRegistry): void
70
    {
71
        if (null !== $this->scoperCompactor) {
72
            $this->scoperCompactor->getScoper()->changeSymbolsRegistry($symbolsRegistry);
73
        }
74
    }
75
76
    public function toArray(): array
77
    {
78
        return $this->compactors;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function count(): int
85
    {
86
        return count($this->compactors);
87
    }
88
}
89