Completed
Push — master ( 7d0d17...e8691f )
by Angus
02:48
created

History_Model::getTitleHistory()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 2
nop 2
dl 0
loc 32
ccs 0
cts 0
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class History_Model extends CI_Model {
4 94
	public function __construct() {
5 94
		parent::__construct();
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CI_Model as the method __construct() does only exist in the following sub-classes of CI_Model: Auth_Model, Batoto, DynastyScans, GameOfScanlation, History_Model, KireiCake, KissManga, MangaFox, MangaHere, MangaPanda, MangaStream, Site_Model, Sites_Model, Tracker_Model, User_Model, User_Options_Model, WebToons. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
6
7 94
		$this->load->database();
8 94
	}
9
10
	/*** TITLE HISTORY ***/
11
	public function updateTitleHistory(int $titleID, $oldChapter, string $newChapter, string $newChapterTimestamp) {
12
		$success = TRUE;
13
		if($oldChapter !== $newChapter) {
14
			$success = $this->db->insert('tracker_titles_history', [
15
				'title_id'    => $titleID,
16
17
				'old_chapter' => $oldChapter,
18
				'new_chapter' => $newChapter,
19
20
				'updated_at'  => $newChapterTimestamp
21
			]);
22
		}
23
		return (bool) $success;
24
	}
25
26
	public function getTitleHistory(int $titleID, int $page = 1) : array {
27
		$rowsPerPage = 50;
28
		$query = $this->db
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
29
			->select('SQL_CALC_FOUND_ROWS
30
			          tt.title_url,
31
			          ts.site_class,
32
			          tth.updated_at, tth.new_chapter', FALSE)
33
			->from('tracker_titles_history AS tth')
34
			->join('tracker_titles AS tt', 'tth.title_id = tt.id', 'left')
35
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
36
			->where('tt.id', $titleID)
37
			->order_by('tth.id DESC')
38
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
39
			->get();
40
41
		$arr = ['rows' => [], 'totalPages' => 1];
42
		if($query->num_rows() > 0) {
43
			foreach($query->result() as $row) {
44
				$arrRow = [];
45
46
				$arrRow['updated_at']  = $row->updated_at;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
47
48
				$newChapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->new_chapter);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49
				$arrRow['new_chapter']      = "<a href=\"{$newChapterData['url']}\">{$newChapterData['number']}</a>";
50
				$arrRow['new_chapter_full'] = $row->new_chapter;
51
52
				$arr['rows'][] = $arrRow;
53
			}
54
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
55
		}
56
		return $arr;
57
	}
58
59
	/*** USER HISTORY ***/
60
	/*
61
	 * --User history types--
62
	 * 1: Title Added
63
	 * 2: Title Updated
64
	 * 3: Title Removed
65
	 * 4: Tags updated
66
	 * 5: Category updated
67
	 * 6: Favourite added
68
	 * 7: Favourite removed
69
	 */
70
71 View Code Duplication
	public function userAddTitle(int $chapterID, string $chapter, string $category) : bool {
1 ignored issue
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...
72
		$success = $this->db->insert('tracker_user_history', [
73
			'chapter_id'  => $chapterID,
74
75
			'type'        => '1',
76
			'custom1'     => $chapter,
77
			'custom2'     => $category,
78
79
			'updated_at'  => date('Y-m-d H:i:s')
80
		]);
81
82
		return $success;
83
	}
84 View Code Duplication
	public function userUpdateTitle(int $chapterID, string $new_chapter) : bool {
1 ignored issue
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...
85
		$success = $this->db->insert('tracker_user_history', [
86
			'chapter_id'  => $chapterID,
87
88
			'type'        => '2',
89
			'custom1'     => $new_chapter,
90
91
			'updated_at'  => date('Y-m-d H:i:s')
92
		]);
93
94
		return $success;
95
	}
96
	public function userRemoveTitle(int $chapterID) : bool {
97
		$success = $this->db->insert('tracker_user_history', [
98
			'chapter_id'  => $chapterID,
99
100
			'type'        => '3',
101
102
			'updated_at'  => date('Y-m-d H:i:s')
103
		]);
104
105
		return $success;
106
	}
107 View Code Duplication
	public function userUpdateTags(int $chapterID, string $new_tags) : bool {
1 ignored issue
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...
108
		$success = $this->db->insert('tracker_user_history', [
109
			'chapter_id'  => $chapterID,
110
111
			'type'        => '4',
112
			'custom1'     => $new_tags,
113
114
			'updated_at'  => date('Y-m-d H:i:s')
115
		]);
116
117
		return $success;
118
	}
119 View Code Duplication
	public function userUpdateCategory(int $chapterID, string $new_category) : bool {
1 ignored issue
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...
120
		$success = $this->db->insert('tracker_user_history', [
121
			'chapter_id'  => $chapterID,
122
123
			'type'        => '5',
124
			'custom1'     => $new_category,
125
126
			'updated_at'  => date('Y-m-d H:i:s')
127
		]);
128
129
		return $success;
130
	}
131 View Code Duplication
	public function userAddFavourite(int $chapterID, string $chapter) : bool {
1 ignored issue
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...
132
		$success = $this->db->insert('tracker_user_history', [
133
			'chapter_id'  => $chapterID,
134
135
			'type'        => '6',
136
			'custom1'     => $chapter,
137
138
			'updated_at'  => date('Y-m-d H:i:s')
139
		]);
140
141
		return $success;
142
	}
143 View Code Duplication
	public function userRemoveFavourite(int $chapterID, string $chapter) : bool {
1 ignored issue
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...
144
		$success = $this->db->insert('tracker_user_history', [
145
			'chapter_id'  => $chapterID,
146
147
			'type'        => '7',
148
			'custom1'     => $chapter,
149
150
			'updated_at'  => date('Y-m-d H:i:s')
151
		]);
152
153
		return $success;
154
	}
155
156
	public function userGetHistory(int $page) : array {
157
		$rowsPerPage = 50;
158
		$query = $this->db
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
159
			->select('SQL_CALC_FOUND_ROWS
160
			          tt.title, tt.title_url,
161
			          ts.site, ts.site_class,
162
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
163
			->from('tracker_user_history AS tuh')
164
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
165
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
166
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
167
			->where('tc.user_id', $this->User->id)
168
			->order_by('tuh.id DESC')
169
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
170
			->get();
171
172
		$arr = ['rows' => [], 'totalPages' => 1];
173
		if($query->num_rows() > 0) {
174
			foreach($query->result() as $row) {
175
				$arrRow = [];
176
177
				$arrRow['updated_at'] = $row->updated_at;
178
				$arrRow['title']      = $row->title;
179
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
180
181
				$arrRow['site'] = $row->site;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
182
				$arrRow['site_sprite'] = str_replace('.', '-', $row->site);
183
184
				switch($row->type) {
185 View Code Duplication
					case 1:
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
186
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
187
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
188
						break;
189
190 View Code Duplication
					case 2:
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
191
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
192
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
193
						break;
194
195
					case 3:
196
						$arrRow['status'] = "Series removed";
197
						break;
198
199
					case 4:
200
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
201
						break;
202
203
					case 5:
204
						$arrRow['status'] = "Category set to '{$row->custom1}'";
205
						break;
206
207 View Code Duplication
					case 6:
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
208
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
209
						$arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
210
						break;
211
212 View Code Duplication
					case 7:
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
213
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
214
						$arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
215
						break;
216
217
					default:
218
						$arrRow['status'] = "Something went wrong!";
219
						break;
220
				}
221
				$arr['rows'][] = $arrRow;
222
			}
223
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
224
		}
225
		return $arr;
226
	}
227
}
228