TimberPostGetter::get_posts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
class TimberPostGetter {
4
5
	/**
6
	 * @param mixed $query
7
	 * @param string $PostClass
8
	 * @return array|bool|null
9
	 */
10
	static function get_post($query = false, $PostClass = 'TimberPost') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
		$posts = self::get_posts( $query, $PostClass );
12
		if ( $post = reset($posts ) ) {
13
			return $post;
14
		}
15
	}
16
17
	static function get_posts( $query = false, $PostClass = 'TimberPost', $return_collection = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
		$posts = self::query_posts( $query, $PostClass );
19
		return apply_filters('timber_post_getter_get_posts', $posts->get_posts( $return_collection ));
0 ignored issues
show
Unused Code introduced by
The call to TimberPostsCollection::get_posts() has too many arguments starting with $return_collection.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
20
	}
21
22
	static function query_post( $query = false, $PostClass = 'TimberPost' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
		$posts = self::query_posts( $query, $PostClass );
24
		if ( method_exists($posts, 'current') && $post = $posts->current() ) {
0 ignored issues
show
Bug introduced by
The method current does only exist in TimberQueryIterator, but not in TimberPostsCollection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
25
			return $post;
26
		}
27
	}
28
29
	/**
30
	 * @param mixed $query
31
	 * @param string $PostClass
32
	 * @return array|bool|null
0 ignored issues
show
Documentation introduced by
Should the return type not be TimberPostsCollection|TimberQueryIterator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
33
	 */
34
	static function query_posts($query = false, $PostClass = 'TimberPost' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
		if (self::is_post_class_or_class_map($query)) {
36
			$PostClass = $query;
37
			$query = false;
38
		}
39
40
		if (is_object($query) && !is_a($query, 'WP_Query') ){
41
			// The only object other than a query is a type of post object
42
			$query = array( $query );
43
		}
44
45
		if ( is_array( $query ) && count( $query ) && isset( $query[0] ) && is_object( $query[0] ) ) {
46
			// We have an array of post objects that already have data
47
			return new TimberPostsCollection( $query, $PostClass );
48
		} else {
49
			// We have a query (of sorts) to work with
50
			$tqi = new TimberQueryIterator( $query, $PostClass );
51
			return $tqi;
52
		}
53
	}
54
55
	static function get_pids($query){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
		$posts = self::get_posts($query);
57
		$pids = array();
58
		foreach($posts as $post){
59
			if (isset($post->ID)){
60
				$pids[] = $post->ID;
61
			}
62
		}
63
		return $pids;
64
	}
65
66
	static function loop_to_id() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
		if (!self::wp_query_has_posts()) { return false; }
68
69
		global $wp_query;
70
		$post_num = property_exists($wp_query, 'current_post')
71
				  ? $wp_query->current_post + 1
72
				  : 0
73
				  ;
74
75
		if (!isset($wp_query->posts[$post_num])) { return false; }
76
77
		return $wp_query->posts[$post_num]->ID;
78
	}
79
80
	/**
81
	 * @return bool
82
	 */
83
	static function wp_query_has_posts() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
		global $wp_query;
85
		return ($wp_query && property_exists($wp_query, 'posts') && $wp_query->posts);
86
	}
87
88
	/**
89
	 * @param string|array $arg
90
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
	 */
92
	static function is_post_class_or_class_map($arg){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
		if (is_string($arg) && class_exists($arg)) {
94
			return true;
95
		}
96
		if (is_array($arg)) {
97
			foreach ($arg as $item) {
98
				if (is_string($item) && (class_exists($item) && is_subclass_of($item, 'TimberPost'))) {
99
					return true;
100
				}
101
			}
102
		}
103
	}
104
}
105