Completed
Push — develop ( c04cf6...7b9575 )
by Zack
08:02
created

Entry_Renderer::render()   D

Complexity

Conditions 15
Paths 28

Size

Total Lines 113

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 18.0755

Importance

Changes 0
Metric Value
cc 15
nc 28
nop 3
dl 0
loc 113
ccs 35
cts 46
cp 0.7609
crap 18.0755
rs 4.7333
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
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 19
	public function render( Entry $entry, View $view, Request $request = null ) {
29 19
		if ( is_null( $request ) ) {
30 10
			$request = &gravityview()->request;
31
		}
32
33 19
		if ( ! $request->is_renderable() ) {
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 19
		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
45
46
		/**
47
		 * @action `gravityview_render_entry_{View ID}` Before rendering a single entry for a specific View ID
48
		 * @since 1.17
49
		 *
50
		 * @since 2.0
51
		 * @param \GV\Entry $entry The entry about to be rendered
52
		 * @param \GV\View $view The connected view
53
		 * @param \GV\Request $request The associated request
54
		 */
55 19
		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...
56
57
		/** Entry does not belong to this view. */
58 19
		if ( $view->joins ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->joins of type GV\Join[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59 1
			$form_ids = array();
60 1
			foreach ( $view->joins as $join ) {
61 1
				$form_ids[] = $join->join->ID;
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in GV\Source.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62 1
				$form_ids[] = $join->join_on->ID;
63
			}
64 1
			foreach ( $entry->entries as $e ) {
0 ignored issues
show
Bug introduced by
The property entries does not seem to exist in GV\Entry.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65 1
				if ( ! in_array( $e['form_id'], $form_ids ) ) {
66
					gravityview()->log->error( 'The requested entry does not belong to this View. Entry #{entry_id}, #View {view_id}', array( 'entry_id' => $e->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...
67
					return null;
68
				}
69
			}
70 18
		} else if ( $view->form && $view->form->ID != $entry['form_id'] ) {
71
			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...
72
			return null;
73
		}
74
75
		/**
76
		 * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views.
77
		 * @since 1.6
78
		 * @param deprecated
79
		 * @see The `gravityview_get_template_id` filter
80
		 * @param string $slug Default: 'table'
81
		 * @param string $view The current view context: single
82
		 */
83 19
		$template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'single' );
84
85
		/**
86
		 * Load a legacy override template if exists.
87
		 */
88 19
		$override = new \GV\Legacy_Override_Template( $view, $entry, null, $request );
89 19
		foreach ( array( 'single' ) as $part ) {
90 19
			if ( ( $path = $override->get_template_part( $template_slug, $part ) ) && strpos( $path, '/deprecated' ) === false ) {
91
				/**
92
				 * We have to bail and call the legacy renderer. Crap!
93
				 */
94
				gravityview()->log->notice( 'Legacy templates detected in theme {path}', array( 'path' => $path ) );
95
96
				/**
97
				 * Show a warning at the top, if View is editable by the user.
98
				 */
99
				add_action( 'gravityview_before', $this->legacy_template_warning( $view, $path ) );
100
101
				return $override->render( $template_slug );
102
			}
103
		}
104
105
		/**
106
		 * @filter `gravityview/template/entry/class` Filter the template class that is about to be used to render the entry.
107
		 * @since 2.0
108
		 * @param string $class The chosen class - Default: \GV\Entry_Table_Template.
109
		 * @param \GV\Entry $entry The entry about to be rendered.
110
		 * @param \GV\View $view The view connected to it.
111
		 * @param \GV\Request $request The associated request.
112
		 */
113 19
		$class = apply_filters( 'gravityview/template/entry/class', sprintf( '\GV\Entry_%s_Template', ucfirst( $template_slug ) ), $entry, $view, $request );
114 19
		if ( ! $class || ! class_exists( $class ) ) {
115
			gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) );
116
			$class = '\GV\Entry_Legacy_Template';
117
		}
118 19
		$template = new $class( $entry, $view, $request );
119
120
		add_action( 'gravityview/template/after', $view_id_output = function( $context ) {
121 19
			printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID );
122 19
		} );
123
124
		/** Mock the legacy state for the widgets and whatnot */
125 19
		$entries = new Entry_Collection();
126 19
		$entries->add( $entry );
127 19
		\GV\Mocks\Legacy_Context::push( array(
128 19
			'view' => $view,
129 19
			'entries' => $entries,
130 19
			'entry' => $entry,
131 19
			'request' => $request,
132
		) );
133
134 19
		ob_start();
135 19
		$template->render();
136
137 19
		remove_action( 'gravityview/template/after', $view_id_output );
138
139 19
		return ob_get_clean();
140
	}
141
}
142