These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
||
3 | |||
4 | class CoinAddress extends Base { |
||
0 ignored issues
–
show
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...
|
|||
5 | protected $table = 'coin_addresses'; |
||
6 | |||
7 | /** |
||
8 | * We allow changing the database for shared accounts across pools |
||
9 | * Load the config on construct so we can assign the DB name |
||
10 | * @param config array MPOS configuration |
||
11 | * @return none |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Adding a
@return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.
Adding a Please refer to the PHP core documentation on constructors.
Loading history...
|
|||
12 | **/ |
||
13 | public function __construct($config) { |
||
14 | $this->setConfig($config); |
||
15 | $this->table = $this->config['db']['shared']['accounts'] . '.' . $this->table; |
||
0 ignored issues
–
show
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...
|
|||
16 | } |
||
17 | |||
18 | /** |
||
19 | * Fetch users coin address for a currency |
||
20 | * @param userID int UserID |
||
21 | * @return data string Coin Address |
||
22 | **/ |
||
23 | View Code Duplication | public function getCoinAddress($userID, $currency=NULL) { |
|
0 ignored issues
–
show
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...
|
|||
24 | if ($currency === NULL) $currency = $this->config['currency']; |
||
25 | $this->debug->append("STA " . __METHOD__, 4); |
||
26 | $stmt = $this->mysqli->prepare(" |
||
27 | SELECT coin_address |
||
28 | FROM " . $this->getTableName() . " |
||
29 | WHERE account_id = ? AND currency = ? |
||
30 | "); |
||
31 | if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) { |
||
32 | if ($result->num_rows == 1) { |
||
33 | return $result->fetch_object()->coin_address; |
||
34 | } |
||
35 | } |
||
36 | $this->debug->append("Unable to fetch users coin address for " . $currency); |
||
37 | return $this->sqlError(); |
||
0 ignored issues
–
show
The return type of
return $this->sqlError(); (boolean ) is incompatible with the return type documented by CoinAddress::getCoinAddress of type data .
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
Loading history...
|
|||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Fetch users Auto Payout Threshold for a currency |
||
42 | * @param UserID int UserID |
||
43 | * @return mixed Float value for threshold, false on error |
||
44 | **/ |
||
45 | View Code Duplication | public function getAPThreshold($userID, $currency=NULL) { |
|
0 ignored issues
–
show
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...
|
|||
46 | if ($currency === NULL) $currency = $this->config['currency']; |
||
47 | $this->debug->append("STA " . __METHOD__, 4); |
||
48 | $stmt = $this->mysqli->prepare(" |
||
49 | SELECT ap_threshold |
||
50 | FROM " . $this->getTableName() . " |
||
51 | WHERE account_id = ? AND currency = ? |
||
52 | "); |
||
53 | if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) { |
||
54 | if ($result->num_rows == 1) { |
||
55 | return $result->fetch_object()->ap_threshold; |
||
56 | } |
||
57 | } |
||
58 | $this->debug->append("Unable to fetch users auto payout threshold for " . $currency); |
||
59 | return $this->sqlError(); |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Check if a coin address is already set |
||
65 | * @param address string Coin Address to check for |
||
66 | * @return bool true or false |
||
67 | **/ |
||
68 | public function existsCoinAddress($address) { |
||
69 | $this->debug->append("STA " . __METHOD__, 4); |
||
70 | return $this->getSingle($address, 'coin_address', 'coin_address', 's') === $address; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Add a new coin address record for a user |
||
75 | * @param userID int Account ID |
||
76 | * @param address string Coin Address |
||
77 | * @param currency string Currency short handle, defaults to config option |
||
78 | * @return bool true or false |
||
79 | **/ |
||
80 | public function add($userID, $address, $currency=NULL) { |
||
81 | if ($currency === NULL) $currency = $this->config['currency']; |
||
82 | View Code Duplication | if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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...
|
|||
83 | $this->setErrorMessage('Unable to update coin address, address already exists'); |
||
84 | return false; |
||
85 | } |
||
86 | $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (account_id, currency, coin_address) VALUES (?, ?, ?)"); |
||
87 | View Code Duplication | if ( $this->checkStmt($stmt) && $stmt->bind_param('iss', $userID, $currency, $address) && $stmt->execute()) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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...
|
|||
88 | return true; |
||
89 | } |
||
90 | return $this->sqlError(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Remove a coin address record for a user |
||
95 | * @param userID int Account ID |
||
96 | * @param currency string Currency short handle, defaults to config option |
||
97 | * @return bool true or false |
||
98 | **/ |
||
99 | public function remove ($userID, $currency=NULL) { |
||
100 | if ($currency === NULL) $currency = $this->config['currency']; |
||
101 | $stmt = $this->mysqli->prepare("DELETE FROM " . $this->getTableName() . " WHERE account_id = ? AND currency = ?"); |
||
102 | View Code Duplication | if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute()) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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...
|
|||
103 | return true; |
||
104 | } |
||
105 | return $this->sqlError(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Update a coin address record for a user and a currency |
||
110 | * @param userID int Account ID |
||
111 | * @param address string Coin Address |
||
112 | * @param ap_threshold float Threshold for auto payouts for this currency |
||
113 | * @param currency string Currency short handle, defaults to config option |
||
114 | * @return bool true or false |
||
115 | **/ |
||
116 | public function update($userID, $address, $ap_threshold, $currency=NULL) { |
||
117 | if ($currency === NULL) $currency = $this->config['currency']; |
||
118 | View Code Duplication | if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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...
|
|||
119 | $this->setErrorMessage('Unable to update coin address, address already exists'); |
||
120 | return false; |
||
121 | } |
||
122 | if ($this->getCoinAddress($userID) != NULL) { |
||
123 | $stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ?, ap_threshold = ? WHERE account_id = ? AND currency = ?"); |
||
124 | View Code Duplication | if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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...
|
|||
125 | return true; |
||
126 | } |
||
127 | } else { |
||
128 | $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, ap_threshold, account_id, currency) VALUES (?, ?, ?, ?)"); |
||
129 | View Code Duplication | if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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...
|
|||
130 | return true; |
||
131 | } |
||
132 | } |
||
133 | return $this->sqlError(); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $coin_address = new CoinAddress($config); |
||
138 | $coin_address->setDebug($debug); |
||
139 | $coin_address->setMysql($mysqli); |
||
140 | $coin_address->setErrorCodes($aErrorCodes); |
||
141 |
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.