Test Failed
Push — master ( a0eed4...bb00ac )
by Kirill
149:40
created

Validator::errorsToString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Backend;
11
12
use JsonSchema\Constraints\Constraint;
13
use Railt\Io\File;
14
use Railt\Io\Readable;
15
use Railt\SDL\Exception\InternalException;
16
use Railt\SDL\IR\Definition;
17
use JsonSchema\Validator as JsonSchema;
18
19
/**
20
 * Class Validator
21
 */
22
class Validator
23
{
24
    /**
25
     * @var string
26
     */
27
    public const RAILT_SDL_1_2 = __DIR__ . '/../../resources/rlsdl-1.2.json';
28
29
    /**
30
     * @var string
31
     */
32
    protected const DEFAULT_VERSION = self::RAILT_SDL_1_2;
33
34
    /**
35
     * @var JsonSchema
36
     */
37
    private $validator;
38
39
    /**
40
     * @var array|object
41
     */
42
    private $schema;
43
44
    /**
45
     * Validator constructor.
46
     * @param Readable|null $schema
47
     */
48
    public function __construct(Readable $schema = null)
49
    {
50
        $this->validator = new JsonSchema();
51
        $this->schema = $this->loadJsonSchema($schema);
52
    }
53
54
    /**
55
     * @param null|Readable|File $schema
56
     * @return mixed|array|object
57
     */
58
    private function loadJsonSchema(?Readable $schema)
59
    {
60
        $schema = $schema ?? File::fromPathname(static::DEFAULT_VERSION);
61
62
        return \json_decode($schema->getContents());
0 ignored issues
show
Bug introduced by
The method getContents does only exist in Railt\Io\Readable, but not in Railt\Io\File.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
63
    }
64
65
    /**
66
     * @param Definition $definition
67
     * @return Definition
68
     * @throws InternalException
69
     */
70
    public function validate(Definition $definition): Definition
71
    {
72
        $json = $definition->toObject();
73
74
        $this->validator->reset();
75
        $this->validator->validate($json, $this->schema, Constraint::CHECK_MODE_VALIDATE_SCHEMA);
76
77
        if (! $this->validator->isValid()) {
78
            $error = \implode(\PHP_EOL, [
79
                'An internal representation code errors was found: ',
80
                $this->errorsToString($this->validator->getErrors())
81
            ]);
82
83
            throw new InternalException($error);
84
        }
85
86
        return $definition;
87
    }
88
89
    /**
90
     * @param array $errors
91
     * @return string
92
     */
93
    private function errorsToString(array $errors): string
94
    {
95
        $applicator = function (array $error): string {
96
            if ($error['property']) {
97
                return \sprintf(' - [%s] %s.', $error['property'], $error['message']);
98
            }
99
100
            return \sprintf(' - %s.', $error['message']);
101
        };
102
103
        return \implode(\PHP_EOL, \array_map($applicator, $errors));
104
    }
105
}
106