Completed
Push — master ( c044d5...f8791f )
by Angus
03:19
created

History_Model::userGetHistory()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 57
Code Lines 43

Duplication

Lines 8
Ratio 14.04 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 43
nc 2
nop 1
dl 8
loc 57
ccs 0
cts 0
cp 0
crap 72
rs 7.2648
c 0
b 0
f 0

How to fix   Long Method   

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 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...
28
		$success = $this->db->insert('tracker_user_history', [
29
			'chapter_id'  => $chapterID,
30
31
			'type'        => '1',
32
			'custom1'     => $chapter,
33
			'custom2'     => $category,
34
35
			'updated_at'  => date('Y-m-d H:i:s')
36
		]);
37
38
		return $success;
39
	}
40 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...
41
		$success = $this->db->insert('tracker_user_history', [
42
			'chapter_id'  => $chapterID,
43
44
			'type'        => '2',
45
			'custom1'     => $new_chapter,
46
47
			'updated_at'  => date('Y-m-d H:i:s')
48
		]);
49
50
		return $success;
51
	}
52
	public function userRemoveTitle(int $chapterID) : bool {
53
		$success = $this->db->insert('tracker_user_history', [
54
			'chapter_id'  => $chapterID,
55
56
			'type'        => '3',
57
58
			'updated_at'  => date('Y-m-d H:i:s')
59
		]);
60
61
		return $success;
62
	}
63 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...
64
		$success = $this->db->insert('tracker_user_history', [
65
			'chapter_id'  => $chapterID,
66
67
			'type'        => '4',
68
			'custom1'     => $new_tags,
69
70
			'updated_at'  => date('Y-m-d H:i:s')
71
		]);
72
73
		return $success;
74
	}
75 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...
76
		$success = $this->db->insert('tracker_user_history', [
77
			'chapter_id'  => $chapterID,
78
79
			'type'        => '5',
80
			'custom1'     => $new_category,
81
82
			'updated_at'  => date('Y-m-d H:i:s')
83
		]);
84
85
		return $success;
86
	}
87
88
	public function userGetHistory(int $page) : array {
89
		$rowsPerPage = 50;
90
		$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...
91
			->select('SQL_CALC_FOUND_ROWS
92
			          tt.title, tt.title_url,
93
			          ts.site, ts.site_class,
94
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
95
			->from('tracker_user_history AS tuh')
96
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
97
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
98
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
99
			->where('tc.user_id', $this->User->id)
100
			->order_by('tuh.id DESC')
101
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
102
			->get();
103
104
		$arr = ['rows' => [], 'totalCount' => 0];
105
		if($query->num_rows() > 0) {
106
			foreach($query->result() as $row) {
107
				$arrRow = [];
108
109
				$arrRow['updated_at'] = $row->updated_at;
110
				$arrRow['title']      = $row->title;
111
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
112
113
				$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...
114
				$arrRow['site_sprite'] = str_replace('.', '-', $row->site);
115
116
				switch($row->type) {
117 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...
118
						$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...
119
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
120
						break;
121
122 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...
123
						$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...
124
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
125
						break;
126
127
					case 3:
128
						$arrRow['status'] = "Series removed";
129
						break;
130
131
					case 4:
132
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
133
						break;
134
135
					case 5:
136
						$arrRow['status'] = "Category set to '{$row->custom1}'";
137
						break;
138
				}
139
				$arr['rows'][] = $arrRow;
140
			}
141
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
142
		}
143
		return $arr;
144
	}
145
}
146