Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

Base   C

Complexity

Total Complexity 70

Size/Duplication

Total Lines 289
Duplicated Lines 3.46 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 70
lcom 2
cbo 0
dl 10
loc 289
rs 5.6163
c 0
b 0
f 0

41 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 3 1
A setDebug() 0 3 1
A setCoin() 0 3 1
A setCoinAddress() 0 3 1
A setLog() 0 3 1
A setMysql() 0 3 1
A setMail() 0 3 1
A setSalt() 0 3 1
A setSalty() 0 3 1
A setSmarty() 0 3 1
A setUser() 0 3 1
A setSessionManager() 0 3 1
A setConfig() 0 3 1
A setErrorCodes() 0 3 1
A setToken() 0 3 1
A setBlock() 0 3 1
A setPayout() 0 3 1
A setNotification() 0 3 1
A setTransaction() 0 3 1
A setMemcache() 0 3 1
A setStatistics() 0 3 1
A setSetting() 0 3 1
A setTools() 0 3 1
A setBitcoin() 0 3 1
A setTokenType() 0 3 1
A setCSRFToken() 0 3 1
A setShare() 0 3 1
A setErrorMessage() 0 5 1
A setCronMessage() 0 4 1
A getError() 0 3 1
A getCronError() 0 3 1
B getErrorMsg() 0 16 5
A getCount() 0 7 4
B getCountFiltered() 0 7 5
A getAllAssoc() 7 7 4
B getSingleAssoc() 0 7 5
A getSingle() 0 16 3
A checkStmt() 0 6 2
A sqlError() 0 12 2
B updateSingle() 0 9 5
A addParam() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
/**
5
 * Our base class that we extend our other classes from
6
 *
7
 * It supplies some basic features as cross-linking with other classes
8
 * after loading a newly created class.
9
 **/
10
class Base {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
  private $sError = '';
12
  private $sCronError = '';
13
  protected $table = '';
14
  private $values = array(), $types = ''; 
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
15
16
  public function getTableName() {
17
    return $this->table;
18
  }
19
  
20
  protected $debug;
21
  public function setDebug($debug) {
22
    $this->debug = $debug;
23
  }
24
  public function setCoin($coin) {
25
    $this->coin = $coin;
0 ignored issues
show
Bug introduced by
The property coin does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
  }
27
  public function setCoinAddress($coin_address) {
28
    $this->coin_address = $coin_address;
0 ignored issues
show
Bug introduced by
The property coin_address does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
  }
30
  
31
  public $log;
32
  public function setLog($log) {
33
    $this->log = $log;
34
  }
35
  
36
  protected $mysqli;
37
  public function setMysql($mysqli) {
38
    $this->mysqli = $mysqli;
39
  }
40
  public function setMail($mail) {
41
    $this->mail = $mail;
0 ignored issues
show
Bug introduced by
The property mail does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
  }
43
  public function setSalt($salt) {
44
    $this->salt = $salt;
0 ignored issues
show
Bug introduced by
The property salt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
  }
46
  public function setSalty($salt) {
47
    $this->salty = $salt;
0 ignored issues
show
Bug introduced by
The property salty does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
  }
49
  /**
50
   * @var Smarty
51
   */
52
  var $smarty;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $smarty.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
53
  public function setSmarty($smarty) {
54
    $this->smarty = $smarty;
55
  }
56
  public function setUser($user) {
57
    $this->user = $user;
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
  }
59
  public function setSessionManager($session) {
60
    $this->session = $session;
0 ignored issues
show
Bug introduced by
The property session does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
  }
62
  public function setConfig($config) {
63
    $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
  }
65
  
66
  protected $aErrorCodes;
67
  public function setErrorCodes(&$aErrorCodes) {
68
    $this->aErrorCodes =& $aErrorCodes;
69
  }
70
  public function setToken($token) {
71
    $this->token = $token;
0 ignored issues
show
Bug introduced by
The property token does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
  }
73
  public function setBlock($block) {
74
    $this->block = $block;
0 ignored issues
show
Bug introduced by
The property block does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
  }
76
  public function setPayout($payout) {
77
    $this->payout = $payout;
0 ignored issues
show
Bug introduced by
The property payout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
  }
79
  public function setNotification($notification) {
80
    $this->notification = $notification;
0 ignored issues
show
Bug introduced by
The property notification does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81
  }
82
  public function setTransaction($transaction) {
83
    $this->transaction = $transaction;
0 ignored issues
show
Bug introduced by
The property transaction does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
84
  }
85
  public function setMemcache($memcache) {
86
    $this->memcache = $memcache;
0 ignored issues
show
Bug introduced by
The property memcache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87
  }
88
  public function setStatistics($statistics) {
89
    $this->statistics = $statistics;
0 ignored issues
show
Bug introduced by
The property statistics does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
  }
91
  public function setSetting($setting) {
92
    $this->setting = $setting;
0 ignored issues
show
Bug introduced by
The property setting does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
93
  }
94
  public function setTools($tools) {
95
    $this->tools = $tools;
0 ignored issues
show
Bug introduced by
The property tools does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
96
  }
97
  public function setBitcoin($bitcoin) {
98
    $this->bitcoin = $bitcoin;
0 ignored issues
show
Bug introduced by
The property bitcoin does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
99
  }
100
  public function setTokenType($tokentype) {
101
    $this->tokentype = $tokentype;
0 ignored issues
show
Bug introduced by
The property tokentype does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
102
  }
103
  public function setCSRFToken($token) {
104
    $this->CSRFToken = $token;
0 ignored issues
show
Bug introduced by
The property CSRFToken does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
105
  }
106
  public function setShare($share) {
107
    $this->share = $share;
0 ignored issues
show
Bug introduced by
The property share does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
108
  }
109
  public function setErrorMessage($msg) {
110
    $this->sError = $msg;
111
    // Default to same error for crons
112
    $this->sCronError = $msg;
113
  }
114
  public function setCronMessage($msg) {
115
    // Used to overwrite any errors with a custom cron one
116
    $this->sCronError = $msg;
117
  }
118
  public function getError() {
119
    return $this->sError;
120
  }
121
  /**
122
   * Additional information in error string for cronjobs logging
123
   **/
124
  public function getCronError() {
125
    return $this->sCronError;
126
  }
127
128
  /**
129
   * Get error message from error code array
130
   * @param errCode string Error code string
131
   * @param optional string Optional addtitional error strings to append
132
   * @retrun string Error Message
133
   **/
134
  public function getErrorMsg($errCode='') {
135
    if (!is_array($this->aErrorCodes)) return 'Error codes not loaded';
136
    if (!array_key_exists($errCode, $this->aErrorCodes)) return 'Unknown Error Code: ' . $errCode;
137
    if (func_num_args() > 1) {
138
      $args = func_get_args();
139
      array_shift($args);
140
      $param_count = substr_count($this->aErrorCodes[$errCode], '%s');
141
      if ($param_count == count($args)) {
142
        return vsprintf($this->aErrorCodes[$errCode], $args);
143
      } else {
144
        return $this->aErrorCodes[$errCode] . ' (missing information to complete string)';
145
      }
146
    } else {
147
      return $this->aErrorCodes[$errCode];
148
    }
149
  }
150
151
  /**
152
   * Fetch count of all entries in table
153
   * @param none
154
   * @param data mixed Count or false
155
   **/
156
  public function getCount() {
157
    $this->debug->append("STA " . __METHOD__, 4);
158
    $stmt = $this->mysqli->prepare("SELECT COUNT(id) AS count FROM $this->table");
159
    if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result())
160
      return $result->fetch_object()->count;
161
    return $this->sqlError();
162
  }
163
164
  /**
165
   * Fetch count of all entries in table filtered by a column/value
166
   * @param none
167
   * @param data mixed Count or false
168
   **/
169
  public function getCountFiltered($column='id', $value=NULL, $type='i', $operator = '=') {
170
    $this->debug->append("STA " . __METHOD__, 4);
171
    $stmt = $this->mysqli->prepare("SELECT COUNT(id) AS count FROM $this->table WHERE $column $operator ?");
172
    if ($this->checkStmt($stmt) && $stmt->bind_param($type, $value) && $stmt->execute() && $result = $stmt->get_result())
173
      return $result->fetch_object()->count;
174
    return $this->sqlError();
175
  }
176
177
  /**
178
   * Fetch all entries as an assoc array from a table
179
   * This should, in general, not be used but sometimes it's just easier
180
   * @param none
181
   * @return array Assoc array of all rows found in table
182
   **/
183 View Code Duplication
  public function getAllAssoc() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    $this->debug->append("STA " . __METHOD__, 4);
185
    $stmt = $this->mysqli->prepare("SELECT * FROM $this->table");
186
    if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result())
187
      return $result->fetch_all(MYSQLI_ASSOC);
188
    return $this->sqlError();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->sqlError(); (boolean) is incompatible with the return type documented by Base::getAllAssoc of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
189
  }
190
191
  /**
192
   * Get a single row as an assoc array
193
   * @param value string Value to search for
194
   * @param field string Column to search for
195
   * @param type string Type of value
196
   * @return array Resulting row
197
   **/
198
  protected function getSingleAssoc($value, $field='id', $type='i') {
199
    $this->debug->append("STA " . __METHOD__, 4);
200
    $stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE $field = ? LIMIT 1");
201
    if ($this->checkStmt($stmt) && $stmt->bind_param($type, $value) && $stmt->execute() && $result = $stmt->get_result())
202
      return $result->fetch_assoc();
203
    return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Base::getSingleAssoc of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
204
  }
205
206
  /**
207
   * Get a single value from a row matching the query specified
208
   * @param value string Value to search for
209
   * @param search Return column to search for
210
   * @param field string Search column
211
   * @param type string Type of value
212
   * @param lower bool try with LOWER comparision
213
   * @return array Return result
214
   **/
215
  protected function getSingle($value, $search='id', $field='id', $type="i", $lower=false) {
216
    $this->debug->append("STA " . __METHOD__, 4); 
217
    $sql = "SELECT $search FROM $this->table WHERE";
218
    $lower ? $sql .= " LOWER($field) = LOWER(?)" : $sql .= " $field = ?";
219
    $sql .= " LIMIT 1";
220
    $stmt = $this->mysqli->prepare($sql);
221
    if ($this->checkStmt($stmt)) {
222
      $stmt->bind_param($type, $value);
223
      $stmt->execute();
224
      $stmt->bind_result($retval);
0 ignored issues
show
Bug introduced by
The variable $retval does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
225
      $stmt->fetch();
226
      $stmt->close();
227
      return $retval;
228
    }
229
    return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Base::getSingle of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
230
  }
231
232
  /**
233
   * Check if the prepared statement is valid
234
   * @param $bState Statement return value
235
   * @return bool true or false
236
   **/
237
  function checkStmt($bState) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
238
    $this->debug->append("STA " . __METHOD__, 4);
239
    if ($bState ===! true)
240
      return $this->sqlError();
241
    return true;
242
  }
243
244
  /**
245
   * Catch SQL errors with this method
246
   * @param error_code string Error code to read
247
   **/
248
  protected function sqlError($error_code='E0020') {
249
    // More human-readable error for UI
250
    if (func_num_args() == 0) {
251
      $this->setErrorMessage($this->getErrorMsg($error_code));
252
    } else {
253
      $this->setErrorMessage(call_user_func_array(array($this, 'getErrorMsg'), func_get_args()));
254
    }
255
    // Default to SQL error for debug and cron errors
256
    $this->debug->append($this->getErrorMsg('E0019', $this->mysqli->error));
257
    $this->setCronMessage($this->getErrorMsg('E0019', $this->mysqli->error));
258
    return false;
259
  }
260
261
  /**
262
   * @param userID int Account ID
263
   * Update a single row in a table
264
   * @param field string Field to update
265
   * @return bool
266
   **/
267
  protected function updateSingle($id, $field, $table='') {
268
    if (empty($table)) $table = $this->table;
269
    $this->debug->append("STA " . __METHOD__, 4);
270
    $stmt = $this->mysqli->prepare("UPDATE $table SET " . $field['name'] . " = ? WHERE id = ? LIMIT 1");
271
    if ($this->checkStmt($stmt) && $stmt->bind_param($field['type'].'i', $field['value'], $id) && $stmt->execute())
272
      return true;
273
    $this->debug->append("Unable to update " . $field['name'] . " with " . $field['value'] . " for ID $id");
274
    return $this->sqlError();
275
  }
276
277
  /**
278
   * We may need to generate our bind_param list
279
   **/
280
  public function addParam($type, &$value) {
281
    $this->values[] = $value;
282
    $this->types .= $type;
283
  }
284
  public function getParam() {
285
    $array = array_merge(array($this->types), $this->values);
286
    // Clear the data
287
    $this->values = NULL;
0 ignored issues
show
Documentation Bug introduced by
It seems like NULL of type null is incompatible with the declared type array of property $values.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
288
    $this->types = NULL;
289
    // See here why we need this: http://stackoverflow.com/questions/16120822/mysqli-bind-param-expected-to-be-a-reference-value-given
290
    if (strnatcmp(phpversion(),'5.3') >= 0) {
291
      $refs = array();
292
      foreach($array as $key => $value)
293
        $refs[$key] = &$array[$key];
294
      return $refs;
295
    }
296
    return $array;
297
  }
298
}
299