Completed
Push — master ( fc8cf3...f6eb7d )
by Angus
08:37
created

History_Model::getCurrentChapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
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
	public function __construct() {
5
		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, 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
		$this->load->database();
8
	}
9
10
	public function updateTitleHistory(int $titleID, string $newChapter, string $newChapterTimestamp, bool $isNewTitle = FALSE) {
11
		$oldChapter = NULL;
12
		if(!$isNewTitle) {
13
			$query = $this->db->select('latest_chapter')
14
			                  ->from('tracker_titles')
15
			                  ->where('id', $titleID)
16
			                  ->get();
17
18
			$oldChapter = $query->row()->latest_chapter;
19
		}
20
21
		$success = TRUE;
22
		if($oldChapter !== $newChapter) {
23
			$success = $this->db->insert('tracker_titles_history', [
24
				'title_id' => $titleID,
25
26
				'old_chapter' => $oldChapter,
27
				'new_chapter' => $newChapter,
28
29
				'updated_at' => $newChapterTimestamp
30
			]);
31
		}
32
		return (bool) $success;
33
	}
34
35
	public function getCurrentChapter(int $titleID) : string {
36
		$query = $this->db->select('latest_chapter')
37
		                  ->from('tracker_titles')
38
		                  ->where('id', $titleID)
39
		                  ->get();
40
41
		return $query->row()->latest_chapter;
42
	}
43
}
44