Passed
Pull Request — master (#555)
by Théo
02:17
created

InstalledPackagesScoper::scope()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 17
ccs 6
cts 6
cp 1
crap 4
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Scoper\Composer;
16
17
use Humbug\PhpScoper\Scoper;
18
use Humbug\PhpScoper\Whitelist;
19
use InvalidArgumentException;
20
use stdClass;
21
use function array_map;
22
use function gettype;
23
use function is_array;
24
use function preg_match as native_preg_match;
25
use function Safe\json_decode;
26
use function Safe\json_encode;
27
use function Safe\sprintf;
28 3
use const JSON_PRETTY_PRINT;
29
use const JSON_THROW_ON_ERROR;
30 3
31
final class InstalledPackagesScoper implements Scoper
32
{
33
    private static string $filePattern = '/composer(\/|\\\\)installed\.json$/';
34
35
    private Scoper $decoratedScoper;
36
    private AutoloadPrefixer $autoloadPrefixer;
37
38 3
    public function __construct(
39
        Scoper $decoratedScoper,
40 3
        AutoloadPrefixer $autoloadPrefixer
41 1
    ) {
42
        $this->decoratedScoper = $decoratedScoper;
43
        $this->autoloadPrefixer = $autoloadPrefixer;
44 2
    }
45
46 2
    /**
47
     * Scopes PHP and JSON files related to Composer.
48 2
     */
49 2
    public function scope(string $filePath, string $contents): string
50 2
    {
51
        if (1 !== native_preg_match(self::$filePattern, $filePath)) {
52
            return $this->decoratedScoper->scope($filePath, $contents);
53
        }
54 2
55
        $decodedJson = self::decodeContents($contents);
56 2
57 2
        if (!isset($decodedJson->packages) || !is_array($decodedJson->packages)) {
58
            throw new InvalidArgumentException('Expected the decoded JSON to contain the list of installed packages');
59
        }
60 2
61
        $decodedJson->packages = $this->prefixLockPackages($decodedJson->packages);
62
63
        return json_encode(
64
            $decodedJson,
65
            JSON_PRETTY_PRINT
66
        );
67
    }
68
69
    private static function decodeContents(string $contents): stdClass
70
    {
71
        $decodedJson = json_decode($contents, false, 512,  JSON_THROW_ON_ERROR);
72
73
        if ($decodedJson instanceof stdClass) {
74
            return $decodedJson;
75
        }
76
77
        throw new InvalidArgumentException(
78
            sprintf(
79
                'Expected the decoded JSON to be an stdClass instance, got "%s" instead',
80
                gettype($decodedJson),
81
            )
82
        );
83
    }
84
85
    /**
86
     * @param array<string, stdClass> $packages
87
     *
88
     * @return array<string, stdClass>
89
     */
90
    private function prefixLockPackages(array $packages): array
91
    {
92
        return array_map(
93
            fn (stdClass $package) => $this->autoloadPrefixer->prefixPackageAutoloadStatements($package),
94
            $packages,
95
        );
96
    }
97
}
98