Test Setup Failed
Push — main ( e03535...dde247 )
by Pieter
03:39
created

createObjectSchemaWithoutProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
4
namespace Apie\OpenapiSchema\Factories;
5
6
use Apie\OpenapiSchema\Contract\SchemaContract;
7
use Apie\OpenapiSchema\Spec\Schema;
8
use ReflectionClass;
9
10
class SchemaFactory
11
{
12
    public static function createObjectSchemaWithoutProperties(
13
        ReflectionClass $class,
14
        string $operation = 'get',
15
        array $groups = []
16
    ): SchemaContract {
17
        $description = $class->getShortName() . ' ' . $operation;
18
        if ($groups) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groups of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
19
            $description .= ' for groups ' . implode(', ', $groups);
20
        }
21
        return Schema::fromNative([
22
            'type' => 'object',
23
            'properties' => [],
24
            'title' => $class->getShortName(),
25
            'description' => $description,
26
        ]);
27
    }
28
29
    public static function createAnyTypeSchema(): Schema
30
    {
31
        return Schema::fromNative([]);
32
    }
33
34
    public static function createStringSchema(?string $format = null, ?string $defaultValue = null, bool $nullable = false): Schema
35
    {
36
        $data = ['type' => 'string'];
37
        if ($format !== null) {
38
            $data['format'] = $format;
39
        }
40
        if ($nullable) {
41
            $data['nullable'] = $nullable;
42
        }
43
        if ($defaultValue !== null) {
44
            $data['example'] = $defaultValue;
45
            $data['default'] = $defaultValue;
46
        }
47
        return Schema::fromNative($data);
48
    }
49
50
    public static function createBooleanSchema(): Schema
51
    {
52
        return Schema::fromNative(['type' => 'boolean']);
53
    }
54
55
    public static function createNumberSchema(?string $format = null): Schema
56
    {
57
        return Schema::fromNative(['type' => 'number', 'format' => $format]);
58
    }
59
60
    public static function createFloatSchema(): Schema
61
    {
62
        return self::createNumberSchema('double');
63
    }
64
}
65