Completed
Push — master ( a9a16a...2f2eb3 )
by
unknown
11:48
created

ViewComposerServiceProvider::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Contracts\View\View;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Str;
8
use Yajra\Acl\Models\Permission;
9
use Yajra\CMS\Entities\Category;
10
use Yajra\CMS\Entities\Configuration;
11
use Yajra\CMS\Entities\Menu;
12
use Symfony\Component\Finder\Finder;
13
14
class ViewComposerServiceProvider extends ServiceProvider
15
{
16
    /**
17
     * Media public storage.
18
     *
19
     * @var string
20
     */
21
    public $mediaPath = 'app/public/media';
22
23
    /**
24
     * Bootstrap the application services.
25
     *
26
     * @return void
27
     */
28
    public function boot()
29
    {
30
        $this->bootAdministratorViewComposer();
31
32
        view()->composer('*', function(View $view) {
0 ignored issues
show
Bug introduced by
The method composer does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
           $view->with('active_menu', session('active_menu', new Menu));
34
        });
35
    }
36
37
    /**
38
     * Register administrator view composers.
39
     */
40
    protected function bootAdministratorViewComposer()
41
    {
42
        view()->composer('administrator.widgets.*', function (View $view) {
0 ignored issues
show
Bug introduced by
The method composer does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
            /** @var \Yajra\CMS\Themes\Repositories\Repository $themes */
44
            $themes    = $this->app['themes'];
45
            $theme     = $themes->current();
46
            $positions = $theme->positions;
47
            $data      = [];
48
            foreach ($positions as $position) {
49
                $data[$position] = Str::title($position);
50
            }
51
52
            $view->with('widget_positions', $data);
53
            $view->with('theme', $theme);
54
55
            /** @var \Yajra\CMS\Repositories\Extension\Repository $extensions */
56
            $extensions = $this->app['extensions'];
57
            $widgets    = $extensions->allWidgets()->filter(function ($extension) {
58
                return $extension->enabled;
59
            });
60
            $view->with('extensions', $widgets);
61
        });
62
63
        view()->composer(['administrator.partials.permissions'], function (View $view) {
0 ignored issues
show
Bug introduced by
The method composer does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
64
            $view->with('permissions', Permission::all());
65
        });
66
67
        view()->composer(['system.macro.image-browser'], function (View $view) {
0 ignored issues
show
Bug introduced by
The method composer does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68
            $view->with('mediaDirectories', $this->getFileDirectories());
69
        });
70
    }
71
72
    /**
73
     * Show all directories on media path.
74
     *
75
     * @return array
76
     */
77 View Code Duplication
    protected function getFileDirectories()
0 ignored issues
show
Duplication introduced by
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...
78
    {
79
        $mediaFiles['/'] = '/';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$mediaFiles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mediaFiles = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
80
        $path            = storage_path($this->mediaPath);
81
        foreach ($this->getFiles($path)->directories() as $file) {
82
            $strFile              = str_replace($path, '', $file->getRealPath());
83
            $mediaFiles[$strFile] = $strFile;
84
        }
85
86
        return $mediaFiles;
87
    }
88
89
    /**
90
     * Symfony file finder.
91
     * Show all files in selected $path directory.
92
     * Sort by file type.
93
     *
94
     * @param string $path
95
     * @return \Symfony\Component\Finder\Finder
96
     */
97
    protected function getFiles($path)
98
    {
99
        return Finder::create()->in($path)->sortByType();
100
    }
101
102
    /**
103
     * Register the application services.
104
     *
105
     * @return void
106
     */
107
    public function register()
108
    {
109
        //
110
    }
111
}
112