GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Menu   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 161
Duplicated Lines 2.48 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.71%

Importance

Changes 0
Metric Value
dl 4
loc 161
ccs 67
cts 70
cp 0.9571
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultScope() 0 6 1
A relations() 0 6 1
A getMenu() 0 18 2
A build() 0 16 3
A setDepth() 0 7 2
A getMenuUrl() 4 10 4
A getChildren() 0 4 1
A getName() 0 4 1
A buildItem() 0 13 1
A loadModels() 0 19 3
A setModels() 0 15 6

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
 * @author Nikita Melnikov <[email protected]>, Sergey Glagolev <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package frontend.models.menu
8
 *
9
 * @method static Menu model(string $class = __CLASS__)
10
 *
11
 * @property int $id
12
 * @property string $name
13
 * @property string $sysname
14
 * @property string $url
15
 * @property int $visible
16
 *
17
 * @property MenuItem[] $items
18
 */
19
class Menu extends FActiveRecord implements IMenuItem
20
{
21
  protected $depth;
22
23
  /**
24
   * @var FActiveRecord[]|IMenuItem[]
25
   */
26
  protected $models;
27
28 9
  public function defaultScope()
29
  {
30
    return array(
31
      'with' => 'items'
32 9
    );
33
  }
34
35 1
  public function relations()
36
  {
37
    return array(
38 1
      'items' => array(self::HAS_MANY, 'MenuItem', 'menu_id', 'order' => 'items.position, items.id'),
39 1
    );
40
  }
41
42
  /**
43
   * Получение массива элементов меню по системному имени
44
   *
45
   * @param string $sysname
46
   * @param integer $depth
47
   *
48
   * @return array
49
   */
50 9
  public function getMenu($sysname, $depth = null)
51
  {
52 9
    $data = array();
53
54
    /**
55
     * @var Menu $menu
56
     */
57
58 9
    $menu = $this->findByAttributes(array('sysname' => $sysname));
59
60
    if( $menu )
61 9
    {
62 9
      $menu->setDepth($depth);
63 9
      $data = $menu->build();
64 9
    }
65
66 9
    return $data;
67
  }
68
69
  /**
70
   * @return array
71
   */
72 9
  public function build()
73
  {
74 9
    $data = array();
75 9
    $this->loadModels();
76
77 9
    foreach($this->items as $item)
78
    {
79 9
      if( $this->depth === 0 )
80 9
        continue;
81
82 9
      $item->setDepth($this->depth - 1);
83 9
      $data[] = $this->buildItem($item);
84 9
    }
85
86 9
    return $data;
87
  }
88
89
  /**
90
   * @param int $depth
91
   */
92 9
  public function setDepth($depth = null)
93
  {
94 9
    if( isset($depth) )
95 9
    {
96 1
      $this->depth = $depth;
97 1
    }
98 9
  }
99
100
  /**
101
   * @return array
102
   */
103 9
  public function getMenuUrl()
104
  {
105 9
    if( empty($this->url) )
106 9
      return '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type declared by the interface IMenuItem::getMenuUrl of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
108 View Code Duplication
    if( substr($this->url, 0, 1) === '/' && substr($this->url, -1, 1) === '/' )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
      return $this->url;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->url; (string) is incompatible with the return type declared by the interface IMenuItem::getMenuUrl of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
110
    else
111
      return array($this->url);
112
  }
113
114
  /**
115
   * @return array
116
   */
117 9
  public function getChildren()
118
  {
119 9
    return $this->build();
120
  }
121
122
  /**
123
   * @return string
124
   */
125 9
  public function getName()
126
  {
127 9
    return $this->name;
128
  }
129
130 9
  protected function buildItem(MenuItem $item)
131
  {
132 9
    $model = preg_replace("/([A-Z][a-z]+)\w*/", "$1", $item->frontend_model);
133
134
    return array(
135 9
      'label' => $item->getName(),
0 ignored issues
show
Bug introduced by
Consider using $item->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
136 9
      'url'   => $item->getMenuUrl(),
137 9
      'items' => $item->getChildren(),
138
      'itemOptions' => array(
139 9
        'class' => 'icn-menu-'.strtolower($model).$item->item_id
140 9
      ),
141 9
    );
142
  }
143
144 9
  protected function loadModels()
145
  {
146 9
    if( $this->models === null )
147 9
    {
148 9
      $this->models = array();
149 9
      $loadedModels = CHtml::listData($this->items, 'item_id', 'item_id', 'frontend_model');
150
151 9
      foreach($loadedModels as $modelClass => $modelPk)
152
      {
153
        /**
154
         * @var FActiveRecord $model
155
         */
156 9
        $model = $modelClass::model();
157 9
        $this->models = CMap::mergeArray($this->models, $model->findAllByPk($modelPk));
158 9
      }
159
160 9
      $this->setModels();
161 9
    }
162 9
  }
163
164 9
  protected function setModels()
165
  {
166 9
    foreach($this->models as $model)
167
    {
168 9
      foreach($this->items as $menuItem)
169
      {
170 9
        if( $menuItem->item_id === $model->getPrimaryKey() && $menuItem->frontend_model === get_class($model) )
171 9
        {
172 9
          $menuItem->setModel($model);
173 9
          $model->setDepth(isset($this->depth) ? $this->depth - 1 : null);
174 9
          break;
175
        }
176 9
      }
177 9
    }
178
  }
179
}