Completed
Push — master ( b639e0...e27f0d )
by Woody
01:53
created

Schema   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 7
dl 0
loc 114
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFile() 0 4 1
C make() 0 37 8
A __construct() 0 4 1
A type() 0 4 1
phpType() 0 1 ?
A isArray() 0 4 1
A isBoolean() 0 4 1
A isInteger() 0 4 1
A isNull() 0 4 1
A isNumber() 0 4 1
A isObject() 0 4 1
A isString() 0 4 1
A hasTitle() 0 4 1
A title() 0 4 2
1
<?php
2
3
namespace RoundingWell\Schematic;
4
5
abstract class Schema
6
{
7
    /**
8
     * @param string $path
9
     * @return static
10
     */
11 2
    public static function fromFile($path): Schema
12
    {
13 2
        return self::make(json_decode(file_get_contents($path)));
14
    }
15
16
    /**
17
     * @param object $json
18
     */
19 9
    public static function make($json): Schema
20
    {
21 9
        if ($json->type === 'array') {
22 3
            return new Schema\ArraySchema($json);
23
        }
24
25 9
        if ($json->type === 'boolean') {
26 2
            return new Schema\BooleanSchema($json);
27
        }
28
29 8
        if ($json->type === 'integer') {
30 3
            return new Schema\IntegerSchema($json);
31
        }
32
33 7
        if ($json->type === 'null') {
34 2
            return new Schema\NullSchema($json);
35
        }
36
37 6
        if ($json->type === 'number') {
38 2
            return new Schema\NumberSchema($json);
39
        }
40
41 5
        if ($json->type === 'object') {
42 3
            return new Schema\ObjectSchema($json);
43
        }
44
45 5
        if ($json->type === 'string') {
46 5
            return new Schema\StringSchema($json);
47
        }
48
49
        // @codeCoverageIgnoreStart
50
        throw new \InvalidArgumentException(sprintf(
51
            "No schema type available for %s",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal No schema type available for %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
52
            $schema->type
53
        ));
54
        // @codeCoverageIgnoreEnd
55
    }
56
57
    /**
58
     * @var object
59
     */
60
    protected $schema;
61
62 9
    public function __construct($schema)
63
    {
64 9
        $this->schema = $schema;
65 9
    }
66
67 9
    public function type(): string
68
    {
69 9
        return $this->schema->type;
70
    }
71
72
    abstract public function phpType(): string;
73
74 3
    public function isArray(): bool
75
    {
76 3
        return $this->type() === 'array';
77
    }
78
79 1
    public function isBoolean(): bool
80
    {
81 1
        return $this->type() === 'boolean';
82
    }
83
84 2
    public function isInteger(): bool
85
    {
86 2
        return $this->type() === 'integer';
87
    }
88
89 2
    public function isNull(): bool
90
    {
91 2
        return $this->type() === 'null';
92
    }
93
94 1
    public function isNumber(): bool
95
    {
96 1
        return $this->type() === 'number';
97
    }
98
99 3
    public function isObject(): bool
100
    {
101 3
        return $this->type() === 'object';
102
    }
103
104 3
    public function isString(): bool
105
    {
106 3
        return $this->type() === 'string';
107
    }
108
109 1
    public function hasTitle(): bool
110
    {
111 1
        return isset($this->schema->title);
112
    }
113
114 1
    public function title(): string
115
    {
116 1
        return $this->hasTitle() ? $this->schema->title : '';
117
    }
118
}
119