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 — migrate-to-friendsofphp/php-cs... ( 0b5a57...4a9bcb )
by Cees-Jan
02:20
created

ResourceGenerator::applyPsr2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 14
ccs 9
cts 9
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 1
    public function __construct(array $configuration, callable $out = null)
47
    {
48 1
        $this->configuration = $configuration;
49 1
        $this->generators = $this->configuration['file_generators'];
50
51 1
        if (is_callable($out)) {
52 1
            $this->out = $out;
53
        }
54 1
    }
55
56 1
    public function run()
57
    {
58 1
        foreach ($this->configuration['files'] as $definition) {
59 1
            $this->out('-----');
60 1
            $this->out('- Definition: ' . $definition['class']);
61 1
            $definition = $this->applyAnnotationsToDefinition($definition);
62 1
            $this->generateFromDefinition($definition);
63 1
            $this->out('-----');
64
        }
65 1
    }
66
67 1
    protected function applyAnnotationsToDefinition(array $definition): array
68
    {
69 1
        foreach ($definition['properties'] as $property => $properties) {
70 1
            if (!isset($properties['annotations'])) {
71 1
                continue;
72
            }
73
74 1
            foreach ($properties['annotations'] as $annotation => $input) {
75 1
                if (!isset($this->configuration['annotation_handlers'][$annotation])) {
76
                    continue;
77
                }
78
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
                )) {
83
                    continue;
84
                }
85
86 1
                $definition = forward_static_call_array(
87
                    [
88 1
                        $this->configuration['annotation_handlers'][$annotation],
89 1
                        'handle',
90
                    ],
91
                    [
92 1
                        $property,
93 1
                        $definition,
94 1
                        $input,
95
                    ]
96
                );
97
            }
98
        }
99
100 1
        return $definition;
101
    }
102
103
    /**
104
     * @param array $file
105
     * @throws Exception
106
     */
107 1
    protected function generateFromDefinition(array $file)
108
    {
109 1
        $config = $this->configuration + $file;
110 1
        unset($config['files']);
111
112 1
        foreach ($this->generators as $generatorClass) {
113
            /** @var FileGeneratorInterface $generator */
114 1
            $generator = new $generatorClass($config);
115 1
            $fileName = $generator->getFilename();
116 1
            $this->out('----');
117 1
            $this->out('-- Generator: ' . $generatorClass);
118 1
            $this->out('-- File: ' . $fileName);
119 1
            $this->out('---');
120 1
            $this->out('-- Generating');
121 1
            $node = $generator->generate();
122 1
            $this->out('-- Printing');
123 1
            $code = $this->printCode($node);
124 1
            $this->out('-- Saving file');
125 1
            $continue = $this->save($fileName, $code);
126 1
            if (!$continue) {
127
                continue;
128
            }
129 1
            $this->out('-- Applying code standards');
130 1
            $this->applyPsr2($fileName);
131 1
            $this->out('----');
132
        }
133 1
    }
134
135
    /**
136
     * @param Node $node
137
     * @return string
138
     */
139 1
    protected function printCode(Node $node): string
140
    {
141 1
        $prettyPrinter = new PrettyPrinter\Standard();
142 1
        return $prettyPrinter->prettyPrintFile([
143 1
            $node
144 1
        ]) . PHP_EOL;
145
    }
146
147
    /**
148
     * @param string $fileName
149
     * @param string $fileContents
150
     * @return bool
151
     * @throws Exception
152
     */
153 1
    protected function save(string $fileName, string $fileContents)
154
    {
155 1
        $fileName = $this->configuration['root'] . $fileName;
156
157 1
        if (file_exists($fileName)) {
158
            $this->out('-- Exists');
159
            return false;
160
        }
161
162 1
        $directory = dirname($fileName);
163 1
        if (!file_exists($directory)) {
164 1
            mkdir($directory, 0755, true);
165
        }
166
167 1
        if (!file_exists($directory)) {
168
            throw new Exception('Unable to create: ' . $directory);
169
        }
170
171 1
        file_put_contents($fileName, $fileContents);
172
173
        do {
174 1
            usleep(500);
175 1
        } while (!file_exists($fileName));
176
177 1
        return true;
178
    }
179
180
    /**
181
     * @param string $fileName
182
     */
183 1
    protected function applyPsr2($fileName)
184
    {
185 1
        $fileName = $this->configuration['root'] . $fileName;
186
187
        $command = 'vendor/bin/php-cs-fixer fix ' .
188 1
            $fileName .
189 1
            ' --config=' .
190 1
            dirname(__DIR__) .
191 1
            DIRECTORY_SEPARATOR .
192
            '.php_cs ' .
193 1
            ' --allow-risky=yes -q -v --stop-on-violation --using-cache=no';
194
195 1
        system($command);
196 1
    }
197
198 1
    private function out(string $message)
199
    {
200 1
        $out = $this->out;
201 1
        $out($message);
202 1
    }
203
}
204