Completed
Push — master ( 794b40...f53c10 )
by Andrey
01:20
created

RoleController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 45
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 8 8 1
A getModelName() 4 4 1
A getSearchModelName() 4 4 1
A getNewSearchModel() 4 4 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
2
3
namespace Itstructure\RbacModule\controllers;
4
5
use Itstructure\RbacModule\models\{Role, RoleSearch};
6
7
/**
8
 * Class RoleController
9
 * RoleController implements the CRUD actions for Role model.
10
 *
11
 * @package Itstructure\RbacModule\controllers
12
 */
13 View Code Duplication
class RoleController extends BaseController
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * Initialize.
17
     * Set viewPath and validateComponent.
18
     */
19
    public function init()
20
    {
21
        $this->viewPath = '@rbac/views/roles';
22
23
        $this->validateComponent = $this->module->get('rbac-validate-component');
0 ignored issues
show
Documentation introduced by
The property $validateComponent is declared private in Itstructure\RbacModule\controllers\BaseController. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
24
25
        parent::init();
26
    }
27
28
    /**
29
     * Returns Role model name.
30
     *
31
     * @return string
32
     */
33
    protected function getModelName(): string
34
    {
35
        return Role::class;
36
    }
37
38
    /**
39
     * Returns RoleSearch model name.
40
     *
41
     * @return string|null
42
     */
43
    protected function getSearchModelName(): string
44
    {
45
        return RoleSearch::class;
46
    }
47
48
    /**
49
     * Returns new object of search main model.
50
     *
51
     * @return mixed
52
     */
53
    protected function getNewSearchModel()
54
    {
55
        return parent::getNewSearchModel()->setAuthManager($this->validateComponent->getAuthManager());
0 ignored issues
show
Documentation introduced by
The property $validateComponent is declared private in Itstructure\RbacModule\controllers\BaseController. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
56
    }
57
}
58