PointerUtil::getSchemaPointers()   B
last analyzed

Complexity

Conditions 10
Paths 48

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.0056

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 36
ccs 25
cts 26
cp 0.9615
rs 7.6666
c 0
b 0
f 0
cc 10
nc 48
nop 2
crap 10.0056

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Swaggest\JsonSchema\Path;
4
5
use Swaggest\JsonDiff\JsonPointer;
6
use Swaggest\JsonSchema\Schema;
7
8
class PointerUtil
9
{
10
    /**
11
     * Builds JSON pointer to schema from processing path
12
     * Path example: #->properties:responses->additionalProperties:envvar->properties:schema
13
     * @param string $path
14
     * @param bool $isURIFragmentId
15
     * @return string|null
16
     */
17 3
    public static function getSchemaPointer($path, $isURIFragmentId = false)
18
    {
19 3
        $result = self::getSchemaPointers($path, $isURIFragmentId);
20 3
        return array_pop($result);
21
    }
22
23
    /**
24
     * Builds JSON pointer to schema from processing path
25
     * Path example: #->properties:responses->additionalProperties:envvar->properties:schema
26
     * @param string $path
27
     * @param bool $isURIFragmentId
28
     * @return string[]
29
     */
30 5
    public static function getSchemaPointers($path, $isURIFragmentId = false)
31
    {
32 5
        $items = explode('->', $path);
33 5
        unset($items[0]);
34 5
        $result = array();
35 5
        $pointer = $isURIFragmentId ? '#' : '';
36 5
        foreach ($items as $item) {
37 5
            $parts = explode(':', $item);
38 5
            if (isset($parts[0])) {
39 5
                $schemaPaths = explode('[', $parts[0], 2);
40 5
                if (isset($schemaPaths[1])) {
41 4
                    $schemaPaths[1] = substr(strtr($schemaPaths[1], array('~1' => '~', '~2' => ':')), 0, -1);
42
                }
43
44 5
                if ($schemaPaths[0] === Schema::PROP_REF) {
45 4
                    $result[] = $pointer . '/' . JsonPointer::escapeSegment(Schema::PROP_REF, $isURIFragmentId);
46 4
                    if ($schemaPaths[1][0] !== '#') { // Absolute URI.
47 2
                        $pointer = $schemaPaths[1];
48
                    } else {
49 2
                        $pointer = self::rebuildPointer($schemaPaths[1], $isURIFragmentId);
50
                    }
51 4
                    continue;
52
                }
53 4
                $pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[0], $isURIFragmentId);
54 4
                if (isset($schemaPaths[1])) {
55 2
                    $pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[1], $isURIFragmentId);
56 4
                } elseif (isset($parts[1])) {
57 3
                    $pointer .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
58
                }
59
            }
60
        }
61 5
        if ($pointer === '') {
62
            $pointer = '/';
63
        }
64 5
        $result[] = $pointer;
65 5
        return $result;
66
    }
67
68
69
    /**
70
     * Builds JSON pointer to data from processing path
71
     * Path example: #->properties:responses->additionalProperties:envvar->properties:schema
72
     * @param string $path
73
     * @param bool $isURIFragmentId
74
     * @return string
75
     */
76 5
    public static function getDataPointer($path, $isURIFragmentId = false)
77
    {
78 5
        $items = explode('->', $path);
79 5
        unset($items[0]);
80 5
        $result = $isURIFragmentId ? '#' : '';
81 5
        foreach ($items as $item) {
82 4
            $parts = explode(':', $item);
83 4
            if (isset($parts[1])) {
84 3
                $result .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
85
            }
86
        }
87 5
        if ($result === '') {
88 1
            return '/';
89
        }
90 4
        return $result;
91
    }
92
93 2
    private static function rebuildPointer($pointer, $isURIFragmentId = false)
94
    {
95 2
        $parts = JsonPointer::splitPath($pointer);
96 2
        $result = $isURIFragmentId ? '#' : '';
97 2
        foreach ($parts as $item) {
98 2
            $result .= '/' . JsonPointer::escapeSegment($item, $isURIFragmentId);
99
        }
100 2
        return $result;
101
    }
102
103
}