Completed
Pull Request — master (#1)
by Jitendra
01:21
created

PadsJson::pad()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ahc\Json;
4
5
/**
6
 * Attempts to fix truncated JSON by padding contextual counterparts at the end.
7
 *
8
 * @author   Jitendra Adhikari <[email protected]>
9
 * @license  MIT
10
 *
11
 * @internal
12
 *
13
 * @link     https://github.com/adhocore/php-json-fixer
14
 */
15
trait PadsJson
16
{
17
    public function pad($tmpJson)
18
    {
19
        if (!$this->inStr) {
20
            $tmpJson = \rtrim($tmpJson, ',');
21
            while ($this->lastToken() === ',') {
0 ignored issues
show
Bug introduced by
It seems like lastToken() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

21
            while ($this->/** @scrutinizer ignore-call */ lastToken() === ',') {
Loading history...
22
                $this->popToken(',');
0 ignored issues
show
Bug introduced by
It seems like popToken() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

22
                $this->/** @scrutinizer ignore-call */ 
23
                       popToken(',');
Loading history...
23
            }
24
        }
25
26
        $tmpJson = $this->padLiteral($tmpJson);
27
        $tmpJson = $this->padObject($tmpJson);
28
29
        return $this->padStack($tmpJson);
30
    }
31
32
    protected function padLiteral($tmpJson)
33
    {
34
        if ($this->inStr) {
35
            return $tmpJson;
36
        }
37
38
        $match = \preg_match('/(tr?u?e?|fa?l?s?e|nu?l?l?)$/', $tmpJson, $matches);
39
40
        if (!$match || null === $literal = $this->maybeLiteral($matches[1])) {
0 ignored issues
show
Bug introduced by
It seems like maybeLiteral() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

40
        if (!$match || null === $literal = $this->/** @scrutinizer ignore-call */ maybeLiteral($matches[1])) {
Loading history...
41
            return $tmpJson;
42
        }
43
44
        return \substr($tmpJson, 0, 0 - \strlen($matches[1])) . $literal;
45
    }
46
47
    protected function padStack($tmpJson)
48
    {
49
        foreach (\array_reverse($this->stack, true) as $index => $token) {
50
            if (isset($this->pairs[$token])) {
51
                $tmpJson .= $this->pairs[$token];
52
            } elseif (\in_array($token, [':', ','])) {
53
                $tmpJson .= $this->padValue($tmpJson, $token, $index);
54
            }
55
        }
56
57
        return $tmpJson;
58
    }
59
60
    protected function padObject($tmpJson)
61
    {
62
        $empty = \substr($tmpJson, -1) == '{' && !$this->inStr;
63
64
        if ($empty || $this->arrayPos > $this->objectPos) {
65
            return $tmpJson;
66
        }
67
68
        $part = \substr($tmpJson, $this->objectPos + 1);
69
70
        if (\preg_match('/(,)?(\"[^"]+\"(\s*:\s*)?[^,]*)+$/', $part, $matches)) {
71
            return $tmpJson;
72
        }
73
74
        $tmpJson .= $this->inStr ? '":' : ':';
75
        $tmpJson .= $this->missingValue;
76
        if ($this->lastToken() === '"') {
77
            $this->popToken();
78
        }
79
80
        return $tmpJson;
81
    }
82
83
    protected function padValue($tmpJson, $token, $index)
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed. ( Ignorable by Annotation )

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

83
    protected function padValue($tmpJson, $token, /** @scrutinizer ignore-unused */ $index)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        if ($token === ':' && !$this->inStr && \substr($tmpJson, -1) === ':') {
86
            return $this->missingValue;
87
        }
88
89
        return '';
90
    }
91
92
    protected function padString($string)
93
    {
94
        $last  = \substr($string, -1);
95
        $last2 = \substr($string, -2);
96
97
        if ($last2 === '\"' || $last !== '"') {
98
            return $string . '"';
99
        }
100
101
        // @codeCoverageIgnoreStart
102
        return null;
103
        // @codeCoverageIgnoreEnd
104
    }
105
}
106