Test Failed
Push — master ( c2873c...a077d1 )
by Jeroen
01:35
created

BootData::populate()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 64
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 10
nop 4
dl 0
loc 64
rs 7.2058
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Elgg;
4
5
use Elgg\Database;
6
use Elgg\Database\EntityTable;
7
use Elgg\Database\Plugins;
8
use Elgg\Database\SiteSecret;
9
10
/**
11
 * Serializable collection of data used to boot Elgg
12
 *
13
 * @access private
14
 * @since 2.1
15
 */
16
class BootData {
17
18
	/**
19
	 * @var \ElggSite|false
20
	 */
21
	private $site = false;
22
23
	/**
24
	 * @var \stdClass[]
25
	 */
26
	private $subtype_data = [];
27
28
	/**
29
	 * @var \ElggPlugin[]
30
	 */
31
	private $active_plugins = [];
32
33
	/**
34
	 * @var array
35
	 */
36
	private $plugin_settings = [];
37
38
	/**
39
	 * Populate the boot data
40
	 *
41
	 * @param Database    $db        Elgg database
42
	 * @param EntityTable $entities  Entities service
43
	 * @param Plugins     $plugins   Plugins service
44
	 * @param bool        $installed Is the site installed?
45
	 *
46
	 * @return void
47
	 * @throws \InstallationException
48
	 */
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');
0 ignored issues
show
Documentation Bug introduced by
It seems like $entities->get(1, 'site') can also be of type object<ElggEntity> or object<stdClass>. However, the property $site is declared as type object<ElggSite>|false. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->active_plugins of type ElggPlugin[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $guids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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
114
	/**
115
	 * Get the site entity
116
	 *
117
	 * @return \ElggSite|false False if not installed
118
	 */
119
	public function getSite() {
120
		return $this->site;
121
	}
122
123
	/**
124
	 * Get the subtype data
125
	 *
126
	 * @return \stdClass[]
127
	 */
128
	public function getSubtypeData() {
129
		return $this->subtype_data;
130
	}
131
132
	/**
133
	 * Get active plugins
134
	 *
135
	 * @return \ElggPlugin[]
136
	 */
137
	public function getActivePlugins() {
138
		return $this->active_plugins;
139
	}
140
141
	/**
142
	 * Get the plugin settings (may not include all active plugins)
143
	 *
144
	 * @return array
145
	 */
146
	public function getPluginSettings() {
147
		return $this->plugin_settings;
148
	}
149
}
150