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 ( 91f608...0bb894 )
by Cees-Jan
08:04 queued 06:09
created

ResourceGenerator::run()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 36
ccs 0
cts 31
cp 0
rs 8.8571
cc 1
eloc 30
nc 1
nop 0
crap 2
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 1
    public function __construct(CLImate $climate)
20
    {
21 1
        $this->climate = $climate;
22
23 1
        $this->setUpArguments();
24 1
    }
25
26 1
    protected function setUpArguments()
27
    {
28 1
        $this->climate->arguments->add([
29
            'definition' => [
30
                'description' => 'YAML definition file',
31
                'required'    => true,
32 1
            ],
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 1
    }
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') .
61
                DIRECTORY_SEPARATOR .
62
                $yaml['class'] .
63
                '.php',
64
            $this->createBaseClass($yaml)
65
        );
66
        file_put_contents(
67
            $this->climate->arguments->get('path') .
68
                DIRECTORY_SEPARATOR .
69
                $yaml['class'] .
70
                'Interface.php',
71
            $this->createInterface($yaml)
72
        );
73
        file_put_contents(
74
            $this->climate->arguments->get('path') .
75
                DIRECTORY_SEPARATOR .
76
                'Async' .
77
                DIRECTORY_SEPARATOR .
78
                $yaml['class'] .
79
                '.php',
80
            $this->createExtendingClass('Async', $yaml)
81
        );
82
        file_put_contents(
83
            $this->climate->arguments->get('path') .
84
                DIRECTORY_SEPARATOR .
85
                'Sync' .
86
                DIRECTORY_SEPARATOR .
87
                $yaml['class'] .
88
                '.php',
89
            $this->createExtendingClass('Sync', $yaml)
90
        );
91
    }
92
93
    protected function readYaml(string $filename): array
94
    {
95
        return Yaml::parse(file_get_contents($filename));
96
    }
97
98
    protected function createBaseClass(array $yaml)
99
    {
100
        $factory = new BuilderFactory;
101
102
        $class = $factory->class($yaml['class'])
103
            ->implement($yaml['class'] . 'Interface')
104
            ->makeAbstract();
105
        $class->addStmt(
106
            new Node\Stmt\TraitUse([
107
                new Node\Name('TransportAwareTrait')
108
            ])
109
        );
110
111
        foreach ($yaml['properties'] as $name => $details) {
112
            $type = $details;
113
            if (is_array($details)) {
114
                $type = $details['type'];
115
            }
116
            $class->addStmt($this->createProperty($factory, $type, $name, $details));
117
            $class->addStmt($this->createMethod($factory, $type, $name, $details));
118
        }
119
120
        $node = $factory->namespace($yaml['namespace'])
121
            ->addStmt($factory->use('WyriHaximus\ApiClient\Resource\TransportAwareTrait'))
122
            ->addStmt($class)
123
124
            ->getNode()
125
        ;
126
127
        $prettyPrinter = new PrettyPrinter\Standard();
128
        return $prettyPrinter->prettyPrintFile([
129
            $node
130
        ]) . PHP_EOL;
131
    }
132
133
    protected function createInterface(array $yaml)
134
    {
135
        $factory = new BuilderFactory;
136
137
        $class = $factory->interface($yaml['class'] . 'Interface')
138
            ->extend('ResourceInterface');
139
140
        foreach ($yaml['properties'] as $name => $details) {
141
            $type = $details;
142
            if (is_array($details)) {
143
                $type = $details['type'];
144
            }
145
            $class->addStmt($this->createMethod($factory, $type, $name, $details));
146
        }
147
148
        $node = $factory->namespace($yaml['namespace'])
149
            ->addStmt($factory->use('WyriHaximus\ApiClient\Resource\ResourceInterface'))
150
            ->addStmt($class)
151
            ->getNode()
152
        ;
153
154
        $prettyPrinter = new PrettyPrinter\Standard();
155
        return $prettyPrinter->prettyPrintFile([
156
            $node
157
        ]) . PHP_EOL;
158
    }
159
160
    protected function createProperty(BuilderFactory $factory, string $type, string $name, $details): Property
161
    {
162
        $property = $factory->property($name)
163
            ->makeProtected()
164
            ->setDocComment('/**
165
                              * @var ' . $type . '
166
                              */');
167
        if (isset($details['default'])) {
168
            $property->setDefault($details['default']);
169
        }
170
171
        return $property;
172
    }
173
174
    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...
175
    {
176
        return $factory->method(Inflector::camelize($name))
177
            ->makePublic()
178
            ->setReturnType($type)
179
            ->setDocComment('/**
180
                              * @return ' . $type . '
181
                              */')
182
            ->addStmt(
183
                new Node\Stmt\Return_(
184
                    new Node\Expr\PropertyFetch(
185
                        new Node\Expr\Variable('this'),
186
                        $name
187
                    )
188
                )
189
            );
190
    }
191
192
    protected function createExtendingClass(string $type, array $yaml)
193
    {
194
        $factory = new BuilderFactory;
195
196
        $class = $factory->class($yaml['class'])
197
            ->extend('Base' . $yaml['class']);
198
199
        $class->addStmt($factory->method('refresh')
200
            ->makePublic()
201
            ->setReturnType($yaml['class'])
202
            ->addStmt(
203
                new Node\Stmt\Return_(
204
                    new Node\Expr\MethodCall(
205
                        new Node\Expr\Variable('this'),
206
                        'wait',
207
                        [
0 ignored issues
show
Documentation introduced by
array(new \PhpParser\Nod...r\String_('refresh')))) is of type array<integer,object<Php...e\\Expr\\MethodCall>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
208
                            new Node\Expr\MethodCall(
209
                                new Node\Expr\Variable('this'),
210
                                'callAsync',
211
                                [
0 ignored issues
show
Documentation introduced by
array(new \PhpParser\Nod...lar\String_('refresh')) is of type array<integer,object<Php...de\\Scalar\\String_>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
                                    new Node\Scalar\String_('refresh'),
213
                                ]
214
                            ),
215
                        ]
216
                    )
217
                )
218
            ));
219
220
        $node = $factory->namespace($yaml['namespace'] . '\\' . $type)
221
            ->addStmt($factory->use($yaml['namespace'] . '\\' . $yaml['class'])->as('Base' . $yaml['class']))
222
            ->addStmt($class)
223
224
            ->getNode()
225
        ;
226
227
        $prettyPrinter = new PrettyPrinter\Standard();
228
        return $prettyPrinter->prettyPrintFile([
229
            $node
230
        ]) . PHP_EOL;
231
    }
232
}
233