Passed
Push — master ( 514edf...359688 )
by Théo
02:17
created

JsonFileScoper::scope()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.2621

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 5
dl 0
loc 24
ccs 9
cts 13
cp 0.6923
crap 3.2621
rs 9.536
c 0
b 0
f 0
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 LogicException;
20
use stdClass;
21
use function gettype;
22
use function Humbug\PhpScoper\json_decode;
23
use function Humbug\PhpScoper\json_encode;
24
use function sprintf;
25
26
final class JsonFileScoper implements Scoper
27
{
28
    private $decoratedScoper;
29
30 8
    public function __construct(Scoper $decoratedScoper)
31
    {
32 8
        $this->decoratedScoper = $decoratedScoper;
33
    }
34
35
    /**
36
     * Scopes PHP and JSON files related to Composer.
37
     *
38
     * {@inheritdoc}
39
     */
40 8
    public function scope(string $filePath, string $contents, string $prefix, array $patchers, Whitelist $whitelist): string
41
    {
42 8
        if (1 !== preg_match('/composer\.json$/', $filePath)) {
43 1
            return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
44
        }
45
46 7
        $decodedJson = json_decode($contents);
47
48 7
        if (false === ($decodedJson instanceof stdClass)) {
49
            throw new LogicException(
50
                sprintf(
51
                    'Expected the decoded JSON to be an stdClass instance, got "%s" instead',
52
                    gettype($decodedJson)
53
                )
54
            );
55
        }
56
57 7
        $decodedJson = AutoloadPrefixer::prefixPackageAutoloadStatements($decodedJson, $prefix, $whitelist);
58
59 7
        return json_encode(
60 7
            $decodedJson,
61 7
            JSON_PRETTY_PRINT
62
        );
63
    }
64
}
65