Completed
Push — trunk ( 0e7a2e...6a6c5e )
by SuperNova.WS
07:28
created

db_mysql_v5::mysql_connect()   D

Complexity

Conditions 10
Paths 33

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 33
nop 1
dl 0
loc 35
ccs 0
cts 21
cp 0
crap 110
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

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 DBAL;
4
use mysqli;
5
use mysqli_result;
6
7
/**
8
 * User: Gorlum
9
 * Date: 02.09.2015
10
 * Time: 0:41
11
 */
12
class db_mysql_v5 {
13
  const DB_MYSQL_TRANSACTION_SERIALIZABLE = 'SERIALIZABLE';
14
  const DB_MYSQL_TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ';
15
  const DB_MYSQL_TRANSACTION_READ_COMMITTED = 'READ COMMITTED';
16
  const DB_MYSQL_TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED';
17
18
  /**
19
   * Соединение с MySQL
20
   *
21
   * @var mysqli $link
22
   */
23
  public $link;
24
  /**
25
   * Статус соеднения с MySQL
26
   *
27
   * @var bool
28
   */
29
  public $connected = false;
30
31
  // public $dbsettings = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
33
  public function mysql_connect($settings) {
34
    global $debug;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
35
36
    static $need_keys = array('server', 'user', 'pass', 'name', 'prefix');
37
38
    if ($this->connected) {
39
      return true;
40
    }
41
42
    if (empty($settings) || !is_array($settings) || array_intersect($need_keys, array_keys($settings)) != $need_keys) {
43
      $debug->error_fatal('There is missconfiguration in your config.php. Check it again');
44
    }
45
46
//    @$this->link = mysql_connect($settings['server'], $settings['user'], $settings['pass']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
//    if(!is_resource($this->link)) {
48
//      $debug->error_fatal('DB Error - cannot connect to server', $this->mysql_error());
49
//    }
50
    @$this->link = mysqli_connect($settings['server'], $settings['user'], $settings['pass'], $settings['name']);
51
    if (!is_object($this->link) || $this->link->connect_error) {
52
      $debug->error_fatal('DB Error - cannot connect to server error #' . $this->link->connect_errno, $this->link->connect_error);
53
    }
54
55
56
    $this->mysql_query("/*!40101 SET NAMES 'utf8' */")
57
    or $debug->error_fatal('DB error - cannot set names 1 error #' . $this->link->errno, $this->link->error);
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):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

// 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...
58
    $this->mysql_query("SET NAMES 'utf8';")
59
    or $debug->error_fatal('DB error - cannot set names 2 error #' . $this->link->errno, $this->link->error);
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):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

// 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...
60
61
    //mysql_select_db($settings['name']) or $debug->error_fatal('DB error - cannot find DB on server', $this->mysql_error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
    $this->mysql_query('SET SESSION TRANSACTION ISOLATION LEVEL ' . self::DB_MYSQL_TRANSACTION_SERIALIZABLE . ';')
63
    or $debug->error_fatal('DB error - cannot set desired isolation level error #' . $this->link->errno, $this->link->error);
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):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

// 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...
64
65
    $this->connected = true;
66
67
    return true;
68
  }
69
70
  public function mysql_query($query_string) {
71
    return $this->link->query($query_string);
72
  }
73
74
  /**
75
   * @param mysqli_result $query_result
76
   *
77
   * @return array|null
78
   */
79
  public function mysql_fetch_assoc(&$query_result) {
80
    return mysqli_fetch_assoc($query_result);
81
  }
82
83
  public function mysql_fetch_row(&$query) {
84
    return mysqli_fetch_row($query);
85
  }
86
87
  public function mysql_real_escape_string($unescaped_string) {
88
    return mysqli_real_escape_string($this->link, $unescaped_string);
89
  }
90
91
  public function mysql_close_link() {
92
    if (is_object($this->link)) {
93
      $this->link->close();
94
      $this->connected = false;
95
      unset($this->link);
96
    }
97
98
    return true;
99
  }
100
101
  public function mysql_error() {
102
    return mysqli_error($this->link);
103
  }
104
105
  /**
106
   * @return int|string
107
   */
108
  public function mysql_insert_id() {
109
    return mysqli_insert_id($this->link);
110
  }
111
112
  public function mysql_num_rows(&$result) {
113
    return mysqli_num_rows($result);
114
  }
115
116
  public function mysql_affected_rows() {
117
    return mysqli_affected_rows($this->link);
118
  }
119
120
  public function mysql_get_client_info() {
121
    return mysqli_get_client_info();
122
  }
123
124
  public function mysql_get_server_info() {
125
    return mysqli_get_server_info($this->link);
126
  }
127
128
  public function mysql_get_host_info() {
129
    return mysqli_get_host_info($this->link);
130
  }
131
132
  public function mysql_stat() {
133
    return mysqli_stat($this->link);
134
  }
135
}
136