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 ( b4e28f...1452a3 )
by Cees-Jan
02:11
created

ResourceGenerator   C

Complexity

Total Complexity 27

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 30

Test Coverage

Coverage 93.89%

Importance

Changes 0
Metric Value
dl 0
loc 261
ccs 123
cts 131
cp 0.9389
rs 5
c 0
b 0
f 0
wmc 27
lcom 1
cbo 30

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B setUpFixers() 0 35 1
A run() 0 10 2
B applyAnnotationsToDefinition() 0 35 6
B generateFromDefinition() 0 27 3
A printCode() 0 7 1
B save() 0 26 5
A prepareFixers() 0 12 3
A out() 0 5 1
B applyPsr2() 0 24 3
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Tools\ResourceGenerator;
4
5
use Exception;
6
use PhpParser\PrettyPrinter;
7
use PhpParser\Node;
8
use Symfony\CS\Config\Config;
9
use Symfony\CS\ConfigAwareInterface;
10
use Symfony\CS\ConfigInterface;
11
use Symfony\CS\Fixer;
12
use Symfony\CS\FixerInterface;
13
14
class ResourceGenerator
15
{
16
    /**
17
     * @var callable
18
     */
19
    protected $out = 'ApiClients\Tools\ResourceGenerator\outln';
20
21
    /**
22
     * @var array
23
     */
24
    protected $configuration;
25
26
    /**
27
     * @var FileGeneratorInterface[]
28
     */
29
    protected $generators = [];
30
31
    /**
32
     * @var string
33
     */
34
    protected $pathSrc;
35
36
    /**
37
     * @var string
38
     */
39
    protected $pathTests;
40
41
    /**
42
     * @var Fixer
43
     */
44
    protected $fixer;
45
46
    /**
47
     * @var array
48
     */
49
    protected $fixers;
50
51 1
    public function __construct(array $configuration, callable $out = null)
52
    {
53 1
        $this->configuration = $configuration;
54 1
        $this->generators = $this->configuration['file_generators'];
55
56 1
        if (is_callable($out)) {
57 1
            $this->out = $out;
58
        }
59
60 1
        $this->setUpFixers();
61 1
    }
62
63 1
    protected function setUpFixers()
64
    {
65 1
        $this->fixer = new Fixer();
66 1
        $this->fixer->registerCustomFixers([
67 1
            new Fixer\Symfony\ExtraEmptyLinesFixer(),
68 1
            new Fixer\Symfony\SingleBlankLineBeforeNamespaceFixer(),
69 1
            new Fixer\PSR0\Psr0Fixer(),
70 1
            new Fixer\PSR1\EncodingFixer(),
71 1
            new Fixer\PSR1\ShortTagFixer(),
72 1
            new Fixer\PSR2\BracesFixer(),
73 1
            new Fixer\PSR2\ElseifFixer(),
74 1
            new Fixer\PSR2\EofEndingFixer(),
75 1
            new Fixer\PSR2\FunctionCallSpaceFixer(),
76 1
            new Fixer\PSR2\FunctionDeclarationFixer(),
77 1
            new Fixer\PSR2\IndentationFixer(),
78 1
            new Fixer\PSR2\LineAfterNamespaceFixer(),
79 1
            new Fixer\PSR2\LinefeedFixer(),
80 1
            new Fixer\PSR2\LowercaseConstantsFixer(),
81 1
            new Fixer\PSR2\LowercaseKeywordsFixer(),
82 1
            new Fixer\PSR2\MethodArgumentSpaceFixer(),
83 1
            new Fixer\PSR2\MultipleUseFixer(),
84 1
            new Fixer\PSR2\ParenthesisFixer(),
85 1
            new Fixer\PSR2\PhpClosingTagFixer(),
86 1
            new Fixer\PSR2\SingleLineAfterImportsFixer(),
87 1
            new Fixer\PSR2\TrailingSpacesFixer(),
88 1
            new Fixer\PSR2\VisibilityFixer(),
89 1
            new Fixer\Contrib\NewlineAfterOpenTagFixer(),
90 1
            new EmptyLineAboveDocblocksFixer(),
91
        ]);
92 1
        $config = Config::create()->
93 1
            fixers($this->fixer->getFixers())
94
        ;
95 1
        $this->fixer->addConfig($config);
96 1
        $this->fixers = $this->prepareFixers($config);
97 1
    }
98
99 1
    public function run()
100
    {
101 1
        foreach ($this->configuration['files'] as $definition) {
102 1
            $this->out('-----');
103 1
            $this->out('- Definition: ' . $definition['class']);
104 1
            $definition = $this->applyAnnotationsToDefinition($definition);
105 1
            $this->generateFromDefinition($definition);
106 1
            $this->out('-----');
107
        }
108 1
    }
109
110 1
    protected function applyAnnotationsToDefinition(array $definition): array
111
    {
112 1
        foreach ($definition['properties'] as $property => $properties) {
113 1
            if (!isset($properties['annotations'])) {
114 1
                continue;
115
            }
116
117 1
            foreach ($properties['annotations'] as $annotation => $input) {
118 1
                if (!isset($this->configuration['annotation_handlers'][$annotation])) {
119
                    continue;
120
                }
121
122 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...
123 1
                    $this->configuration['annotation_handlers'][$annotation],
124 1
                    AnnotationHandlerInterface::class
125
                )) {
126
                    continue;
127
                }
128
129 1
                $definition = forward_static_call_array(
130
                    [
131 1
                        $this->configuration['annotation_handlers'][$annotation],
132 1
                        'handle',
133
                    ],
134
                    [
135 1
                        $property,
136 1
                        $definition,
137 1
                        $input,
138
                    ]
139
                );
140
            }
141
        }
142
143 1
        return $definition;
144
    }
145
146
    /**
147
     * @param array $file
148
     * @throws Exception
149
     */
150 1
    protected function generateFromDefinition(array $file)
151
    {
152 1
        $config = $this->configuration + $file;
153 1
        unset($config['files']);
154
155 1
        foreach ($this->generators as $generatorClass) {
156
            /** @var FileGeneratorInterface $generator */
157 1
            $generator = new $generatorClass($config);
158 1
            $fileName = $generator->getFilename();
159 1
            $this->out('----');
160 1
            $this->out('-- Generator: ' . $generatorClass);
161 1
            $this->out('-- File: ' . $fileName);
162 1
            $this->out('---');
163 1
            $this->out('-- Generating');
164 1
            $node = $generator->generate();
165 1
            $this->out('-- Printing');
166 1
            $code = $this->printCode($node);
167 1
            $this->out('-- Saving file');
168 1
            $continue = $this->save($fileName, $code);
169 1
            if (!$continue) {
170
                continue;
171
            }
172 1
            $this->out('-- Applying code standards');
173 1
            $this->applyPsr2($fileName);
174 1
            $this->out('----');
175
        }
176 1
    }
177
178
    /**
179
     * @param Node $node
180
     * @return string
181
     */
182 1
    protected function printCode(Node $node): string
183
    {
184 1
        $prettyPrinter = new PrettyPrinter\Standard();
185 1
        return $prettyPrinter->prettyPrintFile([
186 1
            $node
187 1
        ]) . PHP_EOL;
188
    }
189
190
    /**
191
     * @param string $fileName
192
     * @param string $fileContents
193
     * @return bool
194
     * @throws Exception
195
     */
196 1
    protected function save(string $fileName, string $fileContents)
197
    {
198 1
        $fileName = $this->configuration['root'] . $fileName;
199
200 1
        if (file_exists($fileName)) {
201
            $this->out('-- Exists');
202
            return false;
203
        }
204
205 1
        $directory = dirname($fileName);
206 1
        if (!file_exists($directory)) {
207 1
            mkdir($directory, 0755, true);
208
        }
209
210 1
        if (!file_exists($directory)) {
211
            throw new Exception('Unable to create: ' . $directory);
212
        }
213
214 1
        file_put_contents($fileName, $fileContents);
215
216
        do {
217 1
            usleep(500);
218 1
        } while (!file_exists($fileName));
219
220 1
        return true;
221
    }
222
223
    /**
224
     * @param string $fileName
225
     */
226 1
    protected function applyPsr2($fileName)
227
    {
228 1
        $fileName = $this->configuration['root'] . $fileName;
229
230 1
        $file = new \SplFileInfo($fileName);
231 1
        $new = file_get_contents($file->getRealPath());
232
233 1
        foreach ($this->fixers as $fixer) {
234 1
            if (!$fixer->supports($file)) {
235
                continue;
236
            }
237
238 1
            $new = $fixer->fix($file, $new);
239
        }
240
241 1
        file_put_contents(
242
            $fileName,
243 1
            str_replace(
244 1
                '<?php',
245 1
                '<?php declare(strict_types=1);',
246 1
                $new
247
            )
248
        );
249 1
    }
250
251
    /**
252
     * @param ConfigInterface $config
253
     *
254
     * @return FixerInterface[]
255
     */
256 1
    private function prepareFixers(ConfigInterface $config): array
257
    {
258 1
        $fixers = $config->getFixers();
259
260 1
        foreach ($fixers as $fixer) {
261 1
            if ($fixer instanceof ConfigAwareInterface) {
262 1
                $fixer->setConfig($config);
263
            }
264
        }
265
266 1
        return $fixers;
267
    }
268
269 1
    private function out(string $message)
270
    {
271 1
        $out = $this->out;
272 1
        $out($message);
273 1
    }
274
}
275