Issues (150)

src/Blocks/ContentManager.php (1 issue)

calls to non-existent methods.

Bug Major
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Controllers\BaseCommand;
6
use SoliDry\Helpers\Console;
7
use SoliDry\Helpers\MethodOptions;
8
use SoliDry\Types\DefaultInterface;
9
use SoliDry\Types\PhpInterface;
10
use SoliDry\Types\ApiInterface;
11
12
/**
13
 * Class ContentManager
14
 *
15
 * @package SoliDry\Blocks
16
 * @property BaseCommand generator
17
 * @property string sourceCode
18
 */
19
trait ContentManager
20
{
21
22
    /**
23
     *  Sets <?php open tag for source code
24
     */
25
    protected function setTag(): void
26
    {
27
        $this->sourceCode = PhpInterface::PHP_OPEN_TAG . PHP_EOL;
28
    }
29
30
    /**
31
     * @param string $postfix
32
     */
33
    protected function setNamespace(string $postfix): void
34
    {
35
        $this->sourceCode .= PhpInterface::PHP_NAMESPACE . PhpInterface::SPACE .
36
            $this->generator->modulesDir . PhpInterface::BACKSLASH .
37
            strtoupper($this->generator->version) .
38
            PhpInterface::BACKSLASH . $postfix . PhpInterface::SEMICOLON . PHP_EOL . PHP_EOL;
39
    }
40
41
    /**
42
     * @param string $path
43
     * @param bool $isTrait
44
     * @param bool $isLast
45
     */
46
    protected function setUse(string $path, bool $isTrait = false, bool $isLast = false): void
47
    {
48
        $this->sourceCode .= (($isTrait === false) ? '' : PhpInterface::TAB_PSR4) .
49
            PhpInterface::PHP_USE . PhpInterface::SPACE . $path . PhpInterface::SEMICOLON .
50
            PHP_EOL . (($isLast === false) ? '' : PHP_EOL);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @param null $extends
56
     */
57
    protected function startClass(string $name, $extends = null): void
58
    {
59
        $this->sourceCode .= PhpInterface::PHP_CLASS . PhpInterface::SPACE . $name
60
            . PhpInterface::SPACE;
61
        if ($extends !== null) {
62
            $this->sourceCode .=
63
                PhpInterface::PHP_EXTENDS
64
                . PhpInterface::SPACE . $extends . PhpInterface::SPACE;
65
        }
66
        $this->sourceCode .= PHP_EOL . PhpInterface::OPEN_BRACE . PHP_EOL;
67
    }
68
69
    /**
70
     *  Ends class declaration
71
     */
72
    protected function endClass(): void
73
    {
74
        $this->sourceCode .= PhpInterface::CLOSE_BRACE . PHP_EOL;
75
    }
76
77
    /**
78
     * @param MethodOptions $methodOptions
79
     */
80
    protected function startMethod(MethodOptions $methodOptions): void
81
    {
82
        // get params
83
        $params = $this->getMethodParams($methodOptions->getParams());
84
        $this->sourceCode .= PhpInterface::TAB_PSR4 . $methodOptions->getModifier() . PhpInterface::SPACE .
85
            (($methodOptions->isStatic() !== false) ? PhpInterface::PHP_STATIC . PhpInterface::SPACE :
86
                '') .
87
            PhpInterface::PHP_FUNCTION . PhpInterface::SPACE .
88
            $methodOptions->getName()
89
            . PhpInterface::OPEN_PARENTHESES . $params . PhpInterface::CLOSE_PARENTHESES .
90
            ((empty($methodOptions->getReturnType())) ? '' :
91
                PhpInterface::COLON . PhpInterface::SPACE . $methodOptions->getReturnType()) .
92
            PhpInterface::SPACE . PHP_EOL . PhpInterface::TAB_PSR4
93
            . PhpInterface::OPEN_BRACE . PHP_EOL;
94
    }
95
96
    /**
97
     * Sets return stmt for any generated method
98
     *
99
     * @param string $value
100
     * @param bool $isString
101
     */
102
    protected function setMethodReturn(string $value, $isString = false): void
103
    {
104
        $this->setTabs(2);
105
        $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE . (($isString === false) ? $value :
106
                PhpInterface::DOUBLE_QUOTES . $value . PhpInterface::DOUBLE_QUOTES) . PhpInterface::SEMICOLON . PHP_EOL;
107
    }
108
109
    /**
110
     * Ends method declaration
111
     *
112
     * @param int $eolCnt
113
     */
114
    protected function endMethod(int $eolCnt = 2): void
115
    {
116
        $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::CLOSE_BRACE;
117
        for ($i = $eolCnt; $i > 0; --$i) {
118
            $this->sourceCode .= PHP_EOL;
119
        }
120
    }
121
122
    /**
123
     *  Starts an array declaration in string notation
124
     */
125
    protected function startArray(): void
126
    {
127
        $this->setTabs(2);
128
        $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE .
129
            PhpInterface::OPEN_BRACKET . PHP_EOL;
130
    }
131
132
    /**
133
     *  Ends an array declaration after values had been set
134
     */
135
    protected function endArray(): void
136
    {
137
        $this->sourceCode .= PHP_EOL . PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4
138
            . PhpInterface::CLOSE_BRACKET . PhpInterface::SEMICOLON . PHP_EOL;
139
    }
140
141
    /**
142
     * Creates simple key=value map array property
143
     *
144
     * @param $key
145
     * @param $value
146
     */
147
    private function setArrayProperty($key, array $value): void
148
    {
149
        $val = $this->setArrayToString($value);
150
        $this->sourceCode .= $this->quoteParam($key)
151
            . PhpInterface::SPACE . PhpInterface::DOUBLE_ARROW
152
            . PhpInterface::SPACE . $val . PhpInterface::COMMA . PHP_EOL;
153
    }
154
155
    /**
156
     * @param string $prop
157
     * @param string $modifier
158
     * @param string $value
159
     * @param bool $isString
160
     */
161
    protected function createProperty(
162
        string $prop,
163
        string $modifier,
164
        $value = PhpInterface::PHP_TYPES_NULL,
165
        bool $isString = false
166
    ): void
167
    {
168
        if ($value === PhpInterface::PHP_TYPES_NULL) { // drop null assignments as they are already nullable by default
169
            $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN .
170
                $prop . PhpInterface::SEMICOLON . PHP_EOL;
171
        } else {
172
            $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN .
173
                $prop . PhpInterface::SPACE . PhpInterface::EQUALS . PhpInterface::SPACE
174
                . (($isString === false) ? $value : PhpInterface::QUOTES . $value . PhpInterface::QUOTES) . PhpInterface::SEMICOLON . PHP_EOL;
175
        }
176
    }
177
178
    /**
179
     * @param string $prop
180
     * @param string $modifier
181
     * @param array $value
182
     */
183
    protected function createPropertyArray(string $prop, string $modifier, array $value): void
184
    {
185
        $val = $this->setArrayToString($value);
186
        $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN .
187
            $prop . PhpInterface::SPACE . PhpInterface::EQUALS . PhpInterface::SPACE . $val . PhpInterface::SEMICOLON . PHP_EOL;
188
    }
189
190
    /**
191
     * @param array $value
192
     * @return string
193
     */
194
    private function setArrayToString(array $value): string
195
    {
196
        $val = PhpInterface::OPEN_BRACKET;
197
        $val .= PhpInterface::QUOTES . implode(
198
                PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $value
199
            ) . PhpInterface::QUOTES;
200
        $val .= PhpInterface::CLOSE_BRACKET;
201
202
        return $val;
203
    }
204
205
    /**
206
     * @param string $comment
207
     * @param int $tabs
208
     */
209
    protected function setComment(string $comment, int $tabs = 1): void
210
    {
211
        $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::COMMENT
212
            . PhpInterface::SPACE . $comment . PHP_EOL;
213
    }
214
215
    /**
216
     * @param int $tabs
217
     */
218
    protected function openComment(int $tabs = 1): void
219
    {
220
        $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::SLASH
221
            . PhpInterface::ASTERISK . PhpInterface::ASTERISK . PHP_EOL;
222
    }
223
224
    /**
225
     * @param int $tabs
226
     */
227
    protected function closeComment(int $tabs = 1): void
228
    {
229
        $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::ASTERISK
230
            . PhpInterface::SLASH . PHP_EOL;
231
    }
232
233
    /**
234
     * @param string $comment
235
     * @param int $tabs
236
     * @param int $afterTabs
237
     */
238
    protected function setStarredComment(string $comment, int $tabs = 1, int $afterTabs = 0): void
239
    {
240
        $this->setTabs($tabs);
241
        $this->sourceCode .= PhpInterface::ASTERISK
242
            . PhpInterface::SPACE;
243
244
        $this->setTabs($afterTabs);
245
246
        $this->sourceCode .= $comment . PHP_EOL;
247
    }
248
249
    /**
250
     * Sets an amount of tabs to source code
251
     *
252
     * @param int $amount
253
     */
254
    protected function setTabs(int $amount = 1): void
255
    {
256
        for ($i = $amount; $i > 0; --$i) {
257
            $this->sourceCode .= PhpInterface::TAB_PSR4;
258
        }
259
    }
260
261
    /**
262
     * Sets an amount of backslashes to source code
263
     *
264
     * @param int $amount
265
     */
266
    protected function setBackslashes(int $amount = 1): void
267
    {
268
        for ($i = $amount; $i > 0; --$i) {
269
            $this->sourceCode .= PhpInterface::BACKSLASH;
270
        }
271
    }
272
273
    /**
274
     * @param array $attrVal
275
     */
276
    public function setDescription(array $attrVal): void
277
    {
278
        foreach ($attrVal as $k => $v) {
279
            if ($k === ApiInterface::RAML_KEY_DESCRIPTION) {
280
                $this->setTabs(3);
281
                $this->setComment($v);
282
            }
283
        }
284
    }
285
286
    /**
287
     * @param array $params
288
     *
289
     * @return string
290
     */
291
    private function getMethodParams(array $params): string
292
    {
293
        $paramsStr = '';
294
        $cnt = count($params);
295
        foreach ($params as $type => $name) {
296
            --$cnt;
297
            if (is_int($type)) {// not typed
298
                $paramsStr .= PhpInterface::DOLLAR_SIGN . $name;
299
            } else {// typed
300
                $paramsStr .= $type . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . $name;
301
            }
302
            if ($cnt > 0) {
303
                $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE;
304
            }
305
        }
306
307
        return $paramsStr;
308
    }
309
310
    /**
311
     * @param array $params
312
     *
313
     * @param bool $arrayToJson
314
     * @return string
315
     */
316
    private function getMethodParamsToPass(array $params, $arrayToJson = true): string
317
    {
318
        $paramsStr = '';
319
        $cnt = count($params);
320
        foreach ($params as $value) {
321
            --$cnt;
322
            if (is_array($value)) {
323
                $paramsStr .= $arrayToJson ? $this->quoteParam(json_encode($value)) : var_export($value, true);
324
            } else {
325
                $paramsStr .= $this->quoteParam($value);
326
            }
327
            if ($cnt > 0) {
328
                $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE;
329
            }
330
        }
331
332
        return $paramsStr;
333
    }
334
335
    /**
336
     * @param string $str
337
     */
338
    public function setEchoString(string $str): void
339
    {
340
        $this->sourceCode .= PhpInterface::ECHO . PhpInterface::SPACE . PhpInterface::QUOTES
341
            . $str . PhpInterface::QUOTES . PhpInterface::SEMICOLON . PHP_EOL;
342
    }
343
344
    /**
345
     * @param string $attribute
346
     */
347
    public function openRule(string $attribute): void
348
    {
349
        $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 .
350
            PhpInterface::TAB_PSR4
351
            . PhpInterface::QUOTES . $attribute . PhpInterface::QUOTES
352
            . PhpInterface::SPACE
353
            . PhpInterface::DOUBLE_ARROW .
354
            PhpInterface::SPACE . PhpInterface::QUOTES;
355
    }
356
357
    /**
358
     *  Close rules in FormRequest
359
     */
360
    public function closeRule(): void
361
    {
362
        $this->sourceCode .= PhpInterface::QUOTES . PhpInterface::COMMA;
363
    }
364
365
    /**
366
     * @param string $basePath
367
     * @param string $postFix
368
     * @uses \SoliDry\Blocks\Migrations::setContent
369
     * @uses \SoliDry\Blocks\Entities::setContent
370
     * @uses \SoliDry\Blocks\FormRequest::setContent
371
     * @uses \SoliDry\Blocks\Tests::setContent
372
     *
373
     * Creates entities like *Controller, *FormRequest, BaseModel entities etc
374
     *
375
     * @uses \SoliDry\Blocks\Controllers::setContent
376
     * @uses \SoliDry\Blocks\Config::setContent
377
     */
378
    public function createEntity(string $basePath, string $postFix = ''): void
379
    {
380
        $this->setContent();
0 ignored issues
show
The method setContent() does not exist on SoliDry\Blocks\ContentManager. Did you maybe mean setComment()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

380
        $this->/** @scrutinizer ignore-call */ 
381
               setContent();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
381
        $file = $this->getEntityFile($basePath, $postFix);
382
        $isCreated = FileManager::createFile(
383
            $file, $this->sourceCode,
384
            FileManager::isRegenerated($this->generator->options)
385
        );
386
        if ($isCreated) {
387
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
388
        }
389
    }
390
391
    /**
392
     * Gets Laravel <Entity> file
393
     *
394
     * @param string $basePath
395
     * @param string $postFix
396
     * @return string
397
     */
398
    public function getEntityFile(string $basePath, string $postFix = ''): string
399
    {
400
        $file = $basePath . DIRECTORY_SEPARATOR . $this->className;
401
        if ($postFix !== '') {
402
            $file .= $postFix;
403
        }
404
        $file .= PhpInterface::PHP_EXT;
405
406
        return $file;
407
    }
408
409
    /**
410
     * Creates entities like *Controller, *FormRequest, BaseModel entities etc
411
     *
412
     * @param string $basePath
413
     * @param string $postFix
414
     */
415
    public function recreateEntity(string $basePath, string $postFix = ''): void
416
    {
417
        $this->resetContent();
418
        $file = $this->getEntityFile($basePath, $postFix);
419
        $isCreated = FileManager::createFile(
420
            $file, $this->sourceCode,
421
            FileManager::isRegenerated($this->generator->options)
422
        );
423
        if ($isCreated) {
424
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
425
        }
426
    }
427
428
    /**
429
     * Gets array param as string to place in generated methods
430
     *
431
     * @param array $param
432
     *
433
     * @return string
434
     */
435
    private function getArrayParam(array $param): string
436
    {
437
        return PhpInterface::OPEN_BRACKET . PhpInterface::QUOTES .
438
            implode(PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $param)
439
            . PhpInterface::QUOTES
440
            . PhpInterface::CLOSE_BRACKET;
441
    }
442
443
    /**
444
     * @param string $param
445
     *
446
     * @return string
447
     */
448
    public function quoteParam(string $param): string
449
    {
450
        return PhpInterface::QUOTES . $param . PhpInterface::QUOTES;
451
    }
452
453
    /**
454
     * Sets the source starting code
455
     *
456
     * @param string $entityFile
457
     */
458
    protected function setBeforeProps(string $entityFile): void
459
    {
460
        $this->resourceCode = file_get_contents($entityFile);
461
        $end = mb_strpos($this->resourceCode, DefaultInterface::PROPS_START, null, PhpInterface::ENCODING_UTF8) - 3;
462
        $this->sourceCode = mb_substr($this->resourceCode, 0, $end, PhpInterface::ENCODING_UTF8);
463
    }
464
465
    /**
466
     * Sets the source middle code
467
     *
468
     * @param string $till
469
     */
470
    protected function setAfterProps($till = null): void
471
    {
472
        $start = $this->setTabs() . (mb_strpos($this->resourceCode, DefaultInterface::PROPS_END, null,
473
                    PhpInterface::ENCODING_UTF8) - 3);
474
        if ($till === null) {
475
            $this->sourceCode .= mb_substr($this->resourceCode, $start, null, PhpInterface::ENCODING_UTF8);
476
        } else {
477
            $end = mb_strpos($this->resourceCode, $till, null, PhpInterface::ENCODING_UTF8) - 3;
478
            $this->sourceCode .= mb_substr($this->resourceCode, $start, $end - $start, PhpInterface::ENCODING_UTF8);
479
        }
480
    }
481
482
    /**
483
     *  Sets the source tail
484
     */
485
    private function setAfterMethods(): void
486
    {
487
        $start = mb_strpos($this->resourceCode, DefaultInterface::METHOD_END, null, PhpInterface::ENCODING_UTF8) - 3;
488
        $this->sourceCode .= $this->setTabs() . mb_substr($this->resourceCode, $start, null,
489
                PhpInterface::ENCODING_UTF8);
490
    }
491
492
    /**
493
     *
494
     * @param string $object
495
     * @param string $method
496
     * @param array $params
497
     * @param bool $arrayToJson
498
     */
499
    private function methodCallOnObject(string $object, string $method, array $params = [], $arrayToJson = true): void
500
    {
501
        $this->sourceCode .= $this->setTabs(2) . PhpInterface::DOLLAR_SIGN . $object
502
            . PhpInterface::ARROW . $method . PhpInterface::OPEN_PARENTHESES
503
            . $this->getMethodParamsToPass($params, $arrayToJson)
504
            . PhpInterface::CLOSE_PARENTHESES . PhpInterface::SEMICOLON . PHP_EOL;
505
    }
506
507
    /**
508
     * Sets n new lines
509
     *
510
     * @param int $numLines
511
     */
512
    protected function setNewLines(int $numLines = 1): void
513
    {
514
        for ($i = $numLines; $i > 0; --$i) {
515
            $this->sourceCode .= PHP_EOL;
516
        }
517
    }
518
}