PadsJson::objectNeedsPadding()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 3
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * This file is part of the PHP-JSON-FIXER package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Json;
13
14
/**
15
 * Attempts to fix truncated JSON by padding contextual counterparts at the end.
16
 *
17
 * @author   Jitendra Adhikari <[email protected]>
18
 * @license  MIT
19
 *
20
 * @internal
21
 *
22
 * @link     https://github.com/adhocore/php-json-fixer
23
 */
24
trait PadsJson
25
{
26
    public function pad($tmpJson)
27
    {
28
        if (!$this->inStr) {
29
            $tmpJson = \rtrim($tmpJson, ',');
30
            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

30
            while ($this->/** @scrutinizer ignore-call */ lastToken() === ',') {
Loading history...
31
                $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

31
                $this->/** @scrutinizer ignore-call */ 
32
                       popToken();
Loading history...
32
            }
33
        }
34
35
        $tmpJson = $this->padLiteral($tmpJson);
36
        $tmpJson = $this->padObject($tmpJson);
37
38
        return $this->padStack($tmpJson);
39
    }
40
41
    protected function padLiteral($tmpJson)
42
    {
43
        if ($this->inStr) {
44
            return $tmpJson;
45
        }
46
47
        $match = \preg_match('/(tr?u?e?|fa?l?s?e?|nu?l?l?)$/', $tmpJson, $matches);
48
49
        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

49
        if (!$match || null === $literal = $this->/** @scrutinizer ignore-call */ maybeLiteral($matches[1])) {
Loading history...
50
            return $tmpJson;
51
        }
52
53
        return \substr($tmpJson, 0, 0 - \strlen($matches[1])) . $literal;
54
    }
55
56
    protected function padStack($tmpJson)
57
    {
58
        foreach (\array_reverse($this->stack, true) as $index => $token) {
59
            if (isset($this->pairs[$token])) {
60
                $tmpJson .= $this->pairs[$token];
61
            }
62
        }
63
64
        return $tmpJson;
65
    }
66
67
    protected function padObject($tmpJson)
68
    {
69
        if (!$this->objectNeedsPadding($tmpJson)) {
70
            return $tmpJson;
71
        }
72
73
        $part = \substr($tmpJson, $this->objectPos + 1);
74
        if (\preg_match('/(\s*\"[^"]+\"\s*:\s*[^,]+,?)+$/', $part, $matches)) {
75
            return $tmpJson;
76
        }
77
78
        if ($this->inStr) {
79
            $tmpJson .= '"';
80
        }
81
82
        $tmpJson = $this->padIf($tmpJson, ':');
83
        $tmpJson = $tmpJson . $this->missingValue;
84
85
        if ($this->lastToken() === '"') {
86
            $this->popToken();
87
        }
88
89
        return $tmpJson;
90
    }
91
92
    protected function objectNeedsPadding($tmpJson)
93
    {
94
        $last  = \substr($tmpJson, -1);
95
        $empty = $last === '{' && !$this->inStr;
96
97
        return !$empty && $this->arrayPos < $this->objectPos;
98
    }
99
100
    protected function padString($string)
101
    {
102
        $last  = \substr($string, -1);
103
        $last2 = \substr($string, -2);
104
105
        if ($last2 === '\"' || $last !== '"') {
106
            return $string . '"';
107
        }
108
109
        // @codeCoverageIgnoreStart
110
        return null;
111
        // @codeCoverageIgnoreEnd
112
    }
113
114
    protected function padIf($string, $substr)
115
    {
116
        if (\substr($string, 0 - \strlen($substr)) !== $substr) {
117
            return $string . $substr;
118
        }
119
120
        return $string;
121
    }
122
}
123