Passed
Push — master ( 5263e7...c33415 )
by Arthur
05:45
created

ContentManager::openRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Controllers\BaseCommand;
6
use SoliDry\Helpers\Console;
7
use SoliDry\Helpers\Json;
8
use SoliDry\Helpers\MethodOptions;
9
use SoliDry\Types\DefaultInterface;
10
use SoliDry\Types\PhpInterface;
11
use SoliDry\Types\ApiInterface;
12
13
/**
14
 * Class ContentManager
15
 *
16
 * @package SoliDry\Blocks
17
 * @property BaseCommand generator
18
 * @property string sourceCode
19
 */
20
trait ContentManager
21
{
22
23
    /**
24
     *  Sets <?php open tag for source code
25
     */
26
    protected function setTag(): void
27
    {
28
        $this->sourceCode = PhpInterface::PHP_OPEN_TAG . PHP_EOL;
29
    }
30
31
    /**
32
     * @param string $postfix
33
     */
34
    protected function setNamespace(string $postfix): void
35
    {
36
        $this->sourceCode .= PhpInterface::PHP_NAMESPACE . PhpInterface::SPACE .
37
            $this->generator->modulesDir . PhpInterface::BACKSLASH .
38
            strtoupper($this->generator->version) .
39
            PhpInterface::BACKSLASH . $postfix . PhpInterface::SEMICOLON . PHP_EOL . PHP_EOL;
40
    }
41
42
    /**
43
     * @param string $path
44
     * @param bool $isTrait
45
     * @param bool $isLast
46
     */
47
    protected function setUse(string $path, bool $isTrait = false, bool $isLast = false): void
48
    {
49
        $this->sourceCode .= (($isTrait === false) ? '' : PhpInterface::TAB_PSR4) .
50
            PhpInterface::PHP_USE . PhpInterface::SPACE . $path . PhpInterface::SEMICOLON .
51
            PHP_EOL . (($isLast === false) ? '' : PHP_EOL);
52
    }
53
54
    /**
55
     * @param string $name
56
     * @param null $extends
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $extends is correct as it would always require null to be passed?
Loading history...
57
     */
58
    protected function startClass(string $name, $extends = NULL): void
59
    {
60
        $this->sourceCode .= PhpInterface::PHP_CLASS . PhpInterface::SPACE . $name
61
            . PhpInterface::SPACE;
62
        if ($extends !== NULL) {
0 ignored issues
show
introduced by
The condition $extends !== NULL is always false.
Loading history...
63
            $this->sourceCode .=
64
                PhpInterface::PHP_EXTENDS
65
                . PhpInterface::SPACE . $extends . PhpInterface::SPACE;
66
        }
67
        $this->sourceCode .= PHP_EOL . PhpInterface::OPEN_BRACE . PHP_EOL;
68
    }
69
70
    /**
71
     *  Ends class declaration
72
     */
73
    protected function endClass(): void
74
    {
75
        $this->sourceCode .= PhpInterface::CLOSE_BRACE . PHP_EOL;
76
    }
77
78
    /**
79
     * @param MethodOptions $methodOptions
80
     */
81
    protected function startMethod(MethodOptions $methodOptions): void
82
    {
83
        // get params
84
        $params = $this->getMethodParams($methodOptions->getParams());
85
        $this->sourceCode .= PhpInterface::TAB_PSR4 . $methodOptions->getModifier() . PhpInterface::SPACE .
86
            (($methodOptions->isStatic() !== false) ? PhpInterface::PHP_STATIC . PhpInterface::SPACE :
87
                '') .
88
            PhpInterface::PHP_FUNCTION . PhpInterface::SPACE .
89
            $methodOptions->getName()
90
            . PhpInterface::OPEN_PARENTHESES . $params . PhpInterface::CLOSE_PARENTHESES .
91
            ((empty($methodOptions->getReturnType())) ? '' :
92
                PhpInterface::COLON . PhpInterface::SPACE . $methodOptions->getReturnType()) .
93
            PhpInterface::SPACE . PHP_EOL . PhpInterface::TAB_PSR4
94
            . PhpInterface::OPEN_BRACE . PHP_EOL;
95
    }
96
97
    /**
98
     * Sets return stmt for any generated method
99
     *
100
     * @param string $value
101
     * @param bool $isString
102
     */
103
    protected function setMethodReturn(string $value, $isString = false): void
104
    {
105
        $this->setTabs(2);
106
        $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE . (($isString === false) ? $value :
107
                PhpInterface::DOUBLE_QUOTES . $value . PhpInterface::DOUBLE_QUOTES) . PhpInterface::SEMICOLON . PHP_EOL;
108
    }
109
110
    /**
111
     * Ends method declaration
112
     *
113
     * @param int $eolCnt
114
     */
115
    protected function endMethod(int $eolCnt = 2): void
116
    {
117
        $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::CLOSE_BRACE;
118
        for ($i = $eolCnt; $i > 0; --$i) {
119
            $this->sourceCode .= PHP_EOL;
120
        }
121
    }
122
123
    /**
124
     *  Starts an array declaration in string notation
125
     */
126
    protected function startArray(): void
127
    {
128
        $this->setTabs(2);
129
        $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE .
130
            PhpInterface::OPEN_BRACKET . PHP_EOL;
131
    }
132
133
    /**
134
     *  Ends an array declaration after values had been set
135
     */
136
    protected function endArray(): void
137
    {
138
        $this->sourceCode .= PHP_EOL . PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4
139
            . PhpInterface::CLOSE_BRACKET . PhpInterface::SEMICOLON . PHP_EOL;
140
    }
141
142
    /**
143
     * Creates simple key=value map array property
144
     *
145
     * @param $key
146
     * @param $value
147
     */
148
    private function setArrayProperty($key, array $value): void
149
    {
150
        $val = $this->setArrayToString($value);
151
        $this->sourceCode .= $this->quoteParam($key)
152
            . PhpInterface::SPACE . PhpInterface::DOUBLE_ARROW
153
            . PhpInterface::SPACE . $val . PhpInterface::COMMA . PHP_EOL;
154
    }
155
156
    /**
157
     * @param string $prop
158
     * @param string $modifier
159
     * @param string $value
160
     * @param bool $isString
161
     */
162
    protected function createProperty(
163
        string $prop,
164
        string $modifier,
165
        $value = PhpInterface::PHP_TYPES_NULL,
166
        bool $isString = false
167
    ): void {
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
0 ignored issues
show
Bug introduced by
Are you sure $this->setTabs($tabs) of type void can be used in concatenation? ( Ignorable by Annotation )

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

211
        $this->sourceCode .= /** @scrutinizer ignore-type */ $this->setTabs($tabs) . PhpInterface::COMMENT
Loading history...
Bug introduced by
Are you sure the usage of $this->setTabs($tabs) targeting SoliDry\Blocks\ContentManager::setTabs() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
212
            . PhpInterface::SPACE . $comment . PHP_EOL;
213
    }
214
215
    /**
216
     * Sets an amount of tabs to source code
217
     * @param int $amount
218
     */
219
    protected function setTabs(int $amount = 1): void
220
    {
221
        for ($i = $amount; $i > 0; --$i) {
222
            $this->sourceCode .= PhpInterface::TAB_PSR4;
223
        }
224
    }
225
226
    /**
227
     * Sets an amount of backslashes to source code
228
     * @param int $amount
229
     */
230
    protected function setBackslashes(int $amount = 1): void
231
    {
232
        for ($i = $amount; $i > 0; --$i) {
233
            $this->sourceCode .= PhpInterface::BACKSLASH;
234
        }
235
    }
236
237
    /**
238
     * @param array $attrVal
239
     */
240
    public function setDescription(array $attrVal): void
241
    {
242
        foreach ($attrVal as $k => $v) {
243
            if ($k === ApiInterface::RAML_KEY_DESCRIPTION) {
244
                $this->setTabs(3);
245
                $this->setComment($v);
246
            }
247
        }
248
    }
249
250
    /**
251
     * @param array $params
252
     *
253
     * @return string
254
     */
255
    private function getMethodParams(array $params): string
256
    {
257
        $paramsStr = '';
258
        $cnt = count($params);
259
        foreach ($params as $type => $name) {
260
            --$cnt;
261
            if (is_int($type)) {// not typed
262
                $paramsStr .= PhpInterface::DOLLAR_SIGN . $name;
263
            } else {// typed
264
                $paramsStr .= $type . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . $name;
265
            }
266
            if ($cnt > 0) {
267
                $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE;
268
            }
269
        }
270
271
        return $paramsStr;
272
    }
273
274
    /**
275
     * @param array $params
276
     *
277
     * @param bool $arrayToJson
278
     * @return string
279
     */
280
    private function getMethodParamsToPass(array $params, $arrayToJson = true): string
281
    {
282
        $paramsStr = '';
283
        $cnt = count($params);
284
        foreach ($params as $value) {
285
            --$cnt;
286
            if (is_array($value)) {
287
                $paramsStr .= $arrayToJson ? $this->quoteParam(json_encode($value)) : var_export($value, true);
288
            } else {
289
                $paramsStr .= $this->quoteParam($value);
290
            }
291
            if ($cnt > 0) {
292
                $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE;
293
            }
294
        }
295
296
        return $paramsStr;
297
    }
298
299
    /**
300
     * @param string $str
301
     */
302
    public function setEchoString(string $str): void
303
    {
304
        $this->sourceCode .= PhpInterface::ECHO . PhpInterface::SPACE . PhpInterface::QUOTES
305
            . $str . PhpInterface::QUOTES . PhpInterface::SEMICOLON . PHP_EOL;
306
    }
307
308
    /**
309
     * @param string $attribute
310
     */
311
    public function openRule(string $attribute): void
312
    {
313
        $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 .
314
            PhpInterface::TAB_PSR4
315
            . PhpInterface::QUOTES . $attribute . PhpInterface::QUOTES
316
            . PhpInterface::SPACE
317
            . PhpInterface::DOUBLE_ARROW .
318
            PhpInterface::SPACE . PhpInterface::QUOTES;
319
    }
320
321
    /**
322
     *  Close rules in FormRequest
323
     */
324
    public function closeRule(): void
325
    {
326
        $this->sourceCode .= PhpInterface::QUOTES . PhpInterface::COMMA;
327
    }
328
329
    /**
330
     * @uses \SoliDry\Blocks\Controllers::setContent
331
     * @uses \SoliDry\Blocks\Config::setContent
332
     * @uses \SoliDry\Blocks\Migrations::setContent
333
     * @uses \SoliDry\Blocks\Entities::setContent
334
     * @uses \SoliDry\Blocks\FormRequest::setContent
335
     * @uses \SoliDry\Blocks\Tests::setContent
336
     *
337
     * Creates entities like *Controller, *FormRequest, BaseModel entities etc
338
     *
339
     * @param string $basePath
340
     * @param string $postFix
341
     */
342
    public function createEntity(string $basePath, string $postFix = ''): void
343
    {
344
        $this->setContent();
0 ignored issues
show
Bug introduced by
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

344
        $this->/** @scrutinizer ignore-call */ 
345
               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...
345
        $file = $this->getEntityFile($basePath, $postFix);
346
        $isCreated = FileManager::createFile(
347
            $file, $this->sourceCode,
348
            FileManager::isRegenerated($this->generator->options)
349
        );
350
        if ($isCreated) {
351
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
352
        }
353
    }
354
355
    /**
356
     * Gets Laravel <Entity> file
357
     *
358
     * @param string $basePath
359
     * @param string $postFix
360
     * @return string
361
     */
362
    public function getEntityFile(string $basePath, string $postFix = ''): string
363
    {
364
        $file = $basePath . DIRECTORY_SEPARATOR . $this->className;
365
        if ($postFix !== '') {
366
            $file .= $postFix;
367
        }
368
        $file .= PhpInterface::PHP_EXT;
369
370
        return $file;
371
    }
372
373
    /**
374
     * Creates entities like *Controller, *FormRequest, BaseModel entities etc
375
     *
376
     * @param string $basePath
377
     * @param string $postFix
378
     */
379
    public function recreateEntity(string $basePath, string $postFix = ''): void
380
    {
381
        $this->resetContent();
0 ignored issues
show
Bug introduced by
It seems like resetContent() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

381
        $this->/** @scrutinizer ignore-call */ 
382
               resetContent();
Loading history...
382
        $file = $this->getEntityFile($basePath, $postFix);
383
        $isCreated = FileManager::createFile(
384
            $file, $this->sourceCode,
385
            FileManager::isRegenerated($this->generator->options)
386
        );
387
        if ($isCreated) {
388
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
389
        }
390
    }
391
392
    /**
393
     * Gets array param as string to place in generated methods
394
     *
395
     * @param array $param
396
     *
397
     * @return string
398
     */
399
    private function getArrayParam(array $param): string
400
    {
401
        return PhpInterface::OPEN_BRACKET . PhpInterface::QUOTES .
402
            implode(PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $param)
403
            . PhpInterface::QUOTES
404
            . PhpInterface::CLOSE_BRACKET;
405
    }
406
407
    /**
408
     * @param string $param
409
     *
410
     * @return string
411
     */
412
    public function quoteParam(string $param): string
413
    {
414
        return PhpInterface::QUOTES . $param . PhpInterface::QUOTES;
415
    }
416
417
    /**
418
     * Sets the source starting code
419
     *
420
     * @param string $entityFile
421
     */
422
    private function setBeforeProps(string $entityFile): void
423
    {
424
        $this->resourceCode = file_get_contents($entityFile);
0 ignored issues
show
Bug Best Practice introduced by
The property resourceCode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
425
        $end = mb_strpos($this->resourceCode, DefaultInterface::PROPS_START, NULL, PhpInterface::ENCODING_UTF8) - 3;
426
        $this->sourceCode = mb_substr($this->resourceCode, 0, $end, PhpInterface::ENCODING_UTF8);
427
    }
428
429
    /**
430
     * Sets the source middle code
431
     *
432
     * @param string $till
433
     */
434
    private function setAfterProps($till = NULL): void
435
    {
436
        $start = $this->setTabs() . mb_strpos($this->resourceCode, DefaultInterface::PROPS_END, NULL,
0 ignored issues
show
Bug introduced by
Are you sure $this->setTabs() of type void can be used in concatenation? ( Ignorable by Annotation )

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

436
        $start = /** @scrutinizer ignore-type */ $this->setTabs() . mb_strpos($this->resourceCode, DefaultInterface::PROPS_END, NULL,
Loading history...
Bug introduced by
Are you sure the usage of $this->setTabs() targeting SoliDry\Blocks\ContentManager::setTabs() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
437
                PhpInterface::ENCODING_UTF8) - 3;
438
        if ($till === NULL) {
439
            $this->sourceCode .= mb_substr($this->resourceCode, $start, NULL, PhpInterface::ENCODING_UTF8);
440
        } else {
441
            $end = mb_strpos($this->resourceCode, $till, NULL, PhpInterface::ENCODING_UTF8) - 3;
442
            $this->sourceCode .= mb_substr($this->resourceCode, $start, $end - $start, PhpInterface::ENCODING_UTF8);
443
        }
444
    }
445
446
    /**
447
     *  Sets the source tail
448
     */
449
    private function setAfterMethods(): void
450
    {
451
        $start = mb_strpos($this->resourceCode, DefaultInterface::METHOD_END, NULL, PhpInterface::ENCODING_UTF8) - 3;
452
        $this->sourceCode .= $this->setTabs() . mb_substr($this->resourceCode, $start, NULL,
0 ignored issues
show
Bug introduced by
Are you sure $this->setTabs() of type void can be used in concatenation? ( Ignorable by Annotation )

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

452
        $this->sourceCode .= /** @scrutinizer ignore-type */ $this->setTabs() . mb_substr($this->resourceCode, $start, NULL,
Loading history...
Bug introduced by
Are you sure the usage of $this->setTabs() targeting SoliDry\Blocks\ContentManager::setTabs() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
453
                PhpInterface::ENCODING_UTF8);
454
    }
455
456
    /**
457
     *
458
     * @param string $object
459
     * @param string $method
460
     * @param array $params
461
     * @param bool $arrayToJson
462
     */
463
    private function methodCallOnObject(string $object, string $method, array $params = [], $arrayToJson = true): void
464
    {
465
        $this->sourceCode .= $this->setTabs(2) . PhpInterface::DOLLAR_SIGN . $object
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setTabs(2) targeting SoliDry\Blocks\ContentManager::setTabs() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure $this->setTabs(2) of type void can be used in concatenation? ( Ignorable by Annotation )

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

465
        $this->sourceCode .= /** @scrutinizer ignore-type */ $this->setTabs(2) . PhpInterface::DOLLAR_SIGN . $object
Loading history...
466
            . PhpInterface::ARROW . $method . PhpInterface::OPEN_PARENTHESES
467
            . $this->getMethodParamsToPass($params, $arrayToJson)
468
            . PhpInterface::CLOSE_PARENTHESES . PhpInterface::SEMICOLON . PHP_EOL;
469
    }
470
}