Conditions | 9 |
Paths | 2 |
Total Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | public function process() |
||
30 | { |
||
31 | // create menus collection, with a 'main' menu |
||
32 | $main = new Menu('main'); |
||
33 | $this->menus = new MenusCollection('menus'); |
||
34 | $this->menus->add($main); |
||
35 | |||
36 | // add entries from pages to menus collection |
||
37 | $this->collectPages(); |
||
38 | |||
39 | /* |
||
40 | * Removing/adding/replacing menus entries from config |
||
41 | * ie: |
||
42 | * site: |
||
43 | * menu: |
||
44 | * main: |
||
45 | * example: |
||
46 | * name: "Example" |
||
47 | * url: https://example.com |
||
48 | * weight: 999 |
||
49 | * a-propos: |
||
50 | * id: about |
||
51 | * enabled: false |
||
52 | */ |
||
53 | if ($this->builder->getConfig()->get('site.menu')) { |
||
54 | call_user_func_array($this->builder->getMessageCb(), ['MENU', 'Creating menus (config)']); |
||
55 | $menus = $this->builder->getConfig()->get('site.menu'); |
||
56 | $totalConfig = array_sum(array_map('count', $menus)); |
||
57 | $countConfig = 0; |
||
58 | foreach ($menus as $menu => $entry) { |
||
59 | /* @var $menu \Cecil\Collection\Menu\Menu */ |
||
60 | if (!$this->menus->has($menu)) { |
||
61 | $this->menus->add(new Menu($menu)); |
||
62 | } |
||
63 | $menu = $this->menus->get($menu); |
||
64 | foreach ($entry as $key => $property) { |
||
65 | $updated = ''; |
||
|
|||
66 | $countConfig++; |
||
67 | |||
68 | // ID is key |
||
69 | if (!array_key_exists('id', $property)) { |
||
70 | $property['id'] = $key; |
||
71 | } |
||
72 | |||
73 | // is entry already exist? |
||
74 | if ($menu->has($property['id'])) { |
||
75 | // remove a disabled entry |
||
76 | if (array_key_exists('enabled', $property) && false === $property['enabled']) { |
||
77 | call_user_func_array($this->builder->getMessageCb(), [ |
||
78 | 'MENU_PROGRESS', |
||
79 | sprintf('%s > %s (removed)', $menu, $property['id']), |
||
80 | $countConfig, |
||
81 | $totalConfig |
||
82 | ]); |
||
83 | $menu->remove($property['id']); |
||
84 | continue; |
||
85 | } |
||
86 | // merge properties |
||
87 | $current = $menu->get($property['id'])->toArray(); |
||
88 | $property = array_merge($current, $property); |
||
89 | call_user_func_array($this->builder->getMessageCb(), [ |
||
90 | 'MENU_PROGRESS', |
||
91 | sprintf('%s > %s (updated)', $menu, $property['id']), |
||
92 | $countConfig, |
||
93 | $totalConfig |
||
94 | ]); |
||
95 | } else { |
||
96 | call_user_func_array($this->builder->getMessageCb(), [ |
||
97 | 'MENU_PROGRESS', |
||
98 | sprintf('%s > %s', $menu, $property['id']), |
||
99 | $countConfig, |
||
100 | $totalConfig |
||
101 | ]); |
||
102 | } |
||
103 | // add/replace entry |
||
104 | $item = (new Entry($property['id'])) |
||
105 | ->setName($property['name'] ?? ucfirst($key)) |
||
106 | ->setUrl($property['url'] ?? '/noURL') |
||
107 | ->setWeight($property['weight'] ?? 0); |
||
108 | $menu->add($item); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $this->builder->setMenus($this->menus); |
||
114 | } |
||
115 | |||
187 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.