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.
Passed
Pull Request — master (#58)
by joseph
17:08
created

CodeHelper::resolvePath()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0099

Importance

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