db_search()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.1239

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 5
nop 0
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
ccs 4
cts 11
cp 0.3636
crap 8.1239
1
<?php
2
3
/**
4
 * This file has all the main functions in it that set up the database connection
5
 * and initializes the appropriate adapters.
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
use ElkArte\Database\AbstractTable;
16
use ElkArte\Database\QueryInterface;
17
18
/**
19
 * Initialize database classes and connection.
20
 *
21
 * @param string $db_server server name
22
 * @param string $db_name database name like forum
23
 * @param string $db_user userid to attempt db connection
24
 * @param string $db_passwd password of user to attempt db connection
25
 * @param string $db_prefix prefix of the database, like elkarte_
26
 * @param array $db_options
27
 * @param string $db_type
28
 *
29
 * @return QueryInterface
30
 */
31
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')
0 ignored issues
show
Unused Code introduced by
The parameter $db_prefix is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, /** @scrutinizer ignore-unused */ $db_prefix, $db_options = array(), $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $db_passwd is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate($db_server, $db_name, $db_user, /** @scrutinizer ignore-unused */ $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $db_server is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate(/** @scrutinizer ignore-unused */ $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $db_user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate($db_server, $db_name, /** @scrutinizer ignore-unused */ $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $db_type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), /** @scrutinizer ignore-unused */ $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $db_name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate($db_server, /** @scrutinizer ignore-unused */ $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $db_options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, /** @scrutinizer ignore-unused */ $db_options = array(), $db_type = 'mysql')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
{
33
	return database(false);
34
}
35
36
/**
37
 * Retrieve existing instance of the active database class.
38
 *
39
 * @param bool $fatal - Stop the execution or throw an \Exception
40
 * @param bool $force - Force the re-creation of the database instance.
41
 *                      If set to true, from that moment onwards the old
42
 *                      instance will be lost and only the new one returned
43
 *
44
 * @return QueryInterface
45
 * @throws \Exception if fatal is false
46
 */
47 387
function database($fatal = true, $force = false)
48
{
49 387
	static $db = null;
50
51 1
	if ($db === null || $force === true)
52 1
	{
53
		global $db_persist, $db_server, $db_user, $db_passwd, $db_port;
54
		global $db_type, $db_name, $db_prefix, $mysql_set_mode;
55 1
56
		$db_options = [
57 1
			'persist' => $db_persist,
58 1
			'select_db' => true,
59
			'port' => $db_port,
60 1
			'mysql_set_mode' => (bool) ($mysql_set_mode ?? false)
61 1
		];
62 1
		$type = strtolower($db_type);
63
		$type = $type === 'mysql' ? 'mysqli' : $type;
64
65 1
		/** @var \ElkArte\Database\Mysqli\Connection $class */
66
		$class = '\\ElkArte\\Database\\' . ucfirst($type) . '\\Connection';
67
		try
68
		{
69
			$db = $class::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
70
		}
71
		catch (\Exception $e)
72
		{
73
			if ($fatal === true)
74
			{
75
				\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage());
0 ignored issues
show
Bug introduced by
The type ElkArte\Errors\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
			}
77
			else
78
			{
79
				throw $e;
80 387
			}
81
		}
82
	}
83
84
	return $db;
85
}
86
87
/**
88
 * This function retrieves an existing instance of \ElkArte\AbstractTable
89
 * and returns it.
90
 *
91
 * @param object|null $db - A database object (e.g. \ElkArte\Mysqli\Query)
92
 * @param bool $fatal - Stop the execution or throw an \Exception
93
 *
94
 * @return AbstractTable
95 3
 */
96 3
function db_table($db = null, $fatal = false)
97
{
98 3
	global $db_prefix, $db_type;
99
	static $db_table = null;
100 1
101
	if ($db_table === null)
102 1
	{
103
		if ($db === null)
104 1
		{
105 1
			$db = database();
106 1
		}
107
		$db_type = strtolower($db_type);
108
		$db_type = $db_type === 'mysql' ? 'mysqli' : $db_type;
109 1
		$class = '\\ElkArte\\Database\\' . ucfirst($db_type) . '\\Table';
110
		try
111
		{
112
			$db_table = new $class($db, $db_prefix);
113
		}
114
		catch (\Exception $e)
115
		{
116
			if ($fatal === true)
117
			{
118
				\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage());
119
			}
120
			else
121
			{
122
				throw $e;
123
			}
124 3
		}
125
	}
126
127
	return $db_table;
128
}
129
130
/**
131
 * This function returns an instance of \ElkArte\AbstractSearch,
132
 * specifically designed for database utilities related to search.
133
 *
134
 * @return \ElkArte\Database\AbstractSearch
135
 */
136 30
function db_search()
137 30
{
138
	global $db_type;
139 30
	static $db_search = null;
140
141
	if ($db_search === null)
142
	{
143
		$db = database();
144
		$db_type = strtolower($db_type);
145
		$db_type = $db_type === 'mysql' ? 'mysqli' : $db_type;
146
		$class = '\\ElkArte\\Database\\' . ucfirst($db_type) . '\\Search';
147
		try
148
		{
149
			$db_search = new $class($db);
150
		}
151
		catch (\Exception $e)
152
		{
153
			\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage());
154
		}
155 30
	}
156
157
	return $db_search;
158
}
159