JsonFileScoper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 44
ccs 11
cts 15
cp 0.7332
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A decodeContents() 0 12 2
A scope() 0 13 2
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\Scoper;
18
use InvalidArgumentException;
19
use stdClass;
20
use function gettype;
21
use function preg_match as native_preg_match;
22
use function Safe\json_decode;
23
use function Safe\json_encode;
24
use function Safe\sprintf;
25
use const JSON_PRETTY_PRINT;
26
use const JSON_THROW_ON_ERROR;
27
28
final class JsonFileScoper implements Scoper
29
{
30 10
    private Scoper $decoratedScoper;
31
    private AutoloadPrefixer $autoloadPrefixer;
32 10
33
    public function __construct(
34
        Scoper $decoratedScoper,
35
        AutoloadPrefixer $autoloadPrefixer
36
    ) {
37
        $this->decoratedScoper = $decoratedScoper;
38
        $this->autoloadPrefixer = $autoloadPrefixer;
39
    }
40 10
41
    /**
42 10
     * Scopes PHP and JSON files related to Composer.
43 1
     */
44
    public function scope(string $filePath, string $contents): string
45
    {
46 9
        if (1 !== native_preg_match('/composer\.json$/', $filePath)) {
47
            return $this->decoratedScoper->scope($filePath, $contents);
48 9
        }
49
50
        $decodedJson = self::decodeContents($contents);
51
52
        $decodedJson = $this->autoloadPrefixer->prefixPackageAutoloadStatements($decodedJson);
53
54
        return json_encode(
55
            $decodedJson,
56
            JSON_PRETTY_PRINT,
57 9
        );
58
    }
59 9
60 9
    private static function decodeContents(string $contents): stdClass
61 9
    {
62
        $decodedJson = json_decode($contents, false, 512, JSON_THROW_ON_ERROR);
63
64
        if ($decodedJson instanceof stdClass) {
65
            return $decodedJson;
66
        }
67
68
        throw new InvalidArgumentException(
69
            sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
            /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
                'Expected the decoded JSON to be an stdClass instance, got "%s" instead',
71
                gettype($decodedJson),
72
            ),
73
        );
74
    }
75
}
76