Completed
Pull Request — develop (#1350)
by Naveen
03:06
created

Videos_Data_Source::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Videoobject\Background_Process;
4
5
use Wordlift\Common\Background_Process\Data_Source;
6
7
class Videos_Data_Source extends Data_Source {
8
9
	/**
10
	 * @var \Wordlift_Log_Service
11
	 */
12
	private $log;
13
14
	public function __construct( $state_storage_key ) {
15
		parent::__construct( $state_storage_key );
16
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
17
	}
18
19
	public function next() {
20
		$this->log->debug("Received video data source index as " . $this->get_state()->index);
21
		$this->log->debug("Count set to " . $this->get_batch_size() );
22
		return get_posts( array(
23
			'fields'      => 'ids',
24
			'post_status' => 'any',
25
			'numberposts' => $this->get_batch_size(),
26
			'offset'      => $this->get_state()->index,
27
		) );
28
	}
29
30
	public function count() {
31
		return count( get_posts( array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return count(get_posts(a...st_status' => 'any'))); (integer) is incompatible with the return type declared by the abstract method Wordlift\Common\Backgrou...cess\Data_Source::count of type integer[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
32
			'fields'      => 'ids',
33
			'numberposts' => - 1,
34
			'post_status' => 'any',
35
		) ) );
36
37
	}
38
39
	public function get_batch_size() {
40
		// For now use only 5 in order to prevent exceeding api limit.
41
		return 5;
42
	}
43
44
}