Completed
Push — master ( 9553e2...91bcf6 )
by Angus
09:37
created

History_Model::userGetHistory()   C

Complexity

Conditions 10
Paths 2

Size

Total Lines 71
Code Lines 54

Duplication

Lines 16
Ratio 22.54 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 54
nc 2
nop 1
dl 16
loc 71
ccs 0
cts 0
cp 0
crap 110
rs 5.9513
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	/*** USER HISTORY ***/
27
	/*
28
	 * --User history types--
29
	 * 1: Title Added
30
	 * 2: Title Updated
31
	 * 3: Title Removed
32
	 * 4: Tags updated
33
	 * 5: Category updated
34
	 * 6: Favourite added
35
	 * 7: Favourite removed
36
	 */
37
38 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...
39
		$success = $this->db->insert('tracker_user_history', [
40
			'chapter_id'  => $chapterID,
41
42
			'type'        => '1',
43
			'custom1'     => $chapter,
44
			'custom2'     => $category,
45
46
			'updated_at'  => date('Y-m-d H:i:s')
47
		]);
48
49
		return $success;
50
	}
51 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...
52
		$success = $this->db->insert('tracker_user_history', [
53
			'chapter_id'  => $chapterID,
54
55
			'type'        => '2',
56
			'custom1'     => $new_chapter,
57
58
			'updated_at'  => date('Y-m-d H:i:s')
59
		]);
60
61
		return $success;
62
	}
63
	public function userRemoveTitle(int $chapterID) : bool {
64
		$success = $this->db->insert('tracker_user_history', [
65
			'chapter_id'  => $chapterID,
66
67
			'type'        => '3',
68
69
			'updated_at'  => date('Y-m-d H:i:s')
70
		]);
71
72
		return $success;
73
	}
74 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...
75
		$success = $this->db->insert('tracker_user_history', [
76
			'chapter_id'  => $chapterID,
77
78
			'type'        => '4',
79
			'custom1'     => $new_tags,
80
81
			'updated_at'  => date('Y-m-d H:i:s')
82
		]);
83
84
		return $success;
85
	}
86 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...
87
		$success = $this->db->insert('tracker_user_history', [
88
			'chapter_id'  => $chapterID,
89
90
			'type'        => '5',
91
			'custom1'     => $new_category,
92
93
			'updated_at'  => date('Y-m-d H:i:s')
94
		]);
95
96
		return $success;
97
	}
98 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...
99
		$success = $this->db->insert('tracker_user_history', [
100
			'chapter_id'  => $chapterID,
101
102
			'type'        => '6',
103
			'custom1'     => $chapter,
104
105
			'updated_at'  => date('Y-m-d H:i:s')
106
		]);
107
108
		return $success;
109
	}
110 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...
111
		$success = $this->db->insert('tracker_user_history', [
112
			'chapter_id'  => $chapterID,
113
114
			'type'        => '7',
115
			'custom1'     => $chapter,
116
117
			'updated_at'  => date('Y-m-d H:i:s')
118
		]);
119
120
		return $success;
121
	}
122
123
	public function userGetHistory(int $page) : array {
124
		$rowsPerPage = 50;
125
		$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...
126
			->select('SQL_CALC_FOUND_ROWS
127
			          tt.title, tt.title_url,
128
			          ts.site, ts.site_class,
129
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
130
			->from('tracker_user_history AS tuh')
131
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
132
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
133
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
134
			->where('tc.user_id', $this->User->id)
135
			->order_by('tuh.id DESC')
136
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
137
			->get();
138
139
		$arr = ['rows' => [], 'totalCount' => 0];
140
		if($query->num_rows() > 0) {
141
			foreach($query->result() as $row) {
142
				$arrRow = [];
143
144
				$arrRow['updated_at'] = $row->updated_at;
145
				$arrRow['title']      = $row->title;
146
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
147
148
				$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...
149
				$arrRow['site_sprite'] = str_replace('.', '-', $row->site);
150
151
				switch($row->type) {
152 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...
153
						$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...
154
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
155
						break;
156
157 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...
158
						$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...
159
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
160
						break;
161
162
					case 3:
163
						$arrRow['status'] = "Series removed";
164
						break;
165
166
					case 4:
167
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
168
						break;
169
170
					case 5:
171
						$arrRow['status'] = "Category set to '{$row->custom1}'";
172
						break;
173
174 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...
175
						$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...
176
						$arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
177
						break;
178
179 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...
180
						$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...
181
						$arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
182
						break;
183
184
					default:
185
						$arrRow['status'] = "Something went wrong!";
186
						break;
187
				}
188
				$arr['rows'][] = $arrRow;
189
			}
190
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
191
		}
192
		return $arr;
193
	}
194
}
195