MenuItemController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 23.19 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 2
cbo 4
dl 16
loc 69
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 6 1
A store() 8 8 1
A edit() 0 6 1
A addMenuId() 0 4 1
A update() 8 8 1
A destroy() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Modules\Menu\Http\Controllers\Admin;
2
3
use Illuminate\Foundation\Http\FormRequest;
4
use Laracasts\Flash\Flash;
5
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
6
use Modules\Menu\Entities\Menu;
7
use Modules\Menu\Entities\Menuitem;
8
use Modules\Menu\Http\Requests\CreateMenuItemRequest;
9
use Modules\Menu\Http\Requests\UpdateMenuItemRequest;
10
use Modules\Menu\Repositories\MenuItemRepository;
11
use Modules\Page\Repositories\PageRepository;
12
13
class MenuItemController extends AdminBaseController
14
{
15
    /**
16
     * @var MenuItemRepository
17
     */
18
    private $menuItem;
19
    /**
20
     * @var PageRepository
21
     */
22
    private $page;
23
24
    public function __construct(MenuItemRepository $menuItem, PageRepository $page)
25
    {
26
        parent::__construct();
27
        $this->menuItem = $menuItem;
28
        $this->page = $page;
29
    }
30
31
    public function create(Menu $menu)
32
    {
33
        $pages = $this->page->all();
34
35
        return view('menu::admin.menuitems.create', compact('menu', 'pages'));
36
    }
37
38 View Code Duplication
    public function store(Menu $menu, CreateMenuItemRequest $request)
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...
39
    {
40
        $this->menuItem->create($this->addMenuId($menu, $request));
41
42
        flash(trans('menu::messages.menuitem created'));
43
44
        return redirect()->route('admin.menu.menu.edit', [$menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
    }
46
47
    public function edit(Menu $menu, Menuitem $menuItem)
48
    {
49
        $pages = $this->page->all();
50
51
        return view('menu::admin.menuitems.edit', compact('menu', 'menuItem', 'pages'));
52
    }
53
54 View Code Duplication
    public function update(Menu $menu, Menuitem $menuItem, UpdateMenuItemRequest $request)
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...
55
    {
56
        $this->menuItem->update($menuItem, $this->addMenuId($menu, $request));
57
58
        flash(trans('menu::messages.menuitem updated'));
59
60
        return redirect()->route('admin.menu.menu.edit', [$menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
    }
62
63
    public function destroy(Menu $menu, Menuitem $menuItem)
64
    {
65
        $this->menuItem->destroy($menuItem);
66
67
        flash(trans('menu::messages.menuitem deleted'));
68
69
        return redirect()->route('admin.menu.menu.edit', [$menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
    }
71
72
    /**
73
     * @param  Menu                                    $menu
74
     * @param  \Illuminate\Foundation\Http\FormRequest $request
75
     * @return array
76
     */
77
    private function addMenuId(Menu $menu, FormRequest $request)
78
    {
79
        return array_merge($request->all(), ['menu_id' => $menu->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Menu\Entities\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
    }
81
}
82