Completed
Push — master ( b9ab02...8a7f17 )
by Zack
54:54 queued 46:56
created

View_Renderer::render()   D

Complexity

Conditions 13
Paths 82

Size

Total Lines 109
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 14.0562

Importance

Changes 0
Metric Value
cc 13
eloc 43
nc 82
nop 2
dl 0
loc 109
ccs 31
cts 38
cp 0.8158
crap 14.0562
rs 4.9922
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 14 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
 * The \GV\View_Renderer class.
11
 *
12
 * Houses some preliminary \GV\View rendering functionality.
13
 */
14
class View_Renderer extends Renderer {
15
16
	/**
17
	 * Renders a \GV\View instance.
18
	 *
19
	 * @param \GV\View $view The View instance to render.
20
	 * @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request`
21
	 *
22
	 * @api
23
	 * @since 2.0
24
	 *
25
	 * @return string The rendered View.
26
	 */
27 14
	public function render( View $view, Request $request = null ) {
28 14
		if ( is_null( $request ) ) {
29 10
			$request = &gravityview()->request;
30
		}
31
32 14
		if ( ! in_array( get_class( $request ), array( 'GV\Frontend_Request', 'GV\Mock_Request', 'GV\REST\Request' ) ) ) {
33
			gravityview()->log->error( 'Renderer unable to render View in {request_class} context', array( 'request_class' => get_class( $request ) ) );
34
			return null;
35
		}
36
37
		/**
38
		 * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views.
39
		 * @since 1.6
40
		 * @param deprecated
41
		 * @see The `gravityview_get_template_id` filter
42
		 * @param string $slug Default: 'table'
43
		 * @param string $view The current view context: directory.
44
		 */
45 14
		$template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'directory' );
46
47
		/**
48
		 * Figure out whether to get the entries or not.
49
		 *
50
		 * Some contexts don't need initial entries, like the DataTables directory type.
51
		 *
52
		 * @filter `gravityview_get_view_entries_{$template_slug}` Whether to get the entries or not.
53
		 * @param boolean $get_entries Get entries or not, default: true.
54
		 */
55 14
		$get_entries = apply_filters( 'gravityview_get_view_entries_' . $template_slug, true );
56
57 14
		$hide_until_searched = $view->settings->get( 'hide_until_searched' );
58
59
		/**
60
		 * Hide View data until search is performed.
61
		 */
62 14
		$get_entries = ( $hide_until_searched && ! $request->is_search() ) ? false : $get_entries;
63
64
		/**
65
		 * Fetch entries for this View.
66
		 */
67 14
		if ( $get_entries ) {
68 14
			$entries = $view->get_entries( $request );
69
		} else {
70 3
			$entries = new \GV\Entry_Collection();
71
		}
72
73
		/**
74
		 * Load a legacy override template if exists.
75
		 */
76 14
		$override = new \GV\Legacy_Override_Template( $view, null, null, $request );
77 14
		foreach ( array( 'header', 'body', 'footer' ) as $part ) {
78 14
			if ( ( $path = $override->get_template_part( $template_slug, $part ) ) && strpos( $path, '/deprecated' ) === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
79
				/**
80
				 * We have to bail and call the legacy renderer. Crap!
81
				 */
82
				gravityview()->log->notice( 'Legacy templates detected in theme {path}', array( 'path' => $path ) );
83
84
				/**
85
				 * Show a warning at the top, if View is editable by the user.
86
				 */
87
				add_action( 'gravityview_before', $this->legacy_template_warning( $view, $path ) );
88
89 14
				return $override->render( $template_slug );
90
			}
91
		}
92
93
		/**
94
		 * @filter `gravityview/template/view/class` Filter the template class that is about to be used to render the view.
95
		 * @since 2.0
96
		 * @param string $class The chosen class - Default: \GV\View_Table_Template.
97
		 * @param \GV\View $view The view about to be rendered.
98
		 * @param \GV\Request $request The associated request.
99
		 */
100 14
		$class = apply_filters( 'gravityview/template/view/class', sprintf( '\GV\View_%s_Template', ucfirst( $template_slug ) ), $view, $request );
101 14
		if ( ! $class || ! class_exists( $class ) ) {
102
			gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) );
103
			$class = '\GV\View_Legacy_Template';
104
		}
105 14
		$template = new $class( $view, $entries, $request );
106
107
		/** @todo Deprecate this! */
108 14
		$parameters = \GravityView_frontend::get_view_entries_parameters( $view->settings->as_atts(), $view->form->ID );
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
109
110 14
		global $post;
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...
111
112
		/** Mock the legacy state for the widgets and whatnot */
113 14
		\GV\Mocks\Legacy_Context::push( array_merge( array(
114 14
			'view' => $view,
115 14
			'entries' => $entries,
116 14
			'request' => $request,
117
		), empty( $parameters ) ? array() : array(
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 8.
Loading history...
118 14
			'paging' => $parameters['paging'],
119 14
			'sorting' => $parameters['sorting'],
120 9
		), empty( $post ) ? array() : array(
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 8.
Loading history...
121 14
			'post' => $post,
122
		) ) );
123
124 14
		add_action( 'gravityview/template/after', $view_id_output = function( $context ) {
125 14
			printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID );
126 14
		} );
127
128
		ob_start();
129
		$template->render();
130
131
		remove_action( 'gravityview/template/after', $view_id_output );
132
133
		\GV\Mocks\Legacy_Context::pop();
134
		return ob_get_clean();
135
	}
136
}
137