GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — bugfix/5.4build ( 31a631 )
by Szurovecz
08:38
created

PageImpl::equals()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 6
eloc 8
nc 6
nop 1
1
<?php
2
/*
3
 * Copyright (c) 2013 Janos Szurovecz
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6
 * this software and associated documentation files (the "Software"), to deal in
7
 * the Software without restriction, including without limitation the rights to
8
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is furnished to do
10
 * so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in all
13
 * copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
22
 */
23
24
namespace predaddy\presentation;
25
26
use ArrayIterator;
27
use IteratorAggregate;
28
use precore\lang\Object;
29
use precore\lang\ObjectInterface;
30
use precore\util\Objects;
31
32
/**
33
 * Default Page implementation.
34
 *
35
 * @author Janos Szurovecz <[email protected]>
36
 */
37
class PageImpl extends Object implements IteratorAggregate, Page
38
{
39
    private $content = [];
40
41
    /**
42
     * @var Pageable
43
     */
44
    private $pageable;
45
46
    /**
47
     * @var int
48
     */
49
    private $total;
50
51
    /**
52
     * @param array $content
53
     * @param Pageable $pageable
54
     * @param int $total Total number of all elements (not just the current page's)
55
     */
56
    public function __construct(array $content, Pageable $pageable = null, $total = null)
57
    {
58
        $this->content = array_merge($this->content, $content);
59
        $this->total = $total === null ? count($content) : $total;
60
        $this->pageable = $pageable;
61
    }
62
63
    /**
64
     * @return array of the records in the current page
65
     */
66
    public function getContent()
67
    {
68
        return $this->content;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->content; (array) is incompatible with the return type declared by the interface predaddy\presentation\Page::getContent of type Traversable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
69
    }
70
71
    /**
72
     * @return int The page number
73
     */
74
    public function getNumber()
75
    {
76
        return $this->pageable === null
77
            ? 0
78
            : $this->pageable->getPageNumber();
79
    }
80
81
    /**
82
     * @return int size of the content
83
     */
84
    public function getSize()
85
    {
86
        return $this->pageable === null
87
            ? 0
88
            : $this->pageable->getPageSize();
89
    }
90
91
    public function getSort()
92
    {
93
        return $this->pageable === null
94
            ? null
95
            : $this->pageable->getSort();
96
    }
97
98
    public function getTotalElements()
99
    {
100
        return $this->total;
101
    }
102
103
    public function getTotalPages()
104
    {
105
        return $this->getSize() == 0
106
            ? 1
107
            : ceil($this->total / $this->getSize());
108
    }
109
110
    public function hasContent()
111
    {
112
        return count($this->content) !== 0;
113
    }
114
115
    public function hasNextPage()
116
    {
117
        return $this->getNumber() + 1 < $this->getTotalPages();
118
    }
119
120
    public function hasPreviousPage()
121
    {
122
        return 0 < $this->getNumber();
123
    }
124
125
    public function isFirstPage()
126
    {
127
        return !$this->hasPreviousPage();
128
    }
129
130
    public function isLastPage()
131
    {
132
        return !$this->hasNextPage();
133
    }
134
135
    /**
136
     * Create a Pageable object to be able to obtain the next page.
137
     *
138
     * @return Pageable
139
     */
140
    public function nextPageable()
141
    {
142
        return $this->hasNextPage()
143
            ? $this->pageable->next()
144
            : null;
145
    }
146
147
    /**
148
     * Create a Pageable object to be able to obtain the previous page.
149
     *
150
     * @return Pageable
151
     */
152
    public function previousPageable()
153
    {
154
        return $this->hasPreviousPage()
155
            ? $this->pageable->previousOrFirst()
156
            : null;
157
    }
158
159
    /**
160
     * @return ArrayIterator|\Traversable
161
     */
162
    public function getIterator()
163
    {
164
        return new ArrayIterator($this->content);
165
    }
166
167
    public function equals(ObjectInterface $object = null)
168
    {
169
        if ($object === $this) {
170
            return true;
171
        }
172
        /* @var $object PageImpl */
173
        return $object !== null
174
            && $this->getClassName() === $object->getClassName()
175
            && Objects::equal($this->total, $object->total)
176
            && Objects::equal($this->content, $object->content)
177
            && Objects::equal($this->pageable, $object->pageable);
178
    }
179
180
    public function toString()
181
    {
182
        return Objects::toStringHelper($this)
183
            ->add('total', $this->total)
184
            ->add('pageable', $this->pageable)
185
            ->toString();
186
    }
187
}
188