CoinAddress   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 132
Duplicated Lines 46.97 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 1
dl 62
loc 132
ccs 0
cts 79
cp 0
rs 9.28
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getCoinAddress() 16 16 7
B getAPThreshold() 16 16 7
A existsCoinAddress() 0 4 1
B add() 8 12 7
A remove() 3 8 5
B update() 11 19 11

How to fix   Duplicated Code   

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:

1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class CoinAddress extends Base {
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 @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

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
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...
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
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...
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
Bug Best Practice introduced by
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 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...
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
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...
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
Duplication introduced by
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
Duplication introduced by
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
Duplication introduced by
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
Duplication introduced by
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
Duplication introduced by
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
Duplication introduced by
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