Passed
Pull Request — master (#22)
by Théo
02:53
created

Json::compact()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
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\Compactor;
16
17
/**
18
 * Compacts JSON files by re-encoding without pretty print.
19
 */
20
final class Json extends FileExtensionCompactor
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function __construct(array $extensions = ['json'])
26
    {
27
        parent::__construct($extensions);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function compactContent(string $contents): string
34
    {
35
        $decodedContents = json_decode($contents);
36
37
        if (JSON_ERROR_NONE !== json_last_error()) {
38
            return $contents;
39
        }
40
41
        return json_encode($decodedContents);
42
    }
43
}
44