Conditions | 7 |
Paths | 10 |
Total Lines | 64 |
Code Lines | 32 |
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 |
||
49 | public function populate(Database $db, EntityTable $entities, Plugins $plugins, $installed) { |
||
50 | |||
51 | // get subtypes |
||
52 | $rows = $db->getData(" |
||
53 | SELECT * |
||
54 | FROM {$db->prefix}entity_subtypes |
||
55 | "); |
||
56 | foreach ($rows as $row) { |
||
57 | $this->subtype_data[$row->id] = $row; |
||
58 | } |
||
59 | |||
60 | // get site entity |
||
61 | $this->site = $entities->get(1, 'site'); |
||
|
|||
62 | if (!$this->site && $installed) { |
||
63 | throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down."); |
||
64 | } |
||
65 | |||
66 | // get plugins |
||
67 | $this->active_plugins = $plugins->find('active'); |
||
68 | |||
69 | // get plugin settings |
||
70 | if (!$this->active_plugins) { |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | // find GUIDs with not too many private settings |
||
75 | $guids = array_map(function (\ElggPlugin $plugin) { |
||
76 | return $plugin->guid; |
||
77 | }, $this->active_plugins); |
||
78 | |||
79 | // find plugin GUIDs with not too many settings |
||
80 | $limit = 40; |
||
81 | $set = implode(',', $guids); |
||
82 | $sql = " |
||
83 | SELECT entity_guid |
||
84 | FROM {$db->prefix}private_settings |
||
85 | WHERE entity_guid IN ($set) |
||
86 | AND name NOT LIKE 'plugin:user_setting:%' |
||
87 | GROUP BY entity_guid |
||
88 | HAVING COUNT(*) > $limit |
||
89 | "; |
||
90 | $unsuitable_guids = $db->getData($sql, function ($row) { |
||
91 | return (int) $row->entity_guid; |
||
92 | }); |
||
93 | $guids = array_values($guids); |
||
94 | $guids = array_diff($guids, $unsuitable_guids); |
||
95 | |||
96 | if ($guids) { |
||
97 | // get the settings |
||
98 | $set = implode(',', $guids); |
||
99 | $rows = $db->getData(" |
||
100 | SELECT entity_guid, `name`, `value` |
||
101 | FROM {$db->prefix}private_settings |
||
102 | WHERE entity_guid IN ($set) |
||
103 | AND name NOT LIKE 'plugin:user_setting:%' |
||
104 | ORDER BY entity_guid |
||
105 | "); |
||
106 | // make sure we show all entities as loaded |
||
107 | $this->plugin_settings = array_fill_keys($guids, []); |
||
108 | foreach ($rows as $i => $row) { |
||
109 | $this->plugin_settings[$row->entity_guid][$row->name] = $row->value; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
150 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.