Completed
Push — develop ( 744f01...bc21cb )
by Zack
17:24
created

Entry_Renderer::render()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 101

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 12.3274

Importance

Changes 0
Metric Value
cc 11
nc 16
nop 3
dl 0
loc 101
ccs 21
cts 27
cp 0.7778
crap 12.3274
rs 5.8533
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\Entry_Renderer class.
11
 *
12
 * Houses some preliminary \GV\Entry rendering functionality.
13
 */
14
class Entry_Renderer extends Renderer {
15
16
	/**
17
	 * Renders a single \GV\Entry instance.
18
	 *
19
	 * @param \GV\Entry $entry The Entry instance to render.
20
	 * @param \GV\View $view The View connected to the entry.
21
	 * @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request`
22
	 *
23
	 * @api
24
	 * @since 2.0
25
	 *
26
	 * @return string The rendered Entry.
27
	 */
28 14
	public function render( Entry $entry, View $view, Request $request = null ) {
29 14
		if ( is_null( $request ) ) {
30 8
			$request = &gravityview()->request;
31
		}
32
33 14
		if ( ! in_array( get_class( $request ), array( 'GV\Frontend_Request', 'GV\Mock_Request', 'GV\REST\Request' ) ) ) {
34
			gravityview()->log->error( 'Renderer unable to render Entry in {request_class} context', array( 'request_class' => get_class( $request ) ) );
35
			return null;
36
		}
37
38
		/**
39
		 * This View is password protected. Output the form.
40
		 */
41 14
		if ( post_password_required( $view->ID ) ) {
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42 1
			return get_the_password_form( $view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
		}
44
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
45
46
		/** Entry does not belong to this view. */
47 14
		if ( $view->form && $view->form->ID != $entry['form_id'] ) {
48 1
			gravityview()->log->error( 'The requested entry does not belong to this View. Entry #{entry_id}, #View {view_id}', array( 'entry_id' => $entry->ID, 'view_id' => $view->ID ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49 1
			return null;
50
		}
51
52
		/**
53
		 * @action `gravityview_render_entry_{View ID}` Before rendering a single entry for a specific View ID
54
		 * @since 1.17
55
		 *
56
		 * @since 2.0
57
		 * @param \GV\Entry $entry The entry about to be rendered
58
		 * @param \GV\View $view The connected view
59
		 * @param \GV\Request $request The associated request
60
		 */
61 14
		do_action( 'gravityview_render_entry_' . $view->ID, $entry, $view, $request );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
63
		/**
64
		 * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views.
65
		 * @since 1.6
66
		 * @param deprecated
67
		 * @see The `gravityview_get_template_id` filter
68
		 * @param string $slug Default: 'table'
69
		 * @param string $view The current view context: single
70
		 */
71 14
		$template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'single' );
72
73
		/**
74
		 * Load a legacy override template if exists.
75
		 */
76 14
		$override = new \GV\Legacy_Override_Template( $view, $entry, null, $request );
77 14
		foreach ( array( 'single' ) 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/entry/class` Filter the template class that is about to be used to render the entry.
95
		 * @since 2.0
96
		 * @param string $class The chosen class - Default: \GV\Entry_Table_Template.
97
		 * @param \GV\Entry $entry The entry about to be rendered.
98
		 * @param \GV\View $view The view connected to it.
99
		 * @param \GV\Request $request The associated request.
100
		 */
101 14
		$class = apply_filters( 'gravityview/template/entry/class', sprintf( '\GV\Entry_%s_Template', ucfirst( $template_slug ) ), $entry, $view, $request );
102 14
		if ( ! $class || ! class_exists( $class ) ) {
103
			gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) );
104
			$class = '\GV\Entry_Legacy_Template';
105
		}
106 14
		$template = new $class( $entry, $view, $request );
107
108 14
		add_action( 'gravityview/template/after', $view_id_output = function( $context ) {
109 14
			printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID );
110 14
		} );
111
112
		/** Mock the legacy state for the widgets and whatnot */
113
		$entries = new Entry_Collection();
114
		$entries->add( $entry );
115
		\GV\Mocks\Legacy_Context::push( array(
116
			'view' => $view,
117
			'entries' => $entries,
118
			'entry' => $entry,
119
			'request' => $request,
120
		) );
121
122
		ob_start();
123
		$template->render();
124
125
		remove_action( 'gravityview/template/after', $view_id_output );
126
127
		return ob_get_clean();
128
	}
129
}
130