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

JsonFileScoper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A scope() 0 24 3
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