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

Json   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 22
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compactContent() 0 9 2
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