InvalidValue   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
eloc 32
dl 0
loc 71
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addPath() 0 8 3
A getSchemaPointer() 0 3 1
A getFailedSubSchema() 0 18 6
A getDataPointer() 0 3 1
A inspect() 0 15 4
1
<?php
2
3
namespace Swaggest\JsonSchema;
4
5
use Swaggest\JsonDiff\JsonPointer;
6
use Swaggest\JsonSchema\Exception\Error;
7
use Swaggest\JsonSchema\Exception\LogicException;
8
use Swaggest\JsonSchema\Path\PointerUtil;
9
use Swaggest\JsonSchema\Structure\ObjectItemContract;
10
11
class InvalidValue extends Exception
12
{
13
    public $error;
14
    public $path;
15
16 1264
    public function addPath($path)
17
    {
18 1264
        if ($this->error === null) {
19 1248
            $this->error = $this->message;
20
        }
21 1264
        $this->path = $path;
22 1264
        if ('#' !== $this->path) {
23 792
            $this->message .= ' at ' . $path;
24
        }
25 1264
    }
26
27
    const INVALID_VALUE = 1;
28
    const NOT_IMPLEMENTED = 2;
29
30
31 3
    public function inspect()
32
    {
33 3
        $error = new Error();
34 3
        $error->error = $this->error;
35 3
        $error->processingPath = $this->path;
36 3
        $error->dataPointer = PointerUtil::getDataPointer($error->processingPath);
37 3
        $error->schemaPointers = PointerUtil::getSchemaPointers($error->processingPath);
38 3
        if ($this instanceof LogicException) {
39 2
            if ($this->subErrors !== null) {
40 1
                foreach ($this->subErrors as $subError) {
41 1
                    $error->subErrors[] = $subError->inspect();
42
                }
43
            }
44
        }
45 3
        return $error;
46
    }
47
48 2
    public function getSchemaPointer()
49
    {
50 2
        return PointerUtil::getSchemaPointer($this->path);
51
    }
52
53
    /**
54
     * @param Schema $schema
55
     * @return bool|Schema
56
     * @throws \Swaggest\JsonDiff\Exception
57
     */
58 2
    public function getFailedSubSchema(Schema $schema)
59
    {
60 2
        $schemaPointer = $this->getSchemaPointer();
61 2
        if ($schema instanceof ObjectItemContract) {
0 ignored issues
show
introduced by
$schema is always a sub-type of Swaggest\JsonSchema\Structure\ObjectItemContract.
Loading history...
62 2
            $refs = $schema->getFromRefs();
63 2
            if ($refs !== null) {
64 1
                foreach ($refs as $ref) {
65 1
                    if (substr($schemaPointer, 0, strlen($ref)) === $ref) {
0 ignored issues
show
Bug introduced by
It seems like $schemaPointer can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

65
                    if (substr(/** @scrutinizer ignore-type */ $schemaPointer, 0, strlen($ref)) === $ref) {
Loading history...
66 1
                        $schemaPointer = substr($schemaPointer, strlen($ref));
67
                    }
68
                }
69
            }
70
        }
71 2
        if (!(bool)$schemaPointer) {
72 1
            return $schema;
73
        }
74
75 1
        return JsonPointer::getByPointer($schema, $this->getSchemaPointer());
76
    }
77
78
79 1
    public function getDataPointer()
80
    {
81 1
        return PointerUtil::getDataPointer($this->path);
82
    }
83
84
}