GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 54317d...e863e3 )
by Cees-Jan
20:35
created

ResourceGenerator::createInterface()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.8571
cc 3
eloc 17
nc 3
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\ApiClient\Tools;
5
6
use Doctrine\Common\Inflector\Inflector;
7
use League\CLImate\CLImate;
8
use PhpParser\Builder\Method;
9
use PhpParser\Builder\Property;
10
use PhpParser\BuilderFactory;
11
use PhpParser\PrettyPrinter;
12
use PhpParser\Node;
13
use Symfony\Component\Yaml\Yaml;
14
15
class ResourceGenerator
16
{
17
    protected $climate;
18
19
    public function __construct(CLImate $climate)
20
    {
21
        $this->climate = $climate;
22
23
        $this->setUpArguments();
24
    }
25
26
    protected function setUpArguments()
27
    {
28
        $this->climate->arguments->add([
29
            'definition' => [
30
                'description' => 'YAML definition file',
31
                'required'    => true,
32
            ],
33
            'path' => [
34
                'description' => 'Path to the resource directory',
35
                'required'    => true,
36
            ],
37
            'sync' => [
38
                'prefix'       => 's',
39
                'longPrefix'   => 'sync',
40
                'defaultValue' => true,
41
                'noValue'      => false,
42
                'description'  => 'Don\'t generate Sync resource',
43
                'castTo'       => 'bool',
44
            ],
45
            'async' => [
46
                'prefix'       => 'as',
47
                'longPrefix'   => 'async',
48
                'defaultValue' => true,
49
                'noValue'      => false,
50
                'description'  => 'Don\'t generate Async resource',
51
                'castTo'       => 'bool',
52
            ],
53
        ]);
54
    }
55
56
    public function run()
57
    {
58
        $yaml = $this->readYaml($this->climate->arguments->get('definition'));
59
        file_put_contents(
60
            $this->climate->arguments->get('path') . DIRECTORY_SEPARATOR . $yaml['class'] . '.php',
61
            $this->createBaseClass($yaml)
62
        );
63
        file_put_contents(
64
            $this->climate->arguments->get('path') . DIRECTORY_SEPARATOR . $yaml['class'] . 'Interface.php',
65
            $this->createInterface($yaml)
66
        );
67
        file_put_contents(
68
            $this->climate->arguments->get('path') . DIRECTORY_SEPARATOR . 'Async' . DIRECTORY_SEPARATOR . $yaml['class'] . '.php',
69
            $this->createExtendingClass('Async', $yaml)
70
        );
71
        file_put_contents(
72
            $this->climate->arguments->get('path') . DIRECTORY_SEPARATOR . 'Sync' . DIRECTORY_SEPARATOR . $yaml['class'] . '.php',
73
            $this->createExtendingClass('Sync', $yaml)
74
        );
75
    }
76
77
    protected function readYaml(string $filename): array
78
    {
79
        return Yaml::parse(file_get_contents($filename));
80
    }
81
82
    protected function createBaseClass(array $yaml)
83
    {
84
        $factory = new BuilderFactory;
85
86
        $class = $factory->class($yaml['class'])
87
            ->implement($yaml['class'] . 'Interface')
88
            ->makeAbstract();
89
        $class->addStmt(
90
            new Node\Stmt\TraitUse([
91
                new Node\Name('TransportAwareTrait')
92
            ])
93
        );
94
95
        foreach ($yaml['properties'] as $name => $details) {
96
            $type = $details;
97
            if (is_array($details)) {
98
                $type = $details['type'];
99
            }
100
            $class->addStmt($this->createProperty($factory, $type, $name, $details));
101
            $class->addStmt($this->createMethod($factory, $type, $name, $details));
102
        }
103
104
        $node = $factory->namespace($yaml['namespace'])
105
            ->addStmt($factory->use('WyriHaximus\ApiClient\Resource\TransportAwareTrait'))
106
            ->addStmt($class)
107
108
            ->getNode()
109
        ;
110
111
        $prettyPrinter = new PrettyPrinter\Standard();
112
        return $prettyPrinter->prettyPrintFile([
113
            $node
114
        ]) . PHP_EOL;
115
    }
116
117
    protected function createInterface(array $yaml)
118
    {
119
        $factory = new BuilderFactory;
120
121
        $class = $factory->interface($yaml['class'] . 'Interface')
122
            ->extend('ResourceInterface');
123
124
        foreach ($yaml['properties'] as $name => $details) {
125
            $type = $details;
126
            if (is_array($details)) {
127
                $type = $details['type'];
128
            }
129
            $class->addStmt($this->createMethod($factory, $type, $name, $details));
130
        }
131
132
        $node = $factory->namespace($yaml['namespace'])
133
            ->addStmt($factory->use('WyriHaximus\ApiClient\Resource\ResourceInterface'))
134
            ->addStmt($class)
135
            ->getNode()
136
        ;
137
138
        $prettyPrinter = new PrettyPrinter\Standard();
139
        return $prettyPrinter->prettyPrintFile([
140
            $node
141
        ]) . PHP_EOL;
142
    }
143
144
    protected function createProperty(BuilderFactory $factory, string $type, string $name, $details): Property
145
    {
146
        $property = $factory->property($name)
147
            ->makeProtected()
148
            ->setDocComment('/**
149
                              * @var ' . $type . '
150
                              */');
151
        if (isset($details['default'])) {
152
            $property->setDefault($details['default']);
153
        }
154
155
        return $property;
156
    }
157
158
    protected function createMethod(BuilderFactory $factory, string $type, string $name, $details): Method
0 ignored issues
show
Unused Code introduced by
The parameter $details is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        return $factory->method(Inflector::camelize($name))
161
            ->makePublic()
162
            ->setReturnType($type)
163
            ->setDocComment('/**
164
                              * @return ' . $type . '
165
                              */')
166
            ->addStmt(
167
                new Node\Stmt\Return_(
168
                    new Node\Expr\PropertyFetch(
169
                        new Node\Expr\Variable('this'),
170
                        $name
171
                    )
172
                )
173
            );
174
    }
175
176
    protected function createExtendingClass(string $type, array $yaml)
177
    {
178
179
        $factory = new BuilderFactory;
180
181
        $class = $factory->class($yaml['class'])
182
            ->extend('Base' . $yaml['class']);
183
184
        $node = $factory->namespace($yaml['namespace'] . '\\' . $type )
185
            ->addStmt($factory->use($yaml['namespace'] . '\\' . $yaml['class'])->as('Base' . $yaml['class']))
186
            ->addStmt($class)
187
188
            ->getNode()
189
        ;
190
191
        $prettyPrinter = new PrettyPrinter\Standard();
192
        return $prettyPrinter->prettyPrintFile([
193
            $node
194
        ]) . PHP_EOL;
195
    }
196
}
197