Util   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 80.85%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 77
ccs 38
cts 47
cp 0.8085
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A schemaPath() 0 4 1
A getStdin() 0 9 2
A printableErrors() 0 12 1
B loadJson() 0 17 5
A renderErrorTable() 0 12 1
A isLoaderPath() 0 4 1
A loadPath() 0 10 1
1
<?php
2
3
namespace Yuloh\JsonGuardCli;
4
5
use League\JsonGuard\ValidationError;
6
use League\JsonReference\Dereferencer;
7
use Seld\JsonLint\JsonParser;
8
use Symfony\Component\Console\Helper\Table;
9
10
class Util
11
{
12
    public static function printableErrors(array $errors)
13
    {
14 6
        return array_map(function (ValidationError $error) {
15 6
            $context = $error->getContext();
16
            return [
17 6
                ValidationError::MESSAGE     => $error->getMessage(),
18 6
                ValidationError::SCHEMA_PATH => $context[ValidationError::SCHEMA_PATH],
19 6
                ValidationError::DATA_PATH   => $context[ValidationError::DATA_PATH],
20 6
                ValidationError::CAUSE       => $context[ValidationError::CAUSE],
21 3
            ];
22 6
        }, $errors);
23
    }
24
25 28
    public static function loadJson($json)
26
    {
27
        // If it's a loader path just load it since we can't lint that.
28 28
        if (static::isLoaderPath($json)) {
29 8
            return static::loadPath($json);
30 22
        } elseif (file_exists($json)) {
31 14
            $json = file_get_contents($json);
32 16
        } elseif ($json === '-') {
33
            $json = static::getStdin();
34
        }
35
36 22
        if ($parseException = (new JsonParser())->lint($json)) {
37 4
            throw $parseException;
38
        }
39
40 20
        return json_decode($json, false, 512, JSON_BIGINT_AS_STRING);
41
    }
42
43 6
    public static function renderErrorTable($output, $errors)
44
    {
45 6
        (new Table($output))
46 6
            ->setHeaders([
47 6
                'Message',
48 3
                'Schema Path',
49 3
                'Data Path',
50 3
                'Cause',
51 3
            ])
52 6
            ->setRows(static::printableErrors($errors))
53 6
            ->render();
54 6
    }
55
56 28
    public static function isLoaderPath($path)
57
    {
58 28
        return preg_match('#^[^{\n\r]+\:\/\/[^}\n\r]*#', $path);
59
    }
60
61 12
    public static function schemaPath($file = '')
62
    {
63 12
        return realpath(__DIR__ . '/../schema/' . $file);
64
    }
65
66 8
    public static function loadPath($path)
67
    {
68 8
        list($prefix, $path) = explode('://', $path, 2);
69
70 8
        $loader = Dereferencer::draft4()
71 8
            ->getLoaderManager()
72 8
            ->getLoader($prefix);
73
74 8
        return $loader->load($path);
75
    }
76
77
    public static function getStdin()
78
    {
79
        $json = '';
80
        $fh   = fopen('php://stdin', 'r');
81
        while ($line = fgets($fh)) {
82
            $json .= $line;
83
        }
84
        return $json;
85
    }
86
}
87