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
Pull Request — master (#23)
by Cees-Jan
10:01
created

ResourceGenerator::applyPsr2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 14
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Tools\ResourceGenerator;
4
5
use Exception;
6
use PhpParser\Node;
7
use PhpParser\PrettyPrinter;
8
9
class ResourceGenerator
10
{
11
    /**
12
     * @var callable
13
     */
14
    protected $out = 'ApiClients\Tools\ResourceGenerator\outln';
15
16
    /**
17
     * @var array
18
     */
19
    protected $configuration;
20
21
    /**
22
     * @var FileGeneratorInterface[]
23
     */
24
    protected $generators = [];
25
26
    /**
27
     * @var string
28
     */
29
    protected $pathSrc;
30
31
    /**
32
     * @var string
33
     */
34
    protected $pathTests;
35
36
    /**
37
     * @var Fixer
38
     */
39
    protected $fixer;
40
41
    /**
42
     * @var array
43
     */
44
    protected $fixers;
45
46
    public function __construct(array $configuration, callable $out = null)
47
    {
48
        $this->configuration = $configuration;
49
        $this->generators = $this->configuration['file_generators'];
50
51 1
        if (is_callable($out)) {
52
            $this->out = $out;
53 1
        }
54 1
    }
55
56 1
    public function run()
57 1
    {
58
        foreach ($this->configuration['files'] as $definition) {
59
            $this->out('-----');
60 1
            $this->out('- Definition: ' . $definition['class']);
61 1
            $definition = $this->applyAnnotationsToDefinition($definition);
62
            $this->generateFromDefinition($definition);
63 1
            $this->out('-----');
64
        }
65 1
    }
66 1
67 1
    protected function applyAnnotationsToDefinition(array $definition): array
68 1
    {
69 1
        foreach ($definition['properties'] as $property => $properties) {
70 1
            if (!isset($properties['annotations'])) {
71 1
                continue;
72 1
            }
73 1
74 1
            foreach ($properties['annotations'] as $annotation => $input) {
75 1
                if (!isset($this->configuration['annotation_handlers'][$annotation])) {
76 1
                    continue;
77 1
                }
78 1
79 1
                if (!is_subclass_of(
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \ApiClients\Tools\Resour...HandlerInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
80 1
                    $this->configuration['annotation_handlers'][$annotation],
81 1
                    AnnotationHandlerInterface::class
82 1
                )) {
83 1
                    continue;
84 1
                }
85 1
86 1
                $definition = forward_static_call_array(
87 1
                    [
88 1
                        $this->configuration['annotation_handlers'][$annotation],
89 1
                        'handle',
90 1
                    ],
91
                    [
92 1
                        $property,
93 1
                        $definition,
94
                        $input,
95 1
                    ]
96 1
                );
97 1
            }
98
        }
99 1
100
        return $definition;
101 1
    }
102 1
103 1
    /**
104 1
     * @param  array     $file
105 1
     * @throws Exception
106 1
     */
107
    protected function generateFromDefinition(array $file)
108 1
    {
109
        $config = $this->configuration + $file;
110 1
        unset($config['files']);
111
112 1
        foreach ($this->generators as $generatorClass) {
113 1
            /** @var FileGeneratorInterface $generator */
114 1
            $generator = new $generatorClass($config);
115
            $fileName = $generator->getFilename();
116
            $this->out('----');
117 1
            $this->out('-- Generator: ' . $generatorClass);
118 1
            $this->out('-- File: ' . $fileName);
119
            $this->out('---');
120
            $this->out('-- Generating');
121
            $node = $generator->generate();
122 1
            $this->out('-- Printing');
123 1
            $code = $this->printCode($node);
124 1
            $this->out('-- Saving file');
125
            $continue = $this->save($fileName, $code);
126
            if (!$continue) {
127
                continue;
128
            }
129 1
            $this->out('-- Applying code standards');
130
            $this->applyPsr2($fileName);
131 1
            $this->out('----');
132 1
        }
133
    }
134
135 1
    /**
136 1
     * @param  Node   $node
137 1
     * @return string
138
     */
139
    protected function printCode(Node $node): string
140
    {
141
        $prettyPrinter = new PrettyPrinter\Standard();
142
143 1
        return $prettyPrinter->prettyPrintFile([
144
            $node,
145
        ]) . PHP_EOL;
146
    }
147
148
    /**
149
     * @param  string    $fileName
150 1
     * @param  string    $fileContents
151
     * @throws Exception
152 1
     * @return bool
153 1
     */
154
    protected function save(string $fileName, string $fileContents)
155 1
    {
156
        $fileName = $this->configuration['root'] . $fileName;
157 1
158 1
        if (file_exists($fileName)) {
159 1
            $this->out('-- Exists');
160 1
161 1
            return false;
162 1
        }
163 1
164 1
        $directory = dirname($fileName);
165 1
        if (!file_exists($directory)) {
166 1
            mkdir($directory, 0755, true);
167 1
        }
168 1
169 1
        if (!file_exists($directory)) {
170
            throw new Exception('Unable to create: ' . $directory);
171
        }
172 1
173 1
        file_put_contents($fileName, $fileContents);
174 1
175
        do {
176 1
            usleep(500);
177
        } while (!file_exists($fileName));
178
179
        return true;
180
    }
181
182 1
    /**
183
     * @param string $fileName
184 1
     */
185 1
    protected function applyPsr2($fileName)
186 1
    {
187 1
        $fileName = $this->configuration['root'] . $fileName;
188
189
        $command = 'vendor/bin/php-cs-fixer fix ' .
190
            $fileName .
191
            ' --config=' .
192
            dirname(__DIR__) .
193
            DIRECTORY_SEPARATOR .
194
            '.php_cs ' .
195
            ' --allow-risky=yes -q -v --stop-on-violation --using-cache=no';
196 1
197
        system($command);
198 1
    }
199
200 1
    private function out(string $message)
201
    {
202
        $out = $this->out;
203
        $out($message);
204
    }
205
}
206