Completed
Push — develop ( a3eda8...2f2ee7 )
by Gennady
17:12
created

Template   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A split_slug() 0 7 3
A push_template_data() 0 15 3
A pop_template_data() 0 7 2
A __destruct() 0 2 1
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 24 and the first side effect is on line 6.

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
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * Load up the Gamajo Template Loader.
11
 *
12
 * @see https://github.com/GaryJones/Gamajo-Template-Loader
13
 */
14
if ( ! class_exists( '\GV\Gamajo_Template_Loader' ) ) {
15
	require gravityview()->plugin->dir( 'future/lib/class-gamajo-template-loader.php' );
16
}
17
18
/**
19
 * The Template abstract class.
20
 *
21
 * Stores information on where a template to render an object is,
22
 *  and other metadata.
23
 */
24
abstract class Template extends \GV\Gamajo_Template_Loader {
25
	
26
	/**
27
	 * @var array The template data stack.
28
	 */
29
	private static $data_stack = array();
30
31
	/**
32
	 * @var string The located template.
33
	 */
34
	public $located_template = '';
35
36
	/**
37
	 * General template initialization.
38
	 *
39
	 * Sets the $plugin_directory field.
40
	 */
41 68
	public function __construct() {
42
		/** Set plugin directory. */
43 68
		$this->plugin_directory = gravityview()->plugin->dir();
44 68
	}
45
46
	/**
47
	 * Disallow any cleanup for fear of loss of global data.
48
	 *
49
	 * The destructor in Gamajo 1.3.0 destroys all of `$wp_query`.
50
	 * This has the chance of inappropriately destroying valid
51
	 *  data that's been stored under the same key.
52
	 *
53
	 * Disallow this.
54
	 */
55 4
	public function __destruct() {
56 4
	}
57
58
	/**
59
	 * Get a directory part and a full slug+name (file) components.
60
	 *
61
	 * @param string $slug The slug, template base.
62
	 * @param string $name The name, template part. Default: null
63
	 *
64
	 * @return array containing slug directory and slug+name.
65
	 */
66 69
	public static function split_slug( $slug, $name = null ) {
67
68 69
		$dir_name = ( dirname( $slug ) != '.' ) ? trailingslashit( dirname( $slug ) ) : '';
69 69
		$slug_name = basename( $slug ) . ( $name ? "-$name" : '' );
70
71 69
		return array( $dir_name, $slug_name );
72
	}
73
74
	/**
75
	 * Push the current template data down the stack and set.
76
	 *
77
	 * This allows us to use the same variable in the template scope
78
	 *  without destroying data under the same variable in a nested
79
	 *  or parallel template.
80
	 *
81
	 * @param mixed $data The data to set.
82
	 * @param string $var_name The data variable identifier (Default: "data")
83
	 *
84
	 * @see \Gamajo_Template_Loader::set_template_data
85
	 * @see \GV\Template::pop_template_data
86
	 *
87
	 * @return \Gamajo_Template_Loader The current instance.
88
	 */
89 69
	public function push_template_data( $data, $var_name = 'data' ) {
90 69
		if ( ! isset( self::$data_stack[ $var_name ] ) ) {
91 2
			self::$data_stack[ $var_name ] = array();
92
		}
93
94 69
		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...
95
96 69
		if ( isset( $wp_query->query_vars[ $var_name ] ) ) {
97 60
			array_push( self::$data_stack[ $var_name ], $wp_query->query_vars[ $var_name ] );
98
		}
99
100 69
		$this->set_template_data( $data, $var_name );
101
102 69
		return $this;
103
	}
104
105
	/**
106
	 * Restore the template data from the stack.
107
	 *
108
	 * @param string $var_name The data variable identifier (Default: "data")
109
	 *
110
	 * @see \Gamajo_Template_Loader::set_template_data
111
	 * @see \GV\Template::pop_template_data
112
	 *
113
	 * @return $this;
0 ignored issues
show
Documentation introduced by
The doc-type $this; could not be parsed: Expected "|" or "end of type", but got ";" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
114
	 */
115 69
	public function pop_template_data( $var_name = 'data' ) {
116 69
		if ( ! empty( self::$data_stack[ $var_name ] ) ) {
117 60
			$this->set_template_data( array_pop( self::$data_stack[ $var_name ] ), $var_name );
118
		}
119
120 69
		return $this;
121
	}
122
}
123