Passed
Branch trunk (3f392c)
by SuperNova.WS
04:59
created

DbSqlPaging   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 115
ccs 0
cts 46
cp 0
rs 10
c 1
b 0
f 0
wmc 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 2 2
A count() 0 2 2
A seekToFirst() 0 3 2
A __construct() 0 8 2
A getPageZeroRecordNum() 0 2 1
A getTotalPages() 0 2 2
A getCurrentPageNumber() 0 2 1
A query() 0 20 4
A getRecordsOnCurrentPage() 0 2 1
A getTotalRecords() 0 2 2
1
<?php
2
/**
3
 * Created by Gorlum 25.11.2017 18:49
4
 */
5
6
namespace DBAL;
7
8
use mysqli_result;
9
10
/**
11
 * Class DbSqlPaging
12
 *
13
 * Another implementation for DbAbstractResultIterator
14
 *
15
 * Fetches data from string SQL-query and pages through it
16
 *
17
 * @package DBAL
18
 */
19
class DbSqlPaging extends DbAbstractResultIterator {
20
  protected $db = null;
21
  protected $sqlQuery;
22
  protected $pageSize = PAGING_PAGE_SIZE_DEFAULT;
23
  protected $currentPage = 1;
24
25
  /**
26
   * DbSqlPaging constructor.
27
   *
28
   * @param string         $sqlQuery
29
   * @param int            $pageSize
30
   * @param int            $currentPage
31
   * @param \db_mysql|null $db
32
   */
33
  public function __construct($sqlQuery, $pageSize = PAGING_PAGE_SIZE_DEFAULT, $currentPage = 1, $db = null) {
34
    $this->sqlQuery = $sqlQuery;
35
    $this->pageSize = max(intval($pageSize), PAGING_PAGE_SIZE_MINIMUM);
36
    $this->currentPage = max(intval($currentPage), 1);
37
38
    $this->db = isset($db) ? $db : \classSupernova::$gc->db;
39
40
    $this->query();
41
  }
42
43
  /**
44
   * @inheritdoc
45
   */
46
  public function valid() {
47
    return parent::valid() && $this->counter < $this->getRecordsOnCurrentPage();
48
  }
49
50
  /**
51
   * @inheritdoc
52
   */
53
  public function count() {
54
    return $this->mysqli_result instanceof mysqli_result ? $this->getRecordsOnCurrentPage() : 0;
55
  }
56
57
  /**
58
   * @inheritdoc
59
   */
60
  protected function seekToFirst() {
61
    if ($this->mysqli_result instanceof mysqli_result) {
62
      $this->mysqli_result->data_seek($this->getPageZeroRecordNum());
63
    }
64
  }
65
66
  /**
67
   * Get current page number
68
   *
69
   * @return int
70
   */
71
  public function getCurrentPageNumber() {
72
    return $this->currentPage;
73
  }
74
75
  /**
76
   * Get total number of records in query
77
   *
78
   * @return int
79
   */
80
  public function getTotalRecords() {
81
    return $this->mysqli_result instanceof mysqli_result ? $this->mysqli_result->num_rows : 0;
82
  }
83
84
  /**
85
   * Calculates total page numbers
86
   *
87
   * @return float|int
88
   */
89
  public function getTotalPages() {
90
    return $this->mysqli_result instanceof mysqli_result ? ceil($this->getTotalRecords() / $this->pageSize) : 0;
91
  }
92
93
  /**
94
   * Calculates record count on current page
95
   *
96
   * @return int
97
   */
98
  public function getRecordsOnCurrentPage() {
99
    return min($this->pageSize, $this->getTotalRecords() - $this->getPageZeroRecordNum());
100
  }
101
102
  /**
103
   * Calculates record index from which current page starts
104
   *
105
   * @return int
106
   */
107
  protected function getPageZeroRecordNum() {
108
    return ($this->currentPage - 1) * $this->pageSize;
109
  }
110
111
  /**
112
   * Queries DB for data and makes all necessary preparations
113
   */
114
  protected function query() {
115
    if ($this->mysqli_result !== null) {
116
      unset($this->mysqli_result);
117
    }
118
    // Performing query
119
    $this->mysqli_result = $this->db->doquery($this->sqlQuery);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->doquery($this->sqlQuery) can also be of type array. However, the property $mysqli_result is declared as type false|mysqli_result. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
120
121
    // Checking if page number within allowed ranges
122
    if (
123
      // Is it not a first page?
124
      $this->currentPage != 1
125
      &&
126
      // Is current page beyond limits?
127
      $this->currentPage > $this->getTotalPages()
128
    ) {
129
      // Correcting page number to first
130
      $this->currentPage = max(1, $this->getTotalPages());
131
    }
132
133
    $this->rewind();
134
  }
135
136
}
137