RegisterAccount   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 47.69 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 62
loc 130
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 3
A account_email_exists() 11 11 1
A is_registered() 14 14 1
A update_emailed_hash() 14 19 4
A update_password() 16 21 4

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
/**
3
 * The PHP Skeleton App
4
 *
5
 * @author      Goran Halusa <[email protected]>
6
 * @copyright   2015 Goran Halusa
7
 * @link        https://github.com/ghalusa/PHP-Skeleton-App
8
 * @license     https://github.com/ghalusa/PHP-Skeleton-App/wiki/License
9
 * @version     0.1.1
10
 * @package     PHP Skeleton App
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
/**
17
 * Register Account
18
 *
19
 * Class for registering accounts.
20
 *
21
 * @author      Goran Halusa <[email protected]>
22
 * @since       0.1.0
23
 */
24
25
namespace PHPSkeleton;
26
use PDO;
27
28
class RegisterAccount
29
{
30
  /**
31
   * @var string|bool  $session_key    The session key
32
   */
33
  private $session_key = false;
34
35
  /**
36
   * @var object  $db   The database connection object
37
   */
38
  public $db;
39
40
  /**
41
   * Constructor
42
   * @param object   $db_connection   The database connection object
43
   * @param string   $session_key     The session key
44
   */
45 View Code Duplication
  public function __construct( $db_connection = false, $session_key = false )
46
  {
47
      if ($db_connection && is_object($db_connection)) {
48
          $this->db = $db_connection;
49
      }
50
      $this->session_key = $session_key;
51
  }
52
53
  /**
54
   * Account Email Exists
55
   *
56
   * Run a query to determine if a user account email exists in the database.
57
   *
58
   * @param       string $user_account_email     The data value
59
   * @return      false|string                   The query result
60
   */
61 View Code Duplication
  public function account_email_exists( $user_account_email )
62
  {
63
      $statement = $this->db->prepare("SELECT 
64
            user_account_id
65
            ,user_account_email
66
          FROM user_account
67
          WHERE user_account_email = :user_account_email");
68
      $statement->bindValue(":user_account_email", $user_account_email, PDO::PARAM_INT);
69
      $statement->execute();
70
      return $statement->fetch(PDO::FETCH_ASSOC);
71
  }
72
73
  /**
74
   * Is Registered
75
   *
76
   * Run a query to determine if a user account has been registered.
77
   *
78
   * @param       int $user_account_id           The data value
79
   * @return      array|bool                     The query result
80
   */
81 View Code Duplication
  public function is_registered( $user_account_id )
82
  {
83
      $statement = $this->db->prepare("SELECT user_account.acceptable_use_policy
84
          ,GROUP_CONCAT(user_account_groups.group_id SEPARATOR ', ') AS groups
85
          FROM user_account
86
          LEFT JOIN user_account_groups ON user_account.user_account_id = user_account_groups.user_account_id
87
          WHERE user_account.user_account_id = :user_account_id
88
          AND user_account.acceptable_use_policy = 1
89
          GROUP BY user_account.user_account_id
90
          HAVING groups != ''");
91
      $statement->bindValue(":user_account_id", $user_account_id, PDO::PARAM_INT);
92
      $statement->execute();
93
      return $statement->fetch(PDO::FETCH_ASSOC);
94
  }
95
96
  /**
97
   * Update Emailed Hash
98
   *
99
   * Run a query to update an emailed hash.
100
   *
101
   * @param       int $user_account_id    The data value
102
   * @param       string $emailed_hash    The data value
103
   * @return      null|boolean            The query result
104
   */
105
  public function update_emailed_hash( $user_account_id = false, $emailed_hash = false )
106
  {
107
      $updated = false;
108 View Code Duplication
      if ($user_account_id && $emailed_hash) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user_account_id of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $emailed_hash of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
109
          // UPDATE the emailed_hash in the user_account table.
110
          $statement = $this->db->prepare("UPDATE user_account
111
              SET emailed_hash = :emailed_hash, active = 0
112
              WHERE user_account_id = :user_account_id");
113
          $statement->bindValue(":emailed_hash", $emailed_hash, PDO::PARAM_STR);
114
          $statement->bindValue(":user_account_id", $user_account_id, PDO::PARAM_INT);
115
          $statement->execute();
116
          $error = $this->db->errorInfo();
117
          if ($error[0] != "00000") {
118
              die('The UPDATE user_account emailed_hash query failed.');
119
          }
120
          $updated = true;
121
      }
122
      return $updated;
123
  }
124
125
  /**
126
   * Update Password
127
   *
128
   * Run a query to update a password.
129
   *
130
   * @param       int $user_account_password    The data value
131
   * @param       int $user_account_id          The data value
132
   * @param       string $emailed_hash          The data value
133
   * @return      null|boolean                  True/False
134
   */
135
  public function update_password( $user_account_password = false, $user_account_id = false, $emailed_hash = false )
136
  {
137
      $updated = false;
138 View Code Duplication
      if ($user_account_id && $emailed_hash) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user_account_id of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $emailed_hash of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
139
          // UPDATE the emailed_hash in the user_account table.
140
          $statement = $this->db->prepare("UPDATE user_account
141
              SET user_account_password = :user_account_password, active = 1
142
              WHERE user_account_id = :user_account_id
143
              AND emailed_hash = :emailed_hash");
144
          $statement->bindValue(":user_account_password", $user_account_password, PDO::PARAM_STR);
145
          $statement->bindValue(":user_account_id", $user_account_id, PDO::PARAM_INT);
146
          $statement->bindValue(":emailed_hash", $emailed_hash, PDO::PARAM_STR);
147
          $statement->execute();
148
          $error = $this->db->errorInfo();
149
          if ($error[0] != "00000") {
150
              die('The UPDATE user_account password query failed.');
151
          }
152
          $updated = true;
153
      }
154
      return $updated;
155
  }
156
157
}
158