Issues (45)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Utils/Pagination.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Utils;
11
12
use Slick\Common\Base;
13
use Slick\Filter\StaticFilter;
14
use Slick\Http\PhpEnvironment\Request;
15
use Slick\Validator\StaticValidator;
16
17
/**
18
 * Pagination utility
19
 *
20
 * @package Slick\Mvc\Utils
21
 * @author  Filipe Silva <[email protected]>
22
 *
23
 * @property Request $request
24
 * @property integer $offset
25
 * @property integer $rowsPerPage
26
 * @property integer $pages
27
 * @property integer $total
28
 * @property integer $current
29
 */
30
class Pagination extends Base
31
{
32
33
    /**
34
     * @readwrite
35
     * @var int Total pages
36
     */
37
    protected $pages = 0;
38
    /**
39
     * @readwrite
40
     * @var int Total records
41
     */
42
    protected $total;
43
    /**
44
     * @readwrite
45
     * @var int current page index
46
     */
47
    protected $current = 1;
48
    /**
49
     * @readwrite
50
     * @var int total rows per page
51
     */
52
    protected $rowsPerPage = 12;
53
    /**
54
     * @readwrite
55
     * @var int First row to return
56
     */
57
    protected $offset = 0;
58
    /**
59
     * @readwrite
60
     * @var Request
61
     */
62
    protected $request;
63
64
    /**
65
     * Overrides the constructor to calculate the properties for current
66
     * pagination state.
67
     *
68
     * @param array $options
69
     */
70 18
    public function __construct($options = array())
71
    {
72 18
        parent::__construct($options);
73 18
        if ($this->getRequest()->getQuery('rows')) {
74 10
            $this->setRowsPerPage($this->getRequest()->getQuery('rows'));
75 5
        }
76 18
        if ($this->getRequest()->getQuery('page')) {
77 10
            $this->setCurrent($this->getRequest()->getQuery('page'));
78 5
        }
79 18
    }
80
81
    /**
82
     * Lazy loads request object
83
     *
84
     * @return Request
85
     */
86 18
    public function getRequest()
87
    {
88 18
        if (is_null($this->request)) {
89 6
            $this->request = new Request();
90 3
        }
91 18
        return $this->request;
92
    }
93
    
94
    /**
95
     * Sets current page and calculates offset
96
     *
97
     * @param integer $value The number of the current page.
98
     *
99
     * @return Pagination A self instance for method chain calls
100
     */
101 10 View Code Duplication
    public function setCurrent($value)
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...
102
    {
103 10
        if (StaticValidator::validates('number', $value)) {
104 10
            $this->current = StaticFilter::filter('number',$value);
105 10
            $this->offset = $this->rowsPerPage * ($this->current - 1);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->rowsPerPage * ($this->current - 1) can also be of type double. However, the property $offset 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...
106 5
        }
107 10
        return $this;
108
    }
109
    
110
    /**
111
     * Sets the total rows to paginate.
112
     *
113
     * @param integer $value The rows total to set.
114
     *
115
     * @return Pagination A self instance for method chain calls
116
     */
117 4 View Code Duplication
    public function setTotal($value)
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...
118
    {
119 4
        if (StaticValidator::validates('number', $value)) {
120 4
            $this->total = StaticFilter::filter('number',$value);
121 4
            $this->pages = ceil($this->total / $this->rowsPerPage);
0 ignored issues
show
Documentation Bug introduced by
The property $pages was declared of type integer, but ceil($this->total / $this->rowsPerPage) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
122 2
        }
123 4
        return $this;
124
    }
125
    
126
    /**
127
     * Sets the total rows per page.
128
     *
129
     * @param integer $value The total rows per page to set.
130
     *
131
     * @return Pagination A self instance for method chain calls
132
     */
133 14 View Code Duplication
    public function setRowsPerPage($value)
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...
134
    {
135 14
        if (StaticValidator::validates('number', $value)) {
136 14
            $this->rowsPerPage = StaticFilter::filter('number',$value);
137 14
            $this->pages = ceil($this->total / $this->rowsPerPage);
0 ignored issues
show
Documentation Bug introduced by
The property $pages was declared of type integer, but ceil($this->total / $this->rowsPerPage) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
138 7
        }
139 14
        return $this;
140
    }
141
    
142
    /**
143
     * Creates a request query for the provided page.
144
     *
145
     * This method check the current request query in order to maintain the
146
     * other parameters unchanged and sets the 'page' parameter to the
147
     * provided page number.
148
     *
149
     * @param integer $page The page number to build the query on.
150
     *
151
     * @return string The query string to use in the pagination links.
152
     */
153 2
    public function pageUrl($page)
154
    {
155 2
        $params = $this->getRequest()->getQuery();
156 2
        if (isset($params['url']))
157 1
            unset($params['url']);
158 2
        $params['page'] = $page;
159 2
        return '?' . http_build_query($params);
160
    }
161
}