MakeViewCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ngtfkx\Laradeck\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
8
class MakeViewCommand extends Command
9
{
10
    protected $signature = 'laradeck:view 
11
                            {name : View name with dot syntax} 
12
                            {--F|force : Rewrite exists view} 
13
                            {--extends= : Extends directive} 
14
                            {--section=* : Section directive} 
15
                            {--stack=* : Push directive} 
16
                            {--component=* : Component directive}
17
                            ';
18
19
    protected $description = 'Create a new view';
20
21
    /**
22
     * @var string
23
     */
24
    protected $view;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $force;
30
31
    /**
32
     * @var string
33
     */
34
    protected $extends;
35
36
    /**
37
     * @var array
38
     */
39
    protected $sections;
40
41
    /**
42
     * @var array
43
     */
44
    protected $stacks;
45
46
    /**
47
     * @var array
48
     */
49
    protected $components;
50
51
    public function __construct()
52
    {
53
        parent::__construct();
54
    }
55
56
    public function handle()
57
    {
58
        $this->view = $this->argument('name');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->argument('name') can also be of type array. However, the property $view is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
60
        $this->force = $this->option('force');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->option('force') of type string or array is incompatible with the declared type boolean of property $force.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
62
        $this->extends = $this->option('extends');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->option('extends') can also be of type array. However, the property $extends is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
64
        $this->sections = $this->parse($this->option('section'));
65
66
        $this->stacks = $this->parse($this->option('stack'));
67
68
        $this->components = $this->parse($this->option('component'));
69
70
        $path = $this->path();
71
72
        $content = $this->content();
73
74
        $this->create($path, $content);
75
    }
76
77
    /**
78
     * Get path of view file and create directories for it
79
     *
80
     * @return string
81
     */
82
    protected function path(): string
83
    {
84
        $path = resource_path('views' . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $this->view) . '.blade.php');
85
86
        $directory = dirname($path);
87
88
        if (!is_dir($directory)) {
89
            \File::makeDirectory($directory, 0755, true);
90
        }
91
92
        return $path;
93
    }
94
95
    /**
96
     * Generate content of a view
97
     *
98
     * @return string
99
     */
100
    protected function content(): string
101
    {
102
        $content = '';
103
104
        if ($this->extends) {
105
            $content .= "@extends('" . $this->extends . "')";
106
        }
107
108
        $types = [
109
            'section' => $this->sections,
110
            'component' => $this->components,
111
            'push' => $this->stacks,
112
        ];
113
114
        foreach ($types as $key => $values) {
115
            $content .= $this->blocks($key, $values);
116
        }
117
118
        return $content;
119
    }
120
121
    /**
122
     * Write content to a view
123
     *
124
     * @param string $path
125
     * @param string $content
126
     */
127
    protected function create(string $path, string $content)
128
    {
129
        if ($this->force || !file_exists($path)) {
130
            \File::put($path, $content);
131
            $this->info('View created successfully.');
132
        } else {
133
            $this->error('View already exists!');
134
        }
135
    }
136
137
    /**
138
     * @param $value
139
     * @return array
140
     */
141
    protected function parse($value): array
142
    {
143
        $values = [];
144
145
        foreach ($value as $item) {
146
            if (Str::contains($item, ',')) {
147
                $values = array_merge($values, explode(',', $item));
148
            } else {
149
                $values[] = $item;
150
            }
151
        }
152
153
        return $values;
154
    }
155
156
    /**
157
     * @param $name
158
     * @param $type
159
     * @return string
160
     */
161
    protected function block($name, $type): string
162
    {
163
        $content = PHP_EOL . PHP_EOL . "@" . $type . "('" . $name . "')" . PHP_EOL;
164
        $content .= "@end" . $type;
165
166
        return $content;
167
    }
168
169
    /**
170
     * @param string $key
171
     * @param array $values
172
     * @return string
173
     */
174
    protected function blocks(string $key, array $values): string
175
    {
176
        $content = '';
177
178
        foreach ($values as $item) {
179
            $content .= $this->block($item, $key);
180
        }
181
182
        return $content;
183
    }
184
}
185