Completed
Push — fm-matches ( f867e2 )
by Vladimir
16:54
created

Page   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 252
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 19
c 3
b 0
f 1
lcom 1
cbo 4
dl 18
loc 252
ccs 31
cts 31
cp 1
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A assignResult() 8 8 1
A assignLazyResult() 0 6 1
A getCreated() 0 6 1
A getStatus() 0 4 1
A isHomePage() 0 4 1
A setStatus() 0 4 1
A updateEditTimestamp() 0 4 1
A addPage() 0 11 1
A getRouteName() 0 4 1
A getDisallowedAliases() 0 8 1
A getActiveStatuses() 0 4 1
A getEagerColumns() 0 4 1
A getLazyColumns() 0 4 1
A getQueryBuilder() 10 10 1
A getHomePage() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file contains functionality relating to the custom pages that admins can great
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * A custom page
11
 * @package    BZiON\Models
12
 */
13
class Page extends AliasModel
14
{
15
    /**
16
     * The content of the page
17
     * @var string
18
     */
19
    protected $content;
20
21
    /**
22
     * The creation date of the page
23
     * @var TimeDate
24
     */
25
    protected $created;
26
27
    /**
28
     * The date the page was last updated
29
     * @var TimeDate
30
     */
31
    protected $updated;
32
33
    /**
34
     * The ID of the author of the page
35
     * @var int
36
     */
37
    protected $author;
38
39
    /**
40
     * Whether the page is the home page
41
     * @var bool
42
     */
43
    protected $home;
44
45
    /**
46
     * The status of the page
47
     * @var string
48
     */
49
    protected $status;
50
51
    /**
52
     * The name of the database table used for queries
53
     */
54
    const TABLE = "pages";
55
56
    const CREATE_PERMISSION = Permission::CREATE_PAGE;
57
    const EDIT_PERMISSION = Permission::EDIT_PAGE;
58
    const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_PAGE;
59
    const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_PAGE;
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1 View Code Duplication
    protected function assignResult($page)
0 ignored issues
show
Duplication introduced by
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...
65
    {
66 1
        $this->name = $page['name'];
67 1
        $this->alias = $page['alias'];
68 1
        $this->author = $page['author'];
69 1
        $this->home = $page['home'];
70 1
        $this->status = $page['status'];
71 1
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function assignLazyResult($page)
77
    {
78
        $this->content = $page['content'];
79
        $this->created = TimeDate::fromMysql($page['created']);
80
        $this->updated = TimeDate::fromMysql($page['updated']);
81
    }
82
83
    /**
84
     * Get the raw content of the page
85
     * @return string
86
     */
87
    public function getContent()
88
    {
89
        $this->lazyLoad();
90
91
        return $this->content;
92
    }
93
94
    /**
95
     * Get the page's submission time
96
     * @return TimeDate
97
     */
98
    public function getCreated()
99
    {
100
        $this->lazyLoad();
101
102
        return $this->created->copy();
103
    }
104
105
    /**
106
     * Get the time when the page was last updated
107
     * @return TimeDate
108
     */
109
    public function getUpdated()
110
    {
111
        $this->lazyLoad();
112
113
        return $this->updated->copy();
114
    }
115
116
    /**
117
     * Get the user who created the page
118
     * @return Player The page's author
119
     */
120
    public function getAuthor()
121
    {
122
        return Player::get($this->author);
123
    }
124
125
    /**
126
     * Get the status of the page
127
     * @return string
128
     */
129 1
    public function getStatus()
130
    {
131 1
        return $this->status;
132
    }
133
134
    /**
135
     * Find out whether this is the homepage
136
     * @return bool
137
     */
138 1
    public function isHomePage()
139
    {
140 1
        return $this->home;
141
    }
142
143
    /**
144
     * Set the content of the page
145
     *
146
     * @param  string $content
147
     * @return self
148
     */
149
    public function setContent($content)
150
    {
151
        return $this->updateProperty($this->content, "content", $content, 's');
152
    }
153
154
    /**
155
     * Set the status of the page
156
     *
157
     * @param  string $status One of "live", "revision" or "disabled"
158
     * @return self
159
     */
160
    public function setStatus($status)
161
    {
162
        return $this->updateProperty($this->status, "status", $status, 's');
163
    }
164
165
    /**
166
     * Update the last edit timestamp
167
     * @return self
168
     */
169
    public function updateEditTimestamp()
170
    {
171
        return $this->updateProperty($this->updated, "updated", TimeDate::now(), 's');
172
    }
173
174
    /**
175
     * Create a new Page
176
     *
177
     * @param string $title    The title of the page
178
     * @param string $content  The content of page
179
     * @param int    $authorID The ID of the author
180
     * @param string $status   Page status: 'live','disabled',or 'deleted'
181
     *
182
     * @return Page An object representing the page that was just created
183
     */
184 1
    public static function addPage($title, $content, $authorID, $status = "live")
185
    {
186 1
        return self::create(array(
187 1
            'name'    => $title,
188 1
            'alias'   => self::generateAlias($title),
189 1
            'content' => $content,
190 1
            'author'  => $authorID,
191 1
            'home'    => 0,
192 1
            'status'  => $status,
193 1
        ), 'sssiis', array('created', 'updated'));
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public static function getRouteName($action = 'show')
200
    {
201 1
        return "custom_page_$action";
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 1
    protected static function getDisallowedAliases()
208
    {
209
        return array(
210 1
            "admin", "bans", "index", "login", "logout", "maps", "matches",
211
            "messages", "news", "notifications", "pages", "players", "servers",
212
            "teams"
213
        );
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 1
    public static function getActiveStatuses()
220
    {
221 1
        return array('live', 'revision');
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 1
    public static function getEagerColumns()
228
    {
229 1
        return 'id,parent_id,name,alias,author,home,status';
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public static function getLazyColumns()
236
    {
237
        return 'content,created,updated';
238
    }
239
240
    /**
241
     * Get a query builder for pages
242
     * @return QueryBuilder
243
     */
244 1 View Code Duplication
    public static function getQueryBuilder()
0 ignored issues
show
Duplication introduced by
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...
245
    {
246 1
        return new QueryBuilder('Page', array(
247
            'columns' => array(
248
                'name'   => 'name',
249
                'status' => 'status'
250 1
            ),
251
            'name' => 'name'
252
        ));
253
    }
254
255
    /**
256
     * Get the home page
257
     * @deprecated
258
     * @return Page
259
     */
260
    public static function getHomePage()
261
    {
262
        return self::get(parent::fetchIdFrom(1, "home"));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchIdFrom() instead of getHomePage()). Are you sure this is correct? If so, you might want to change this to $this->fetchIdFrom().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
263
    }
264
}
265