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 ( 810b23...1b23c8 )
by joseph
12s
created

CodeHelper::consty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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