Passed
Push — master ( 845ff4...7a0761 )
by Bruno
03:38
created

createDirective()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 33
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 50
rs 9.392
1
<?php declare(strict_types=1);
2
3
require(__DIR__ . '/../vendor/autoload.php');
4
5
use Illuminate\Support\Str;
6
use LightnCandy\LightnCandy;
7
8
use function Safe\file_put_contents;
9
10
function createDirective(string $name, array $processors)
11
{
12
    $parameters = [
13
        'name' => $name,
14
        'namespace' => 'Modelarium\\Laravel\\Lighthouse\\Directives',
15
        'studlyName' => Str::studly($name),
16
        'event' => in_array('event', $processors),
17
        'factory' => in_array('factory', $processors),
18
        'model' => in_array('model', $processors),
19
        'migration' => in_array('migration', $processors),
20
        'policy' => in_array('policy', $processors),
21
        'seed' => in_array('seed', $processors),
22
        'implements' => implode(
23
            ', ',
24
            array_map(
25
                function ($i) {
26
                    return Str::studly($i) . 'DirectiveInterface';
27
                },
28
                $processors
29
            )
30
        )
31
    ];
32
    $template = \Safe\file_get_contents(__DIR__ . '/directive.lighthouse.mustache');
33
    $phpStr = LightnCandy::compile(
34
        $template,
35
        [
36
            'flags' => LightnCandy::FLAG_ERROR_EXCEPTION
37
        ]
38
    );
39
    if (!$phpStr) {
40
        throw new Exception('Invalid template');
41
    }
42
    /** @var callable $renderer */
43
    $renderer = LightnCandy::prepare($phpStr);
0 ignored issues
show
Deprecated Code introduced by
The function LightnCandy\LightnCandy::prepare() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

43
    $renderer = /** @scrutinizer ignore-deprecated */ LightnCandy::prepare($phpStr);
Loading history...
44
    file_put_contents(__DIR__ . '/../Modelarium/Laravel/Lighthouse/Directives/' . $parameters['studlyName'] . 'Directive.php', $renderer($parameters));
45
46
    $template = \Safe\file_get_contents(__DIR__ . '/directive.mustache');
47
    $phpStr = LightnCandy::compile(
48
        $template,
49
        [
50
            'flags' => LightnCandy::FLAG_ERROR_EXCEPTION
51
        ]
52
    );
53
    if (!$phpStr) {
54
        throw new Exception('Invalid template');
55
    }
56
    /** @var callable $renderer */
57
    $renderer = LightnCandy::prepare($phpStr);
0 ignored issues
show
Deprecated Code introduced by
The function LightnCandy\LightnCandy::prepare() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
    $renderer = /** @scrutinizer ignore-deprecated */ LightnCandy::prepare($phpStr);
Loading history...
58
    $renderer($parameters);
59
    file_put_contents(__DIR__ . '/../Modelarium/Laravel/Directives/' . $parameters['studlyName'] . 'Directive.php', $renderer($parameters));
60
}
61
62
// Script example.php
63
$longopts  = array(
64
    "name:",
65
    "processors:"
66
);
67
$options = getopt('', $longopts);
68
createDirective($options['name'], explode(',', $options['processors']));
69