Builder::build()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 30
rs 9.1111
1
<?php declare(strict_types=1);
2
/**
3
 * @author  Vitaliy IIIFX Khomenko (c) 2021
4
 * @license MIT
5
 *
6
 * @link    https://github.com/iiifx-production/yii2-autocomplete-helper
7
 */
8
9
namespace iiifx\Yii2\Autocomplete;
10
11
use yii\base\BaseObject;
12
13
class Builder extends BaseObject
14
{
15
    public ?string $template = null;
16
    public array $components = [];
17
    public ?string $webAppClass = null;
18
    public ?string $consoleAppClass = null;
19
20
    public function build(string|false $file = null): bool|string
21
    {
22
        $prepared = preg_replace_callback('/%.*%/U', function ($m) {
23
            if ($m[0] === '%phpdoc%') {
24
                $string = '/**';
25
26
                foreach ($this->components as $name => $classes) {
27
                    $string .= PHP_EOL . ' * @property ' . implode('|', $classes) . ' $' . $name;
28
                }
29
                $string .= PHP_EOL . ' */';
30
31
                return $string;
32
            }
33
34
            if ($m[0] === '%webapp%') {
35
                return $this->webAppClass ?? '\yii\web\Application';
36
            }
37
38
            if ($m[0] === '%consoleapp%') {
39
                return $this->consoleAppClass ?? '\yii\console\Application';
40
            }
41
42
            return $m[0];
43
        }, $this->template);
44
45
        if ($file === null) {
46
            return $prepared;
47
        }
48
49
        return (bool)file_put_contents($file, $prepared);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $filename of file_put_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
        return (bool)file_put_contents(/** @scrutinizer ignore-type */ $file, $prepared);
Loading history...
50
    }
51
}
52