Completed
Pull Request — 2.0 (#31)
by Nicolas
18:46 queued 15:41
created

MenuItemController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 14.58 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 12
c 9
b 0
f 0
lcom 2
cbo 6
dl 14
loc 96
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 8 1
A store() 7 7 1
A edit() 0 8 1
A update() 7 7 1
A destroy() 0 7 1
A getMenuSelect() 0 4 1
B addMenuId() 0 16 5

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
2
3
namespace Modules\Menu\Http\Controllers\Admin;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
7
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
8
use Modules\Menu\Entities\Menu;
9
use Modules\Menu\Entities\Menuitem;
10
use Modules\Menu\Http\Requests\CreateMenuItemRequest;
11
use Modules\Menu\Http\Requests\UpdateMenuItemRequest;
12
use Modules\Menu\Repositories\MenuItemRepository;
13
use Modules\Menu\Services\MenuItemUriGenerator;
14
use Modules\Page\Repositories\PageRepository;
15
16
class MenuItemController extends AdminBaseController
17
{
18
    /**
19
     * @var MenuItemRepository
20
     */
21
    private $menuItem;
22
    /**
23
     * @var PageRepository
24
     */
25
    private $page;
26
    /**
27
     * @var MenuItemUriGenerator
28
     */
29
    private $menuItemUriGenerator;
30
31
    public function __construct(MenuItemRepository $menuItem, PageRepository $page, MenuItemUriGenerator $menuItemUriGenerator)
32
    {
33
        parent::__construct();
34
        $this->menuItem = $menuItem;
35
        $this->page = $page;
36
        $this->menuItemUriGenerator = $menuItemUriGenerator;
37
    }
38
39
    public function create(Menu $menu)
40
    {
41
        $pages = $this->page->all();
42
43
        $menuSelect = $this->getMenuSelect($menu);
44
45
        return view('menu::admin.menuitems.create', compact('menu', 'pages', 'menuSelect'));
46
    }
47
48 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...
49
    {
50
        $this->menuItem->create($this->addMenuId($menu, $request));
51
52
        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...
53
            ->withSuccess(trans('menu::messages.menuitem created'));
54
    }
55
56
    public function edit(Menu $menu, Menuitem $menuItem)
57
    {
58
        $pages = $this->page->all();
59
60
        $menuSelect = $this->getMenuSelect($menu);
61
62
        return view('menu::admin.menuitems.edit', compact('menu', 'menuItem', 'pages', 'menuSelect'));
63
    }
64
65 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...
66
    {
67
        $this->menuItem->update($menuItem, $this->addMenuId($menu, $request));
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
            ->withSuccess(trans('menu::messages.menuitem updated'));
71
    }
72
73
    public function destroy(Menu $menu, Menuitem $menuItem)
74
    {
75
        $this->menuItem->destroy($menuItem);
76
77
        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...
78
            ->withSuccess(trans('menu::messages.menuitem deleted'));
79
    }
80
81
    /**
82
     * @param Menu, $menuItemId
0 ignored issues
show
Documentation introduced by
The doc-type Menu, could not be parsed: Expected "|" or "end of type", but got "," at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $menuItemId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
83
     * @return array
84
     */
85
    private function getMenuSelect($menu)
86
    {
87
        return $menu->menuitems()->where('is_root', '!=', true)->get()->nest()->listsFlattened('title');
88
    }
89
90
    /**
91
     * @param  Menu $menu
92
     * @param  \Illuminate\Foundation\Http\FormRequest $request
93
     * @return array
94
     */
95
    private function addMenuId(Menu $menu, FormRequest $request)
96
    {
97
        $data = $request->all();
98
99
        foreach (LaravelLocalization::getSupportedLanguagesKeys() as $lang) {
100
            if ($data['link_type'] === 'page' && ! empty($data['page_id'])) {
101
                $data[$lang]['uri'] = $this->menuItemUriGenerator->generateUri($data['page_id'], $data['parent_id'], $lang);
102
            }
103
        }
104
105
        if (empty($data['parent_id'])) {
106
            $data['parent_id'] = $this->menuItem->getRootForMenu($menu->id)->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...
107
        }
108
109
        return array_merge($data, ['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...
110
    }
111
}
112