Completed
Pull Request — master (#8)
by
unknown
01:47
created

Schema::make()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace RoundingWell\Schematic;
4
5
abstract class Schema
6
{
7
    protected const SCHEMA_TYPES = [
8
        'array',
9
        'boolean',
10
        'integer',
11
        'null',
12
        'number',
13
        'object',
14
        'string'
15
    ];
16
17
    /**
18
     * @param string $path
19
     * @return static
20
     */
21 2
    public static function fromFile($path): Schema
22
    {
23 2
        return self::make(json_decode(file_get_contents($path)));
24
    }
25
26
    /**
27
     * @param object $json
28
     */
29 11
    public static function make($json): Schema
30
    {
31 11
        if ( ! isset($json->type)) {
32 1
            throw new \InvalidArgumentException('Missing schema type.');
33
        }
34
35 10
        if ( ! in_array(strtolower($json->type), Schema::SCHEMA_TYPES)) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
36 1
            throw new \InvalidArgumentException(sprintf(
37 1
                "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...
38 1
                $json->type
39
            ));
40
        }
41
42 9
        $schema = 'RoundingWell\\Schematic\\Schema\\' . ucfirst($json->type) . 'Schema';
43
44 9
        return new $schema($json);
45
    }
46
47
    /**
48
     * @var object
49
     */
50
    protected $schema;
51
52 9
    public function __construct($schema)
53
    {
54 9
        $this->schema = $schema;
55 9
    }
56
57 9
    public function type(): string
58
    {
59 9
        return $this->schema->type;
60
    }
61
62
    abstract public function phpType(): string;
63
64 3
    public function isArray(): bool
65
    {
66 3
        return $this->type() === 'array';
67
    }
68
69 1
    public function isBoolean(): bool
70
    {
71 1
        return $this->type() === 'boolean';
72
    }
73
74 2
    public function isInteger(): bool
75
    {
76 2
        return $this->type() === 'integer';
77
    }
78
79 2
    public function isNull(): bool
80
    {
81 2
        return $this->type() === 'null';
82
    }
83
84 1
    public function isNumber(): bool
85
    {
86 1
        return $this->type() === 'number';
87
    }
88
89 3
    public function isObject(): bool
90
    {
91 3
        return $this->type() === 'object';
92
    }
93
94 3
    public function isString(): bool
95
    {
96 3
        return $this->type() === 'string';
97
    }
98
99 1
    public function title(): string
100
    {
101 1
        return $this->schema->title ?? '';
102
    }
103
}
104