Completed
Push — master ( d4ecfc...438ba7 )
by Peter
05:36
created

NavigateRange::buildOffset()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
ccs 19
cts 19
cp 1
rs 6.7272
cc 7
eloc 15
nc 9
nop 0
crap 7
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace AnimeDb\Bundle\PaginationBundle\Service;
12
13
/**
14
 * NavigateRange
15
 * @package AnimeDb\Bundle\PaginationBundle\Service
16
 */
17
class NavigateRange
18
{
19
    /**
20
     * @var Configuration
21
     */
22
    protected $config;
23
24
    /**
25
     * @var int
26
     */
27
    protected $left_offset = -1;
28
29
    /**
30
     * @var int
31
     */
32
    protected $right_offset = -1;
33
34
    /**
35
     * @param Configuration $config
36
     */
37 11
    public function __construct(Configuration $config) {
38 11
        $this->config = $config;
39 11
    }
40
41
    /**
42
     * @return int
43
     */
44 10
    public function getLeftOffset()
45
    {
46 10
        return $this->buildOffset()->left_offset;
47
    }
48
49
    /**
50
     * @return int
51
     */
52 10
    public function getRightOffset()
53
    {
54 10
        return $this->buildOffset()->right_offset;
55
    }
56
57
    /**
58
     * @return NavigateRange
59
     */
60 10
    protected function buildOffset()
61
    {
62 10
        if (($this->left_offset < 0 || $this->right_offset < 0) && $this->config->getTotalPages() > 1) {
63
            // definition of offset to the left and to the right of the selected page
64 9
            $this->left_offset = (int)floor(($this->config->getMaxNavigate() - 1) / 2);
65 9
            $this->right_offset = (int)ceil(($this->config->getMaxNavigate() - 1) / 2);
66
67
            // adjustment, if the offset is too large left
68 9
            if ($this->config->getCurrentPage() - $this->left_offset < 1) {
69 4
                $offset = abs($this->config->getCurrentPage() - 1 - $this->left_offset);
70 4
                $this->left_offset = $this->left_offset - $offset;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->left_offset - $offset can also be of type double. However, the property $left_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...
71 4
                $this->right_offset = $this->right_offset + $offset;
0 ignored issues
show
Documentation Bug introduced by
The property $right_offset was declared of type integer, but $this->right_offset + $offset 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...
72 4
            }
73
74
            // adjustment, if the offset is too large right
75 9
            if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) {
76 4
                $offset = abs($this->config->getTotalPages() - $this->config->getCurrentPage() - $this->right_offset);
77 4
                $this->left_offset = $this->left_offset + $offset;
0 ignored issues
show
Documentation Bug introduced by
The property $left_offset was declared of type integer, but $this->left_offset + $offset 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...
78 4
                $this->right_offset = $this->right_offset - $offset;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->right_offset - $offset can also be of type double. However, the property $right_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...
79 4
            }
80
81
            // left offset should point not lower of the first page
82 9
            if ($this->left_offset >= $this->config->getCurrentPage()) {
83 2
                $this->left_offset = $this->config->getCurrentPage() - 1;
84 2
            }
85 9
        }
86
87 10
        return $this;
88
    }
89
}
90