Completed
Push — mysql_improvements ( 13c4ea...013528 )
by George
03:07 queued 44s
created

StatsModel::getItems()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 65
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 6.0208

Importance

Changes 13
Bugs 0 Features 1
Metric Value
c 13
b 0
f 1
dl 0
loc 65
ccs 33
cts 36
cp 0.9167
rs 8.6195
cc 6
eloc 33
nc 5
nop 1
crap 6.0208

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 Stats\Models;
4
5
use Joomla\Database\Query\LimitableInterface;
6
use Joomla\Model\AbstractDatabaseModel;
7
8
/**
9
 * Statistics database model
10
 *
11
 * @since  1.0
12
 */
13
class StatsModel extends AbstractDatabaseModel
14
{
15
	/**
16
	 * The query batch size
17
	 *
18
	 * @var    integer
19
	 * @since  1.0
20
	 */
21
	private $batchSize = 25000;
22
23
	/**
24
	 * Loads the statistics data from the database.
25
	 *
26
	 * @param   string  $column  A single column to filter on
27
	 *
28
	 * @return  \array[]  Array of data arrays.
29
	 *
30
	 * @since   1.0
31
	 * @throws  \InvalidArgumentException
32
	 */
33 3
	public function getItems($column = null)
34
	{
35 3
		$db = $this->getDb();
36
37
		// Validate the requested column is actually in the table
38 3
		if ($column !== null)
39 3
		{
40 2
			$columnList = $db->getTableColumns('#__jstats');
41
42
			// The column should exist in the table and be part of the API
43 2
			if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified']))
44 2
			{
45 1
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
46
			}
47
48 1
			return $db->setQuery(
49 1
				$db->getQuery(true)
50 1
					->select('*')
51 1
					->from('#__jstats_counter_' . $column)
52 1
			)->loadAssocList($column);
53
		}
54
55
		// If fetching all data from the table, we need to break this down a fair bit otherwise we're going to run out of memory
56 1
		$totalRecords = $db->setQuery(
57 1
			$db->getQuery(true)
58 1
				->select('COUNT(unique_id)')
59 1
				->from('#__jstats')
60 1
		)->loadResult();
61
62 1
		$return = [];
63
64 1
		$query = $db->getQuery(true)
65 1
			->select(['php_version', 'db_type', 'db_version', 'cms_version', 'server_os'])
66 1
			->from('#__jstats')
67 1
			->group('unique_id');
68
69 1
		$limitable = $query instanceof LimitableInterface;
70
71
		// We can't have this as a single array, we run out of memory... This is gonna get interesting...
72 1
		for ($offset = 0; $offset < $totalRecords; $offset + $this->batchSize)
73
		{
74
			if ($limitable)
75 1
			{
76
				$query->setLimit($this->batchSize, $offset);
77
78
				$db->setQuery($query);
79
			}
80
			else
81
			{
82 1
				$db->setQuery($query, $offset, $this->batchSize);
83
			}
84
85 1
			$return[] = $db->loadAssocList();
86
87 1
			$offset += $this->batchSize;
88 1
		}
89
90
		// Disconnect the DB to free some memory
91 1
		$db->disconnect();
92
93
		// And unset some variables
94 1
		unset($db, $query, $offset, $totalRecords);
95
96 1
		return $return;
97
	}
98
99
	/**
100
	 * Saves the given data.
101
	 *
102
	 * @param   \stdClass  $data  Data object to save.
103
	 *
104
	 * @return  void
105
	 *
106
	 * @since   1.0
107
	 */
108 2
	public function save($data)
109
	{
110 2
		$db = $this->getDb();
111
112
		// Set the modified date of the record
113 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
114
115
		// Check if a row exists for this unique ID and update the existing record if so
116 2
		$recordExists = $db->setQuery(
117 2
			$db->getQuery(true)
118 2
				->select('unique_id')
119 2
				->from('#__jstats')
120 2
				->where('unique_id = ' . $db->quote($data->unique_id))
121 2
		)->loadResult();
122
123
		if ($recordExists)
124 2
		{
125 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
126 1
		}
127
		else
128
		{
129 1
			$db->insertObject('#__jstats', $data, ['unique_id']);
0 ignored issues
show
Documentation introduced by
array('unique_id') is of type array<integer,string,{"0":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
		}
131 2
	}
132
}
133