1 | <?php |
|||||||||||
2 | ||||||||||||
3 | /** |
|||||||||||
4 | * Connect to DB |
|||||||||||
5 | * |
|||||||||||
6 | * @since 1.0 |
|||||||||||
7 | */ |
|||||||||||
8 | function yourls_db_connect() { |
|||||||||||
9 | global $ydb; |
|||||||||||
10 | ||||||||||||
11 | if ( !defined( 'YOURLS_DB_USER' ) |
|||||||||||
12 | or !defined( 'YOURLS_DB_PASS' ) |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
13 | or !defined( 'YOURLS_DB_NAME' ) |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code.
Loading history...
|
||||||||||||
14 | or !defined( 'YOURLS_DB_HOST' ) |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code.
Loading history...
|
||||||||||||
15 | ) { |
|||||||||||
16 | yourls_die( yourls__( 'Incorrect DB config, please refer to documentation' ), yourls__( 'Fatal error' ), 503 ); |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
17 | } |
|||||||||||
18 | ||||||||||||
19 | $dbhost = YOURLS_DB_HOST; |
|||||||||||
20 | $user = YOURLS_DB_USER; |
|||||||||||
21 | $pass = YOURLS_DB_PASS; |
|||||||||||
22 | $dbname = YOURLS_DB_NAME; |
|||||||||||
23 | ||||||||||||
24 | // This action is deprecated |
|||||||||||
25 | yourls_do_action( 'set_DB_driver', 'deprecated' ); |
|||||||||||
26 | ||||||||||||
27 | // Get custom port if any |
|||||||||||
28 | if ( false !== strpos( $dbhost, ':' ) ) { |
|||||||||||
29 | list( $dbhost, $dbport ) = explode( ':', $dbhost ); |
|||||||||||
30 | $dbhost = sprintf( '%1$s;port=%2$d', $dbhost, $dbport ); |
|||||||||||
31 | } |
|||||||||||
32 | ||||||||||||
33 | $charset = yourls_apply_filter( 'db_connect_charset', 'utf8mb4' ); |
|||||||||||
34 | ||||||||||||
35 | /** |
|||||||||||
36 | * Data Source Name (dsn) used to connect the DB |
|||||||||||
37 | * |
|||||||||||
38 | * DSN with PDO is something like: |
|||||||||||
39 | * 'mysql:host=123.4.5.6;dbname=test_db;port=3306' |
|||||||||||
40 | * 'sqlite:/opt/databases/mydb.sq3' |
|||||||||||
41 | * 'pgsql:host=192.168.13.37;port=5432;dbname=omgwtf' |
|||||||||||
42 | */ |
|||||||||||
43 | $dsn = sprintf( 'mysql:host=%s;dbname=%s;charset=%s', $dbhost, $dbname, $charset ); |
|||||||||||
44 | $dsn = yourls_apply_filter( 'db_connect_custom_dsn', $dsn ); |
|||||||||||
45 | ||||||||||||
46 | /** |
|||||||||||
47 | * PDO driver options and attributes |
|||||||||||
48 | * |
|||||||||||
49 | * The PDO constructor is something like: |
|||||||||||
50 | * new PDO( string $dsn, string $username, string $password [, array $options ] ) |
|||||||||||
51 | * The driver options are passed to the PDO constructor, eg array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) |
|||||||||||
52 | * The attribute options are then set in a foreach($attr as $k=>$v){$db->setAttribute($k, $v)} loop |
|||||||||||
53 | */ |
|||||||||||
54 | $driver_options = yourls_apply_filter( 'db_connect_driver_option', [] ); // driver options as key-value pairs |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
55 | $attributes = yourls_apply_filter( 'db_connect_attributes', [] ); // attributes as key-value pairs |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
56 | ||||||||||||
57 | $ydb = new \YOURLS\Database\YDB( $dsn, $user, $pass, $driver_options, $attributes ); |
|||||||||||
58 | $ydb->init(); |
|||||||||||
59 | ||||||||||||
60 | // Past this point, we're connected |
|||||||||||
61 | yourls_debug_log( sprintf( 'Connected to database %s on %s ', $dbname, $dbhost ) ); |
|||||||||||
62 | ||||||||||||
63 | yourls_debug_mode( YOURLS_DEBUG ); |
|||||||||||
64 | ||||||||||||
65 | return $ydb; |
|||||||||||
66 | } |
|||||||||||
67 | ||||||||||||
68 | /** |
|||||||||||
69 | * Helper function : return instance of the DB |
|||||||||||
70 | * |
|||||||||||
71 | * Instead of: |
|||||||||||
72 | * global $ydb; |
|||||||||||
73 | * $ydb->do_stuff() |
|||||||||||
74 | * Prefer : |
|||||||||||
75 | * yourls_get_db()->do_stuff() |
|||||||||||
76 | * |
|||||||||||
77 | * @since 1.7.10 |
|||||||||||
78 | * @return \YOURLS\Database\YDB |
|||||||||||
79 | */ |
|||||||||||
80 | function yourls_get_db() { |
|||||||||||
81 | // Allow plugins to short-circuit the whole function |
|||||||||||
82 | 70 | $pre = yourls_apply_filter( 'shunt_get_db', false ); |
||||||||||
83 | 70 | if ( false !== $pre ) { |
||||||||||
84 | return $pre; |
|||||||||||
85 | } |
|||||||||||
86 | ||||||||||||
87 | 70 | global $ydb; |
||||||||||
88 | 70 | $ydb = ( isset( $ydb ) ) ? $ydb : yourls_db_connect(); |
||||||||||
89 | 70 | return yourls_apply_filter('get_db', $ydb); |
||||||||||
90 | } |
|||||||||||
91 | ||||||||||||
92 | /** |
|||||||||||
93 | * Helper function : set instance of DB, or unset it |
|||||||||||
94 | * |
|||||||||||
95 | * Instead of: |
|||||||||||
96 | * global $ydb; |
|||||||||||
97 | * $ydb = stuff |
|||||||||||
98 | * Prefer : |
|||||||||||
99 | * yourls_set_db( stuff ) |
|||||||||||
100 | * (This is mostly used in the test suite) |
|||||||||||
101 | * |
|||||||||||
102 | * @since 1.7.10 |
|||||||||||
103 | * @param mixed $db Either a \YOURLS\Database\YDB instance, or anything. If null, the function will unset $ydb |
|||||||||||
104 | */ |
|||||||||||
105 | function yourls_set_db($db) { |
|||||||||||
106 | 9 | global $ydb; |
||||||||||
107 | ||||||||||||
108 | 9 | if (is_null($db)) { |
||||||||||
109 | 2 | unset($ydb); |
||||||||||
110 | } else { |
|||||||||||
111 | 9 | $ydb = $db; |
||||||||||
112 | } |
|||||||||||
113 | } |
|||||||||||
114 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.