PostQuery::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Timber;
4
5
use Timber\Helper;
6
use Timber\Post;
7
use Timber\PostGetter;
8
9
/**
10
 * A PostQuery allows a user to query for a Collection of WordPress Posts.
11
 * PostCollections are used directly in Twig templates to iterate through and retrieve
12
 * meta information about the collection of posts
13
 * @api
14
 * @package Timber
15
 */
16
class PostQuery extends PostCollection {
17
	/**
18
	 * Found posts.
19
	 *
20
	 * The total amount of posts found for this query. Will be `0` if you used `no_found_rows` as a
21
	 * query parameter. Will be `null` if you passed in an existing collection of posts.
22
	 *
23
	 * @api
24
	 * @since 1.11.1
25
	 * @var int The amount of posts found in the query.
26
	 */
27
	public $found_posts = null;
28
29
	protected $userQuery;
30
	protected $queryIterator;
31
	protected $pagination = null;
32
33
	/**
34
	 * @param mixed   	$query
35
	 * @param string 	$post_class
36
	 */
37
	public function __construct( $query = false, $post_class = '\Timber\Post' ) {
38
		$this->userQuery = $query;
39
		$this->queryIterator = PostGetter::query_posts($query, $post_class);
40
41
		if ( $this->queryIterator instanceof QueryIterator ) {
42
			$this->found_posts = $this->queryIterator->found_posts();
43
		}
44
45
		$posts = $this->queryIterator->get_posts();
46
47
		parent::__construct($posts, $post_class);
48
	}
49
50
	/**
51
	 * @return mixed the query the user orignally passed
52
	 * to the pagination object
53
	 */
54
	protected function get_query() {
55
		return $this->userQuery;
56
	}
57
58
	/**
59
	 * Set pagination for the collection. Optionally could be used to get pagination with custom preferences.
60
	 *
61
	 * @param 	array $prefs
62
	 * @return 	Timber\Pagination object
0 ignored issues
show
Bug introduced by
The type Timber\Timber\Pagination was not found. Did you mean Timber\Pagination? If so, make sure to prefix the type with \.
Loading history...
63
	 */
64
	public function pagination( $prefs = array() ) {
65
		if ( !$this->pagination && is_a($this->queryIterator, 'Timber\QueryIterator') ) {
66
			$this->pagination = $this->queryIterator->get_pagination($prefs, $this->get_query());
0 ignored issues
show
Unused Code introduced by
The call to Timber\QueryIterator::get_pagination() has too many arguments starting with $this->get_query(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
			/** @scrutinizer ignore-call */ 
67
   $this->pagination = $this->queryIterator->get_pagination($prefs, $this->get_query());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
		}
68
		return $this->pagination;
69
	}
70
71
}
72