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 ( a48bd8...83d092 )
by joseph
16:18 queued 18s
created

CodeHelper::declareStrictFirstLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
7
use gossi\codegen\generator\CodeFileGenerator;
8
use gossi\codegen\model\GenerateableInterface;
9
10
/**
11
 * Class CodeHelper
12
 *
13
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration
14
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
15
 * @SuppressWarnings(PHPMD.StaticAccess)
16
 */
17
class CodeHelper
18
{
19
20
    /**
21
     * @var NamespaceHelper
22
     */
23
    private $namespaceHelper;
24
25
    public function __construct(NamespaceHelper $namespaceHelper)
26
    {
27
        $this->namespaceHelper = $namespaceHelper;
28
    }
29
30
    public function propertyIsh(string $name): string
31
    {
32
        return lcfirst($this->classy($name));
33
    }
34
35
    public function classy(string $name): string
36
    {
37
        return Inflector::classify($name);
38
    }
39
40
    public function consty(string $name): string
41
    {
42
        if (0 === \preg_match('%[^A-Z_]%', $name)) {
43
            return $name;
44
        }
45
46
        return strtoupper(Inflector::tableize($name));
47
    }
48
49
    /**
50
     * @param string $filePath
51
     *
52
     * @throws \RuntimeException
53
     */
54
    public function tidyNamespacesInFile(string $filePath): void
55
    {
56
        $contents = \ts\file_get_contents($filePath);
57
        $contents = preg_replace_callback(
58
            /**
59
            * @param $matches
60
            *
61
            * @return string
62
            */
63
            '%(namespace|use) (.+?);%',
64
            function ($matches): string {
65
                return $matches[1] . ' ' . $this->namespaceHelper->tidy($matches[2]) . ';';
66
            },
67
            $contents
68
        );
69
        file_put_contents($filePath, $contents);
70
    }
71
72
    /**
73
     * We use the string type hint as our default in templates
74
     *
75
     * This method will then replace those with the updated type
76
     *
77
     * @param string $filePath
78
     * @param string $type
79
     * @param string $dbalType
80
     * @param bool   $isNullable
81
     */
82
    public function replaceTypeHintsInFile(
83
        string $filePath,
84
        string $type,
85
        string $dbalType,
86
        bool $isNullable
87
    ): void {
88
        $contents = \ts\file_get_contents($filePath);
89
90
        $search = [
91
            ': string;',
92
            '(string $',
93
            ': string {',
94
            '@var string',
95
            '@return string',
96
            '@param string',
97
98
        ];
99
100
        $replaceNormal   = [
101
            ": $type;",
102
            "($type $",
103
            ": $type {",
104
            "@var $type",
105
            "@return $type",
106
            "@param $type",
107
        ];
108
        $replaceNullable = [
109
            ": ?$type;",
110
            "(?$type $",
111
            ": ?$type {",
112
            "@var $type|null",
113
            "@return $type|null",
114
            "@param $type|null",
115
        ];
116
        $replaceRemove   = [
117
            ';',
118
            '($',
119
            ' {',
120
            '',
121
            '',
122
            '',
123
        ];
124
125
        $replace = $replaceNormal;
126
127
        if (\in_array($dbalType, MappingHelper::MIXED_TYPES, true)) {
128
            $replace = $replaceRemove;
129
        } elseif ($isNullable) {
130
            $replace = $replaceNullable;
131
        }
132
133
        $contents = \str_replace(
134
            $search,
135
            $replace,
136
            $contents
137
        );
138
139
        \file_put_contents($filePath, $contents);
140
    }
141
142
    public function generate(
143
        GenerateableInterface $generateable,
144
        string $filePath,
145
        ?PostProcessorInterface $postProcessor = null
146
    ): void {
147
        $generator = new CodeFileGenerator(
148
            [
149
                'generateDocblock'   => false,
150
                'declareStrictTypes' => true,
151
            ]
152
        );
153
154
        $generated = $generator->generate($generateable);
155
        $generated = $this->postProcessGeneratedCode($generated, $postProcessor);
156
        \file_put_contents($filePath, $generated);
157
    }
158
159
    /**
160
     * Fix niggles with code that is generated by gossi/php-code-generator
161
     *
162
     * @param string                      $generated
163
     *
164
     * @param PostProcessorInterface|null $postProcessor
165
     *
166
     * @return string
167
     */
168
    public function postProcessGeneratedCode(string $generated, ?PostProcessorInterface $postProcessor = null): string
169
    {
170
171
        $generated = $this->fixSuppressWarningsTags($generated);
172
        $generated = $this->breakImplementsAndExtendsOntoLines($generated);
173
        $generated = $this->makeConstsPublic($generated);
174
        $generated = $this->constArraysOnMultipleLines($generated);
175
        $generated = $this->phpcsIgnoreUseSection($generated);
176
        $generated = $this->declareStrictFirstLine($generated);
177
        if (null !== $postProcessor) {
178
            $generated = $postProcessor($generated);
179
        }
180
181
        return $generated;
182
    }
183
184
    public function fixSuppressWarningsTags(string $generated): string
185
    {
186
        return str_replace('SuppressWarnings (', 'SuppressWarnings(', $generated);
187
    }
188
189
    public function breakImplementsAndExtendsOntoLines(string $generated): string
190
    {
191
        return preg_replace_callback(
192
            '%(class|interface) (.+?) (implements|extends) (.+?){%s',
193
            function ($matches) {
194
                return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3] . ' '
195
                       . "\n    "
196
                       . trim(
197
                           implode(
198
                               ",\n    ",
199
                               explode(
200
                                   ', ',
201
                                   $matches[4]
202
                               )
203
                           )
204
                       ) . "\n{";
205
            },
206
            $generated
207
        );
208
    }
209
210
    public function makeConstsPublic(string $generated): string
211
    {
212
        return \str_replace("\tconst", "\tpublic const", $generated);
213
    }
214
215
    public function constArraysOnMultipleLines(string $generated): string
216
    {
217
        return preg_replace_callback(
218
            "%(.*?)const ([A-Z_0-9]+?) = \[([^\]]+?)\];%",
219
            function ($matches) {
220
                return $matches[1] . 'const ' . $matches[2] . " = [\n        "
221
                       . trim(
222
                           implode(
223
                               ",\n        ",
224
                               explode(
225
                                   ', ',
226
                                   $matches[3]
227
                               )
228
                           )
229
                       ) . "\n    ];";
230
            },
231
            $generated
232
        );
233
    }
234
235
    /**
236
     * Use section can become long and fail line length limits. I don't care about line length here
237
     *
238
     * @param string $generated
239
     *
240
     * @return string
241
     */
242
    public function phpcsIgnoreUseSection(string $generated): string
243
    {
244
        return preg_replace(
245
            '%namespace (.+?);(.+?)(class|trait|interface) %si',
246
            "namespace \$1;\n// phpcs:disable Generic.Files.LineLength.TooLong\$2// phpcs:enable\n\$3 ",
247
            $generated
248
        );
249
    }
250
251
    public function declareStrictFirstLine(string $generated): string
252
    {
253
        return preg_replace('%php\s+declare%', 'php declare', $generated);
254
    }
255
256
    public function getGetterMethodNameForBoolean(string $fieldName): string
257
    {
258
        if (0 === stripos($fieldName, 'is')) {
259
            return lcfirst($fieldName);
260
        }
261
262
        if (0 === stripos($fieldName, 'has')) {
263
            return lcfirst($fieldName);
264
        }
265
266
        return 'is' . ucfirst($fieldName);
267
    }
268
}
269