Passed
Pull Request — master (#563)
by Théo
02:31
created

SimpleScoper::createSerializablePatchers()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
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\PhpScoper;
16
17
use Exception;
18
use Humbug\PhpScoper\Container as PhpScoperContainer;
19
use Humbug\PhpScoper\Patcher\ComposerPatcher;
20
use Humbug\PhpScoper\Patcher\SymfonyPatcher;
21
use Humbug\PhpScoper\Scoper as PhpScoper;
22
use Humbug\PhpScoper\Scoper\FileWhitelistScoper;
23
use Humbug\PhpScoper\Whitelist;
24
use Humbug\PhpScoper\Configuration as PhpScoperConfiguration;
25
use Opis\Closure\SerializableClosure;
26
use Serializable;
27
use function array_map;
28
use function count;
29
use function var_dump;
30
31
/**
32
 * @private
33
 */
34
final class SimpleScoper implements Scoper
35
{
36
    private PhpScoperConfiguration $scoperConfig;
37
    private PhpScoperContainer $scoperContainer;
38
    private PhpScoper $scoper;
39
40
    /**
41
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type KevinGH\Box\PhpScoper\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
     */
43
    private array $whitelistedFilePaths;
44
45
    public function __construct(
46
        PhpScoperConfiguration $scoperConfig,
47
        string ...$whitelistedFilePaths
48
    ) {
49
        $this->scoperConfig = new PhpScoperConfiguration(
50
            $scoperConfig->getPath(),
51
            $scoperConfig->getPrefix(),
52
            $scoperConfig->getFilesWithContents(),
53
            $scoperConfig->getWhitelistedFilesWithContents(),
54
            self::createSerializablePatchers($scoperConfig->getPatchers()),
55
            $scoperConfig->getWhitelist(),
56
            $scoperConfig->getInternalClasses(),
57
            $scoperConfig->getInternalFunctions(),
58
            $scoperConfig->getInternalConstants(),
59
        );
60
        $this->whitelistedFilePaths = $whitelistedFilePaths;
0 ignored issues
show
Documentation Bug introduced by
It seems like $whitelistedFilePaths of type array<integer,string> is incompatible with the declared type KevinGH\Box\PhpScoper\list of property $whitelistedFilePaths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function scope(string $filePath, string $contents): string
67
    {
68
        return $this->getScoper()->scope(
69
            $filePath,
70
            $contents,
71
        );
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function changeWhitelist(Whitelist $whitelist): void
78
    {
79
        $previousConfig = $this->scoperConfig;
0 ignored issues
show
Unused Code introduced by
The assignment to $previousConfig is dead and can be removed.
Loading history...
80
81
        var_dump($whitelist);die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Security Debugging Code introduced by
var_dump($whitelist) looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
83
        $this->scoperConfig = new PhpScoperConfiguration(
0 ignored issues
show
Unused Code introduced by
$this->scoperConfig = ne...getInternalConstants()) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
            $previousConfig->getPath(),
85
            $previousConfig->getPrefix(),
86
            $previousConfig->getFilesWithContents(),
87
            $previousConfig->getWhitelistedFilesWithContents(),
88
            $previousConfig->getPatchers(),
89
            $whitelist,
90
            $previousConfig->getInternalClasses(),
91
            $previousConfig->getInternalFunctions(),
92
            $previousConfig->getInternalConstants(),
93
        );
94
95
        unset($this->scoper);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getWhitelist(): Whitelist
102
    {
103
        return $this->scoperConfig->getWhitelist();
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getPrefix(): string
110
    {
111
        return $this->scoperConfig->getPrefix();
112
    }
113
114
    private function getScoper(): PhpScoper
115
    {
116
        if (isset($this->scoper)) {
117
            return $this->scoper;
118
        }
119
120
        if (!isset($this->scoperContainer)) {
121
            $this->scoperContainer = new PhpScoperContainer();
122
        }
123
124
        $scoper = $this->scoperContainer
125
            ->getScoperFactory()
126
            ->createScoper($this->scoperConfig);
127
128
        if (count($this->whitelistedFilePaths) !== 0) {
129
            $scoper = new FileWhitelistScoper(
130
                $scoper,
131
                ...$this->whitelistedFilePaths,
132
            );
133
        }
134
135
        $this->scoper = $scoper;
136
137
        return $this->scoper;
138
    }
139
140
    /**
141
     * @param callable[] $patchers
142
     *
143
     * @retunr SerializableClosure[]
144
     */
145
    private static function createSerializablePatchers(array $patchers): array
146
    {
147
        return array_map(
148
            static function (callable $patcher): SerializableClosure {
149
                if ($patcher instanceof SymfonyPatcher
150
                    || $patcher instanceof ComposerPatcher
151
                ) {
152
                    $patcher = static fn (string $filePath, string $prefix, string $contents) => $patcher($filePath, $prefix, $contents);
153
                }
154
155
                return new SerializableClosure($patcher);
156
            },
157
            $patchers,
158
        );
159
    }
160
}
161