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

db_mysql_v4::mysql_connect()   D

Complexity

Conditions 10
Paths 65

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 65
nop 1
dl 0
loc 28
ccs 0
cts 19
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
/**
5
 * User: Gorlum
6
 * Date: 02.09.2015
7
 * Time: 0:41
8
 */
9
class db_mysql_v4 {
10
  const DB_MYSQL_TRANSACTION_SERIALIZABLE = 'SERIALIZABLE';
11
  const DB_MYSQL_TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ';
12
  const DB_MYSQL_TRANSACTION_READ_COMMITTED = 'READ COMMITTED';
13
  const DB_MYSQL_TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED';
14
15
  /**
16
   * Соединение с MySQL
17
   *
18
   * @var resource $link
19
   */
20
  public $link;
21
  /**
22
   * Статус соеднения с MySQL
23
   *
24
   * @var bool
25
   */
26
  public $connected = false;
27
28
  // 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...
29
30
  public function mysql_connect($settings) {
31
    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...
32
33
    static $need_keys = array('server', 'user', 'pass', 'name', 'prefix');
34
35
    if ($this->connected) {
36
      return true;
37
    }
38
39
    if (empty($settings) || !is_array($settings) || array_intersect($need_keys, array_keys($settings)) != $need_keys) {
40
      $debug->error_fatal('There is missconfiguration in your config.php. Check it again', $this->mysql_error());
41
    }
42
43
    // TODO !!!!!! DEBUG -> error!!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
44
    @$this->link = mysql_connect($settings['server'], $settings['user'], $settings['pass']);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_connect() has been deprecated: 5.5 Open a connection to a MySQL Server ( Ignorable by Annotation )

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

44
    @$this->link = /** @scrutinizer ignore-deprecated */ mysql_connect($settings['server'], $settings['user'], $settings['pass']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
45
    if (!is_resource($this->link)) {
46
      $debug->error_fatal('DB Error - cannot connect to server', $this->mysql_error());
47
    }
48
49
    $this->mysql_query("/*!40101 SET NAMES 'utf8' */") or $debug->error_fatal('DB error - cannot set names', $this->mysql_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...
50
    $this->mysql_query("SET NAMES 'utf8';") or $debug->error_fatal('DB error - cannot set names', $this->mysql_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...
51
52
    mysql_select_db($settings['name']) or $debug->error_fatal('DB error - cannot find DB on server', $this->mysql_error());
0 ignored issues
show
Deprecated Code introduced by
The function mysql_select_db() has been deprecated: 5.5 Select a MySQL database ( Ignorable by Annotation )

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

52
    /** @scrutinizer ignore-deprecated */ mysql_select_db($settings['name']) or $debug->error_fatal('DB error - cannot find DB on server', $this->mysql_error());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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...
53
    $this->mysql_query('SET SESSION TRANSACTION ISOLATION LEVEL ' . self::DB_MYSQL_TRANSACTION_REPEATABLE_READ . ';') or $debug->error_fatal('DB error - cannot set desired isolation level', $this->mysql_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...
54
55
    $this->connected = true;
56
57
    return true;
58
  }
59
60
  public function mysql_query($query_string) {
61
    return mysql_query($query_string, $this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

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

61
    return /** @scrutinizer ignore-deprecated */ mysql_query($query_string, $this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
  }
63
64
  public function mysql_fetch_assoc(&$query) {
65
    return mysql_fetch_assoc($query);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_fetch_assoc() has been deprecated: 5.5 Fetch a result row as an associative array ( Ignorable by Annotation )

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

65
    return /** @scrutinizer ignore-deprecated */ mysql_fetch_assoc($query);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66
  }
67
68
  public function mysql_fetch_row(&$query) {
69
    return mysql_fetch_row($query);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_fetch_row() has been deprecated: 5.5 Get a result row as an enumerated array ( Ignorable by Annotation )

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

69
    return /** @scrutinizer ignore-deprecated */ mysql_fetch_row($query);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
  }
71
72
  public function mysql_real_escape_string($unescaped_string) {
73
    return mysql_real_escape_string($unescaped_string, $this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_real_escape_string() has been deprecated: 5.5 Escapes special characters in a string for use in an SQL statement ( Ignorable by Annotation )

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

73
    return /** @scrutinizer ignore-deprecated */ mysql_real_escape_string($unescaped_string, $this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
74
  }
75
76
  public function mysql_close_link() {
77
    if ($this->connected) {
78
      $this->connected = false;
79
      mysql_close($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_close() has been deprecated: 5.5 Close MySQL connection ( Ignorable by Annotation )

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

79
      /** @scrutinizer ignore-deprecated */ mysql_close($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
80
      unset($this->link);
81
    }
82
83
    return true;
84
    // return mysql_close($this->link);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
85
  }
86
87
  public function mysql_error() {
88
    return mysql_error($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_error() has been deprecated: 5.5 Returns the text of the error message from previous MySQL operation ( Ignorable by Annotation )

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

88
    return /** @scrutinizer ignore-deprecated */ mysql_error($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89
  }
90
91
  public function mysql_insert_id() {
92
    return mysql_insert_id($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_insert_id() has been deprecated: 5.5 Get the ID generated in the last query ( Ignorable by Annotation )

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

92
    return /** @scrutinizer ignore-deprecated */ mysql_insert_id($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
93
  }
94
95
  public function mysql_num_rows(&$result) {
96
    return mysql_num_rows($result);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_num_rows() has been deprecated: 5.5 ( Ignorable by Annotation )

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

96
    return /** @scrutinizer ignore-deprecated */ mysql_num_rows($result);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
97
  }
98
99
  public function mysql_affected_rows() {
100
    return mysql_affected_rows($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_affected_rows() has been deprecated: 5.5 Get number of affected rows in previous MySQL operation ( Ignorable by Annotation )

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

100
    return /** @scrutinizer ignore-deprecated */ mysql_affected_rows($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
101
  }
102
103
  public function mysql_get_client_info() {
104
    return mysql_get_client_info();
0 ignored issues
show
Deprecated Code introduced by
The function mysql_get_client_info() has been deprecated: 5.5 Get MySQL client info ( Ignorable by Annotation )

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

104
    return /** @scrutinizer ignore-deprecated */ mysql_get_client_info();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
105
  }
106
107
  public function mysql_get_server_info() {
108
    return mysql_get_server_info($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_get_server_info() has been deprecated: 5.5 Get MySQL server info ( Ignorable by Annotation )

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

108
    return /** @scrutinizer ignore-deprecated */ mysql_get_server_info($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
109
  }
110
111
  public function mysql_get_host_info() {
112
    return mysql_get_host_info($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_get_host_info() has been deprecated: 5.5 Get MySQL host info ( Ignorable by Annotation )

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

112
    return /** @scrutinizer ignore-deprecated */ mysql_get_host_info($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
113
  }
114
115
  public function mysql_stat() {
116
    return mysql_stat($this->link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_stat() has been deprecated: 5.5 Get current system status ( Ignorable by Annotation )

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

116
    return /** @scrutinizer ignore-deprecated */ mysql_stat($this->link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
117
  }
118
}
119