Passed
Push — master ( 0ff30e...cb8abc )
by Bruno
09:58
created

Util::scalars()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 22
rs 9.2222
c 2
b 0
f 0
ccs 0
cts 4
cp 0
cc 6
nc 3
nop 2
crap 42
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Formularium\Formularium;
6
use Illuminate\Support\Str;
7
use Nette\PhpGenerator\PhpNamespace;
8
9
final class Util
10
{
11
    /**
12
     * Undocumented function
13
     *
14
     * @param string $name The type name
15
     * @param string $ns The namespace
16
     * @return string
17
     */
18
    public static function generateLighthouseTypeFile(string $name, string $ns): string
19
    {
20
        $date = date('c');
21
        $HEADER = <<<EOF
22
<?php declare(strict_types=1);
23
/**
24
 * This file is automatically generated by Modelarium on {$date}.
25
 */
26
27
28
EOF;
29
        
30
        $printer = new \Nette\PhpGenerator\PsrPrinter;
31
        $typeName = Str::lower($name);
0 ignored issues
show
Unused Code introduced by
The assignment to $typeName is dead and can be removed.
Loading history...
32
        
33
        $namespace = new PhpNamespace($ns);
34
        $namespace->addClass('Datatype_' . $name)
35
            ->setExtends('\\Modelarium\\Types\\FormulariumScalarType');
36
37
        $stub = $HEADER . $printer->printNamespace($namespace);
38
39
        return $stub;
40
    }
41
42
    /**
43
     * @param array<string, string> $datatypes a list class name => datatype.
44
     * @param string $ns The namespace for these types
45
     * @return string[]
46
     */
47
    public static function scalars(array $datatypes, string $ns): array
48
    {
49
        $graphql = [ '# File automatically generated by Modelarium on ' . date('c') . "\n\n" ];
50
51
        foreach ($datatypes as $name) {
52
            $typeName = Str::studly($name);
53
54
            if ($typeName === 'String' ||
55
                $typeName === 'Boolean' ||
56
                $typeName === 'Int' ||
57
                $typeName === 'Float'
58
            ) {
59
                // base types
60
                continue;
61
            }
62
63
            $graphql[] = "scalar $typeName @scalar(class: \"" .
64
                str_replace('\\', '\\\\', $ns . '\\Datatype_' . $name) .
65
                "\")";
66
        }
67
        
68
        return $graphql;
69
    }
70
71
    /**
72
     * TODO
73
     * @throws
74
     *
75
    public static generateDirectives(string $filename): void
76
    {
77
        \Safe\file_put_contents(
78
            $filename
79
            LaravelProcessor::getDirectivesGraphqlString()
80
        );
81
    } */
82
}
83