FormServiceProvider::boot()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 8.5806
1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Support\HtmlString;
6
use Illuminate\Support\ServiceProvider;
7
use Yajra\CMS\Entities\Category;
8
9
class FormServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     *
14
     * @return void
15
     */
16
    public function boot()
17
    {
18
        form()->macro('categories', function ($name = 'category_id', $selected = null, $options = []) {
19
            $html = [];
20
            Category::lists()->each(function (Category $category) use (&$html, $selected, $name) {
21
                $selected = form()->getValueAttribute($name, $selected);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $selected, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
22
23
                if (is_array($selected)) {
24
                    $selected = in_array($category->id, $selected) ? 'selected' : null;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $selected, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
25
                } else {
26
                    $selected = ((string)$category->id == (string)$selected) ? 'selected' : null;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $selected, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
27
                }
28
29
                $options = [
30
                    'value'      => $category->id,
31
                    'selected'   => $selected,
32
                    'data-alias' => $category->present()->alias,
33
                ];
34
35
                $html[] = new HtmlString('<option ' . html()->attributes($options) . '>' . e($category->present()->indentedTitle) . '</option>');
36
            });
37
38
            $list    = implode('', $html);
39
            $options = html()->attributes($options + ['name' => $name]);
40
41
            return new HtmlString("<select{$options}>{$list}</select>");
42
        });
43
    }
44
45
    /**
46
     * Register the application services.
47
     *
48
     * @return void
49
     */
50
    public function register()
51
    {
52
        //
53
    }
54
}
55