Completed
Branch master (5090d0)
by Pierre-Henry
35:42
created

Page::getUrlSlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 19 and the first side effect is on line 15.

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
/**
3
 * @title            Page Class
4
 * @desc             Various Page methods with also the pagination methods.
5
 *
6
 * @author           Pierre-Henry Soria <[email protected]>
7
 * @copyright        (c) 2012-2018, Pierre-Henry Soria. All Rights Reserved.
8
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
9
 * @package          PH7 / Framework / Navigation
10
 * @version          1.2
11
 */
12
13
namespace PH7\Framework\Navigation;
14
15
defined('PH7') or exit('Restricted access');
16
17
use PH7\Framework\Mvc\Request\Http as HttpRequest;
18
19
class Page
20
{
21
    const DEFAULT_NUMBER_ITEMS = 10;
22
23
    /** @var HttpRequest */
24
    private $oHttpRequest;
25
26
    /** @var int */
27
    private $iTotalPages;
28
29
    /** @var int */
30
    private $iTotalItems;
31
32
    /** @var int */
33
    private $iNbItemsPerPage;
34
35
    /** @var int */
36
    private $iCurrentPage;
37
38
    /** @var int */
39
    private $iFirstItem;
40
41
    public function __construct()
42
    {
43
        $this->oHttpRequest = new HttpRequest;
44
    }
45
46
    /**
47
     * @param int $iTotalItems
48
     * @param int $iNbItemsPerPage
49
     *
50
     * @return void
51
     */
52
    protected function totalPages($iTotalItems, $iNbItemsPerPage)
53
    {
54
        $this->iTotalItems = (int)$iTotalItems;
55
        $this->iNbItemsPerPage = (int)$iNbItemsPerPage; // or intval() function, but it is slower than casting
56
        $this->iCurrentPage = (int)$this->oHttpRequest->getExists('p') ? $this->oHttpRequest->get('p') : 1;
0 ignored issues
show
Documentation Bug introduced by
It seems like (int) $this->oHttpReques...tpRequest->get('p') : 1 can also be of type string. However, the property $iCurrentPage is declared as type integer. 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...
57
58
        // Ternary condition to prevent division by zero
59
        $this->iTotalPages = (int)($this->iTotalItems !== 0 && $this->iNbItemsPerPage !== 0) ? ceil($this->iTotalItems / $this->iNbItemsPerPage) : 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like (int) ($this->iTotalItem...s->iNbItemsPerPage) : 0 can also be of type double. However, the property $iTotalPages is declared as type integer. 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...
60
61
        $this->iFirstItem = (int)($this->iCurrentPage - 1) * $this->iNbItemsPerPage;
62
    }
63
64
    /**
65
     * @param int $iTotalItems
66
     * @param int $iNbItemsPerPage Default 10
67
     *
68
     * @return int The number of pages.
69
     */
70
    public function getTotalPages($iTotalItems, $iNbItemsPerPage = self::DEFAULT_NUMBER_ITEMS)
71
    {
72
        $this->totalPages($iTotalItems, $iNbItemsPerPage);
73
        return ($this->iTotalPages < 1) ? 1 : $this->iTotalPages;
74
    }
75
76
    public function getTotalItems()
77
    {
78
        return $this->iTotalItems;
79
    }
80
81
    public function getFirstItem()
82
    {
83
        return $this->iFirstItem < 0 ? 0 : $this->iFirstItem;
84
    }
85
86
    public function getNbItemsPerPage()
87
    {
88
        return $this->iNbItemsPerPage;
89
    }
90
91
    public function getCurrentPage()
92
    {
93
        return $this->iCurrentPage;
94
    }
95
96
    /**
97
     * Clean a Dynamic URL for some features CMS.
98
     *
99
     * @param string $sVar The Query URL (e.g. www.pierre-henry-soria.com/my-mod/?query=value).
100
     *
101
     * @return string $sPageUrl The new clean URL.
102
     */
103
    public static function cleanDynamicUrl($sVar)
104
    {
105
        $sCurrentUrl = (new HttpRequest)->currentUrl();
106
        $sUrl = preg_replace('#\?.+$#', '', $sCurrentUrl);
107
108
        if (preg_match('#\?(.+[^\./])=(.+[^\./])$#', $sCurrentUrl)) {
109
            $sPageUrl = $sUrl . self::getUrlSlug($sCurrentUrl) . '&amp;' . $sVar . '=';
110
        } else {
111
            $sPageUrl = $sUrl . static::trailingSlash($sUrl) . '?' . $sVar . '=';
0 ignored issues
show
Bug introduced by
Since trailingSlash() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of trailingSlash() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
112
        }
113
114
        return $sPageUrl;
115
    }
116
117
    /**
118
     * Returns a trailing slash if needed.
119
     *
120
     * @param  string $sUrl
121
     *
122
     * @return string
123
     */
124
    private static function trailingSlash($sUrl)
125
    {
126
        return (substr($sUrl, -1) !== PH7_SH && !strstr($sUrl, PH7_PAGE_EXT)) ? PH7_SH : '';
127
    }
128
129
    /**
130
     * @param string $sCurrentUrl
131
     *
132
     * @return string
133
     */
134
    private static function getUrlSlug($sCurrentUrl)
135
    {
136
        return strpos($sCurrentUrl, '&amp;') !== false ? strrchr($sCurrentUrl, '?') : strrchr($sCurrentUrl, '?');
137
    }
138
}
139