Issues (65)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Console/ComponentMakeCommand.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Sco\Admin\Console;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Illuminate\Console\GeneratorCommand;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Str;
9
use InvalidArgumentException;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ComponentMakeCommand extends GeneratorCommand
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'make:component';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new component class(Sco-Admin)';
27
28
    /**
29
     * The type of class being generated.
30
     *
31
     * @var string
32
     */
33
    protected $type = 'Component';
34
35
    protected $displayTypes = ['table', 'image', 'tree'];
36
37
    protected $columnTypeMappings = [
38
        'smallint' => 'text',
39
        'integer'  => 'text',
40
        'bigint'   => 'text',
41
        'float'    => 'text',
42
        'string'   => 'text',
43
        'text'     => 'text',
44
        'boolean'  => 'mapping',
45
        'datetime' => 'datetime',
46
        'date'     => 'datetime',
47
    ];
48
49
    protected $elementTypeMappings = [
50
        'smallint' => 'number',
51
        'integer'  => 'number',
52
        'bigint'   => 'number',
53
        'float'    => 'number',
54
        'string'   => 'text',
55
        'text'     => 'textarea',
56
        'boolean'  => 'elswitch',
57
        'datetime' => 'datetime',
58
        'date'     => 'date',
59
        'time'     => 'time',
60
    ];
61
62
    /**
63
     * Get the stub file for the generator.
64
     *
65
     * @return string
66
     */
67
    protected function getStub()
68
    {
69
        return __DIR__ . '/stubs/component.stub';
70
    }
71
72
    /**
73
     * Build the class with the given name.
74
     *
75
     * @param string $name
76
     *
77
     * @return string
78
     */
79
    protected function buildClass($name)
80
    {
81
        $replace = array_merge(
82
            ['DummyDisplayType' => $this->getDisplayType()],
83
            $this->buildObserverReplacements(),
84
            $this->buildModelReplacements()
85
        );
86
87
        return str_replace(
88
            array_keys($replace),
89
            array_values($replace),
90
            parent::buildClass($name)
91
        );
92
    }
93
94
    protected function getDisplayType()
95
    {
96
        $type = $this->option('display');
97
        if (empty($type) || ! in_array($type, $this->displayTypes)) {
98
            $type = $this->choice(
99
                'What is Display type?',
100
                $this->displayTypes,
101
                0
102
            );
103
        }
104
105
        return $type;
106
    }
107
108
    protected function buildObserverReplacements()
109
    {
110 View Code Duplication
        if (! ($observer = $this->option('observer'))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            $observer = $this->anticipate(
112
                'What is the observer class name?',
113
                [
114
                    $class = $this->parseObserver($this->getNameInput())
115
                ],
116
                $class
117
            );
118
        }
119
        $observerClass = $this->parseObserver($observer);
0 ignored issues
show
It seems like $observer defined by $this->option('observer') on line 110 can also be of type array; however, Sco\Admin\Console\Compon...ommand::parseObserver() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
120
121
        if (! class_exists($observerClass)) {
122
            if ($this->confirm(
123
                "A {$observerClass} observer does not exist. Do you want to generate it?",
124
                true
125
            )) {
126
                $this->call('make:observer', [
127
                    'name' => $observerClass,
128
                ]);
129
            }
130
        }
131
132
        return [
133
            'DummyFullObserverClass' => $observerClass,
134
            'DummyObserverClass'     => class_basename($observerClass),
135
        ];
136
    }
137
138
    /**
139
     * Get the fully-qualified observer class name.
140
     *
141
     * @param  string $observer
142
     *
143
     * @return string
144
     */
145
    protected function parseObserver($observer)
146
    {
147
        if (preg_match('([^A-Za-z0-9_/\\\\])', $observer)) {
148
            throw new InvalidArgumentException('Observer name contains invalid characters.');
149
        }
150
151
        $observer = str_replace('/', '\\', $observer);
152
153
        if (! Str::startsWith($observer, [
154
            $rootNamespace = $this->laravel->getNamespace(),
155
            '\\'
156
        ])) {
157
            $observer = $rootNamespace . $this->getComponentNamespace()
158
                . '\Observers\\'
159
                . rtrim($observer, 'Observer') . 'Observer';
160
        }
161
162
        return $observer;
163
    }
164
165
    protected function buildModelReplacements()
166
    {
167 View Code Duplication
        if (! ($model = $this->option('model'))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
            $model = $this->anticipate(
169
                'What is the model class name?',
170
                [
171
                    $class = $this->parseModel($this->getNameInput())
172
                ],
173
                $class
174
            );
175
        }
176
177
        $modelClass = $this->parseModel($model);
0 ignored issues
show
It seems like $model defined by $this->option('model') on line 167 can also be of type array; however, Sco\Admin\Console\Compon...keCommand::parseModel() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
178
179
        if (! class_exists($modelClass)) {
180
            if ($this->confirm(
181
                "A {$modelClass} model does not exist. Do you want to generate it?",
182
                true
183
            )) {
184
                $this->call('make:model', ['name' => $modelClass]);
185
            }
186
        }
187
188
        $columns = $this->getViewColumns($modelClass);
189
        $elements = $this->getFormElements($modelClass);
190
191
        return [
192
            'DummyFullModelClass' => $modelClass,
193
            'DummyColumns'        => $columns ? implode("\n", $columns) : '',
194
            'DummyElements'       => $elements ? implode("\n", $elements) : '',
195
        ];
196
    }
197
198
    /**
199
     * Get the fully-qualified model class name.
200
     *
201
     * @param  string $model
202
     *
203
     * @return string
204
     */
205
    protected function parseModel($model)
206
    {
207
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
208
            throw new InvalidArgumentException('Model name contains invalid characters.');
209
        }
210
211
        $model = str_replace('/', '\\', $model);
212
213
        if (! Str::startsWith($model, [
214
            $rootNamespace = $this->laravel->getNamespace(),
215
            '\\'
216
        ])) {
217
            $model = $rootNamespace . $model;
218
        }
219
220
        return $model;
221
    }
222
223 View Code Duplication
    protected function getViewColumns($model)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
    {
225
        $columns = $this->getTableColumns($model);
226
        if (! $columns) {
227
            return;
228
        }
229
230
        $list = [];
231
        foreach ($columns as $column) {
232
            $list[] = $this->buildViewColumn($column);
233
        }
234
235
        return $list;
236
    }
237
238
    protected function buildViewColumn(Column $column)
239
    {
240
        return sprintf(
241
            "            AdminColumn::%s('%s', '%s'),",
242
            $this->getViewColumnType($column->getType()->getName()),
243
            $column->getName(),
244
            $this->getColumnTitle($column)
245
        );
246
    }
247
248
    protected function getColumnTitle(Column $column)
249
    {
250
        return $column->getComment() ?? studly_case($column->getName());
251
    }
252
253
    protected function getViewColumnType($name)
254
    {
255
        return $this->columnTypeMappings[$name] ?? 'text';
256
    }
257
258 View Code Duplication
    protected function getFormElements($model)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
    {
260
        $columns = $this->getTableColumns($model);
261
262
        if (! $columns) {
263
            return;
264
        }
265
266
        $list = [];
267
        foreach ($columns as $column) {
268
            if (! $column->getAutoincrement()) {
269
                $list[] = $this->buildFormElement($column);
270
            }
271
        }
272
273
        return $list;
274
    }
275
276
    protected function buildFormElement(Column $column)
277
    {
278
        return sprintf(
279
            "            AdminElement::%s('%s', '%s')->required(),",
280
            $this->getFormElementType($column->getType()->getName()),
281
            $column->getName(),
282
            $this->getColumnTitle($column)
283
        );
284
    }
285
286
    protected function getFormElementType($name)
287
    {
288
        return $this->elementTypeMappings[$name] ?? 'text';
289
    }
290
291
    protected function getTableColumns($class)
292
    {
293
        if (empty($class)) {
294
            return;
295
        }
296
297
        if (! class_exists($class)) {
298
            return;
299
        }
300
301
        $model = new $class();
302
        if (! ($model instanceof Model)) {
303
            return;
304
        }
305
        $schema = $model->getConnection()->getDoctrineSchemaManager();
306
307
        $table = $model->getConnection()->getTablePrefix() . $model->getTable();
308
309
        return $schema->listTableColumns($table);
310
    }
311
312
    /**
313
     * Get the default namespace for the class.
314
     *
315
     * @param  string $rootNamespace
316
     *
317
     * @return string
318
     */
319
    protected function getDefaultNamespace($rootNamespace)
320
    {
321
        return $rootNamespace . '\\' . $this->getComponentNamespace();
322
    }
323
324
    /**
325
     *
326
     * @return string
327
     */
328
    protected function getComponentNamespace()
329
    {
330
        return str_replace(
331
            '/',
332
            '\\',
333
            Str::after(
334
                config('admin.components'),
335
                app_path() . DIRECTORY_SEPARATOR
336
            )
337
        );
338
    }
339
340
    /**
341
     * Get the console command options.
342
     *
343
     * @return array
344
     */
345
    protected function getOptions()
346
    {
347
        return [
348
            [
349
                'observer',
350
                'o',
351
                InputOption::VALUE_OPTIONAL,
352
                'The access observer that should be assigned, and generate it if not exists.',
353
            ],
354
            [
355
                'model',
356
                'm',
357
                InputOption::VALUE_OPTIONAL,
358
                'The model that should be assigned, and generate it if not exists.',
359
            ],
360
            [
361
                'display',
362
                'd',
363
                InputOption::VALUE_OPTIONAL,
364
                'Choose a type of data display.',
365
                $this->displayTypes[0]
366
            ]
367
        ];
368
    }
369
}
370