Completed
Push — master ( ab4944...534e9c )
by Andrew
02:30
created

Classy::get_pagination()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
cc 7
eloc 35
c 2
b 1
f 2
nc 48
nop 1
dl 0
loc 56
rs 7.7489

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 
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 322.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * The core theme class.
5
 *
6
 *
7
 * @since      1.0.0
8
 * @package    Classy
9
 * @author     Andrew Tolochka <[email protected]>
10
 */
11
class Classy {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
13
	/**
14
	 * Singleton instance of plugin
15
	 *
16
	 * @var Classy
17
	 * @since  0.1.0
18
	 */
19
	protected static $single_instance = null;
20
21
	/**
22
	 * Creates or returns an instance of this class.
23
	 *
24
	 * @since  0.1.0
25
	 * @return Classy A single instance of this class.
26
	 */
27
	public static function get_instance() {
28
		if ( null === self::$single_instance ) {
29
			self::$single_instance = new self();
30
		}
31
32
		return self::$single_instance;
33
	}
34
35
	/**
36
	 * Define the core functionality of the them.
37
	 *
38
	 * Set the theme name and the theme version that can be used throughout the theme.
39
	 *
40
	 * @since    1.0.0
41
	 */
42
	protected function __construct() {
43
		$this->define_constants();
44
45
		$this->include_core_files();
46
47
		$this->include_models();
48
49
		$this->init_config();
50
	}
51
52
	/**
53
	 * Function to define constants
54
	 * 
55
	 * @param  string
56
	 * @param  string
57
	 */
58
	private function define( $name, $value ) {
59
		if ( !defined($name) ) {
60
			
61
			define( $name, $value );
62
63
		}
64
	}
65
66
	/**
67
	 * Defines plugin constants
68
	 * 
69
	 * @since    1.0.0
70
	 * @access   private
71
	 */
72
	private function define_constants() {
73
		$theme = wp_get_theme();
74
75
		$this->define( 'THEME', $theme->template );
76
		$this->define( 'THEME_NAME', $theme->get('Name') );
77
		$this->define( 'THEME_PATH', get_template_directory() . '/' );
78
		$this->define( 'THEME_DIR', get_template_directory_uri() . '/' );
79
		$this->define( 'THEME_VERSION', $theme->get('Version') );
80
		$this->define( 'THEME_FRAMEWORK_PATH', THEME_PATH . 'app/' );
81
		$this->define( 'THEME_FRAMEWORK_DIR', THEME_DIR . 'app/' );
82
	}
83
84
	/**
85
	 * Include core files that are responsible for theme render
86
	 */
87
	private function include_core_files() {
88
		require_once THEME_PATH . 'vendor/autoload.php';
89
90
		// Basis Class
91
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-basis.php';
92
93
		// Theme Config
94
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-config.php';
95
	
96
		// Scope
97
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-scope.php';
98
99
		// Template Loader
100
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-template.php';
101
102
		// Helper functions
103
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-helper.php';
104
105
		// Query Helper
106
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-query-helper.php';
107
108
		// Menu
109
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-menu.php';
110
		require_once THEME_FRAMEWORK_PATH . 'classy/classy-menu-item.php';
111
112
		// Appearance
113
		require_once THEME_FRAMEWORK_PATH . 'appearance.php';
114
	}
115
116
	/**
117
	 * Include theme Object-Orienter models
118
	 */
119
	private function include_models() {
120
		$files = (array) glob( THEME_FRAMEWORK_PATH . '/models/*.php' );
121
122
		foreach ( $files as $filename ) {
123
124
			if ( !empty($filename) ) {
125
126
				require_once $filename;
127
128
			}
129
130
		}
131
	}
132
133
	/**
134
	 * Init Theme Configuration
135
	 */
136
	private function init_config() {
137
		$this->config = ClassyConfig::init();
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
Are you sure the assignment to $this->config is correct as \ClassyConfig::init() (which targets ClassyConfig::init()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
138
	}
139
140
	/**
141
	 * Returns theme config variable
142
	 * 
143
	 * @param  string $name
144
	 * @return any
145
	 */
146
	public static function get_config_var($name) {
147
		$vars = ClassyConfig::get_vars();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $vars is correct as \ClassyConfig::get_vars() (which targets ClassyConfig::get_vars()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
148
149
		if (isset($vars[$name])) return $vars[$name];
150
151
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Classy::get_config_var of type any.

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...
152
	}
153
154
	/**
155
	 * Returns theme textdomain
156
	 * 
157
	 * @return string
158
	 */
159
	public static function textdomain() {
160
		$textdomain = Classy::get_config_var('textdomain');
161
162
		return $textdomain ? $textdomain : THEME;
163
	}
164
165
	/**
166
	 * Performs template render. 
167
	 * If there is $template attribute presented, it will render requested template. 
168
	 * If it's not it will try to find necessary template based on $wp_query
169
	 * 
170
	 * @param  string|null $template template path in blade format, ex: single, base.default, single.partials.slider and etc
171
	 * @param  array|null  $data     Additional params
172
	 * @return void                
173
	 */
174
	public static function render($template = null, $data = null) {
175
		ClassyTemplate::render($template, $data);
176
	}
177
178
	/**
179
	 * Alias for ClassyHelper::get_archives_title()
180
	 * Returns page title for archive page. 
181
	 * Example: Archives, Author: John Doe, Tag: Lorem Ipsum
182
	 * 
183
	 * @return string
184
	 */
185
	public static function archives_title() {
186
		return ClassyHelper::get_archives_title();
187
	}
188
189
190
	/**
191
	 * Returns posts
192
	 * 
193
	 * @param  mixed $args        Array of query args
194
	 * @param  string  $return_type ClassyPost/object/id
195
	 * @return mixed               
196
	 */
197
	public static function get_posts($args = false, $return_type = 'ClassyPost') {
198
		$_return = array();
199
200
		$query = ClassyQueryHelper::find_query($args);
201
202
		if (isset($query->posts)) {
203
204
			foreach ($query->posts as $post_id) {
205
				
206
				if ($return_type == 'ClassyPost') {
207
				
208
					$_return[] = new ClassyPost($post_id);
209
				
210
				} elseif($return_type == 'object') {
211
212
					$_return[] = get_post($post_id);
213
214
				} else {
215
216
					$_return[] = $post_id;
217
218
				}
219
220
			}
221
222
		}
223
224
		return $_return;
225
	}
226
227
	/**
228
	 * Returns post
229
	 * 
230
	 * @param  mixed $args Array of query args
231
	 * @param  string  $return_type ClassyPost/object/id
232
	 * @return mixed               
233
	 */
234
	public static function get_post($args = false, $return_type = 'ClassyPost') {
235
		$posts = self::get_posts($args, $return_type);
236
237
		if ( $post = reset($posts ) ) {
238
			return $post;
239
		}
240
	}
241
242
	/**
243
	 * @param array   $prefs
244
	 * @return array mixed
245
	 */
246
	public static function get_pagination( $prefs = array() ) {
247
		global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
248
		global $paged;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
249
		global $wp_rewrite;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
250
251
		$args = array();
252
		$args['total'] = ceil( $wp_query->found_posts / $wp_query->query_vars['posts_per_page'] );
253
		
254
		if ( $wp_rewrite->using_permalinks() ) {
255
			
256
			$url = explode( '?', get_pagenum_link( 0 ) );
257
			
258
			if ( isset( $url[1] ) ) {
259
				parse_str( $url[1], $query );
260
				$args['add_args'] = $query;
261
			}
262
			
263
			$args['format'] = 'page/%#%';
264
			$args['base'] = trailingslashit( $url[0] ).'%_%';
265
266
		} else {
267
			$big = 999999999;
268
			$args['base'] = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) );
269
		}
270
271
		$args['type'] = 'array';
272
		$args['current'] = max( 1, get_query_var( 'paged' ) );
273
		$args['mid_size'] = max( 9 - $args['current'], 3 );
274
		$args['prev_next'] = false;
275
		
276
		if ( is_int( $prefs ) ) {
277
			$args['mid_size'] = $prefs - 2;
278
		} else {
279
			$args = array_merge( $args, $prefs );
280
		}
281
282
		$data = array();
283
		$data['pages'] = ClassyHelper::paginate_links( $args );
0 ignored issues
show
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
284
		$next = get_next_posts_page_link( $args['total'] );
285
		
286
		if ( $next ) {
287
			$data['next'] = array( 'link' => untrailingslashit( $next ), 'class' => 'page-numbers next' );
288
		}
289
290
		$prev = previous_posts( false );
291
		
292
		if ( $prev ) {
293
			$data['prev'] = array( 'link' => untrailingslashit( $prev ), 'class' => 'page-numbers prev' );
294
		}
295
		
296
		if ( $paged < 2 ) {
297
			$data['prev'] = null;
298
		}
299
		
300
		return ClassyHelper::array_to_object($data);
301
	}
302
303
}
304
305
306
/**
307
 * Grab the Classy object and return it.
308
 * Wrapper for Classy::get_instance()
309
 *
310
 * @since  0.1.0
311
 * @return Classy  Singleton instance of plugin class.
312
 */
313
function get_theme_framework() {
314
	return Classy::get_instance();
315
}
316
317
/**
318
 * Get Instance
319
 * 
320
 * @var classy
321
 */
322
$classy = get_theme_framework();