Completed
Push — develop ( e9f2c4...db0f69 )
by Zack
17:54
created

View_List_Template::the_field()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 3
dl 0
loc 56
ccs 23
cts 23
cp 1
crap 6
rs 8.3377
c 0
b 0
f 0

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 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 View List Template class .
11
 *
12
 * Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer.
13
 */
14
class View_List_Template extends View_Template {
15
	/**
16
	 * @var string The template slug to be loaded (like "table", "list")
17
	 */
18
	public static $slug = 'list';
19
20
	/**
21
	 * Output the field in the list view.
22
	 *
23
	 * @param \GV\Field $field The field to output.
24
	 * @param \GV\Entry $entry The entry.
25
	 * @param array $extras Extra stuff, like wpautop, etc.
26
	 *
27
	 * @return string
28
	 */
29 3
	public function the_field( \GV\Field $field, \GV\Entry $entry, $extras = null ) {
30 3
		$form = $this->view->form;
31
32
		/**
33
		 * Push legacy entry context.
34
		 */
35 3
		\GV\Mocks\Legacy_Context::load( array(
36 3
			'entry' => $entry,
37
		) );
38
39 3
		if ( $entry instanceof Multi_Entry ) {
40 1
			if ( ! $entry = Utils::get( $entry, $field->form_id ) ) {
0 ignored issues
show
Documentation introduced by
The property form_id does not exist on object<GV\Field>. 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...
41 1
				return;
42
			}
43 1
			$form = GF_Form::by_id( $field->form_id );
0 ignored issues
show
Documentation introduced by
The property form_id does not exist on object<GV\Field>. 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...
44
		}
45
46 3
		$context = Template_Context::from_template( $this, compact( 'field', 'entry' ) );
47
48 3
		$renderer = new Field_Renderer();
49 3
		$source = is_numeric( $field->ID ) ? $form : new Internal_Source();
50
		
51 3
		$value = $renderer->render( $field, $this->view, $source, $entry, $this->request );
52
53
		/**
54
		 * @deprecated Here for back-compatibility.
55
		 */
56 3
		$label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form ), $field->as_configuration() );
57 3
		$label = apply_filters( 'gravityview/template/field_label', $label, $field->as_configuration(), $form->form ? $form->form : null, null );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
58
59
		/**
60
		 * @filter `gravityview/template/field/label` Override the field label.
61
		 * @since 2.0
62
		 * @param[in,out] string $label The label to override.
63
		 * @param \GV\Template_Context $context The context.
64
		 */
65 3
		$label = apply_filters( 'gravityview/template/field/label', $label, $context );
66
67
		/**
68
		 * @filter `gravityview/template/table/entry/hide_empty`
69
		 * @param boolean Should the row be hidden if the value is empty? Default: don't hide.
70
		 * @param \GV\Template_Context $context The context ;) Love it, cherish it. And don't you dare modify it!
71
		 */
72 3
		$hide_empty = apply_filters( 'gravityview/render/hide-empty-zone', Utils::get( $extras, 'hide_empty', $this->view->settings->get( 'hide_empty', false ) ), $context );
73
74 3
		if ( is_numeric( $field->ID ) ) {
75 3
			$extras['field'] = $field->as_configuration();
76
		}
77
78 3
		$extras['entry'] = $entry->as_entry();
79 3
		$extras['hide_empty'] = $hide_empty;
80 3
		$extras['label'] = $label;
81 3
		$extras['value'] = $value;
82
83 3
		return \gravityview_field_output( $extras, $context );
84
	}
85
86
	/**
87
	 * Return an array of variables ready to be extracted.
88
	 *
89
	 * @param string|array $zones The field zones to grab.
90
	 *
91
	 * @return array An array ready to be extract()ed in the form of
92
	 *  $zone => \GV\Field_Collection
93
	 *  has_$zone => int
94
	 */
95 3
	public function extract_zone_vars( $zones ) {
96 3
		if ( ! is_array( $zones ) ) {
97
			$zones = array( $zones );
98
		}
99
100 3
		$vars = array();
101 3
		foreach ( $zones as $zone ) {
102 3
			$zone_var = str_replace( '-', '_', $zone );
103 3
			$vars[ $zone_var ] = $this->view->fields->by_position( 'directory_list-' . $zone )->by_visible();
104 3
			$vars[ "has_$zone_var" ] = $vars[ $zone_var ]->count();
105
		}
106
107 3
		return $vars;
108
	}
109
110
	/**
111
	 * `gravityview_entry_class` and `gravityview/template/list/entry/class` filters.
112
	 *
113
	 * Modify of the class of a row.
114
	 *
115
	 * @param string $class The class.
116
	 * @param \GV\Entry $entry The entry.
117
	 * @param \GV\Template_Context The context.
118
	 *
119
	 * @return string The classes.
120
	 */
121 3
	public static function entry_class( $class, $entry, $context ) {
122
		/**
123
		 * @filter `gravityview_entry_class` Modify the class applied to the entry row.
124
		 * @param string $class Existing class.
125
		 * @param array $entry Current entry being displayed
126
		 * @param \GravityView_View $this Current GravityView_View object
127
		 * @deprecated Use `gravityview/template/list/entry/class`
128
		 * @return string The modified class.
129
		 */
130 3
		$class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() );
131
132
		/**
133
		 * @filter `gravityview/template/list/entry/class` Modify the class aplied to the entry row.
134
		 * @param string $class The existing class.
135
		 * @param \GV\Template_Context The context.
136
		 * @return string The modified class.
137
		 */
138 3
		return apply_filters( 'gravityview/template/list/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) );
139
	}
140
141
	/**
142
	 * `gravityview_list_body_before` and `gravityview/template/list/body/before` actions.
143
	 *
144
	 * Output inside the `tbody` of the list.
145
	 *
146
	 * @param $context \GV\Template_Context The 2.0 context.
147
	 *
148
	 * @return void
149
	 */
150 3
	public static function body_before( $context ) {
151
		/**
152
		 * @action `gravityview/template/list/body/before` Output inside the `tbody` of the list.
153
		 * @since 2.0
154
		 * @param \GV\Template_Context $context The template context.
155
		 */
156 3
		do_action( 'gravityview/template/list/body/before', $context );
157
158
		/**
159
		* @action `gravityview_list_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows.
160
		* @deprecated Use `gravityview/template/list/body/before`
161
		* @since 1.0.7
162
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
163
		*/
164 3
		do_action( 'gravityview_list_body_before', \GravityView_View::getInstance() /** ugh! */ );
165 3
	}
166
167
	/**
168
	 * `gravityview_list_body_after` and `gravityview/template/list/body/after` actions.
169
	 *
170
	 * Output inside the `tbody` of the list.
171
	 *
172
	 * @param $context \GV\Template_Context The 2.0 context.
173
	 *
174
	 * @return void
175
	 */
176 3
	public static function body_after( $context ) {
177
		/**
178
		 * @action `gravityview/template/list/body/after` Output inside the `tbody` of the list at the end.
179
		 * @since 2.0
180
		 * @param \GV\Template_Context $context The template context.
181
		 */
182 3
		do_action( 'gravityview/template/list/body/after', $context );
183
184
		/**
185
		* @action `gravityview_list_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows.
186
		* @deprecated Use `gravityview/template/list/body/after`
187
		* @since 1.0.7
188
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
189
		*/
190 3
		do_action( 'gravityview_list_body_after', \GravityView_View::getInstance() /** ugh! */ );
191 3
	}
192
193
	/**
194
	 * `gravityview_list_entry_before` and `gravityview/template/list/entry/before` actions.
195
	 * `gravityview_list_entry_title_before` and `gravityview/template/list/entry/title/before` actions.
196
	 * `gravityview_list_entry_content_before` and `gravityview/template/list/entry/content/before` actions.
197
	 * `gravityview_list_entry_footer_before` and `gravityview/template/list/entry/footer/before` actions.
198
	 *
199
	 * Output inside the `entry` of the list.
200
	 *
201
	 * @param \GV\Entry $entry The entry.
202
	 * @param \GV\Template_Context $context The 2.0 context.
203
	 * @param string $zone The list zone (footer, image, title, etc.).
204
	 *
205
	 * @return void
206
	 */
207 3
	public static function entry_before( $entry, $context, $zone = '' ) {
208 3
		$zone = str_replace( '//', '/', "/$zone/" );
209
210
		/**
211
		 * @action `gravityview/template/list/entry/$zone/before` Output inside the `entry` of the list at the end.
212
		 * @since 2.0
213
		 * @param \GV\Template_Context $context The template context.
214
		 */
215 3
		do_action( sprintf( 'gravityview/template/list/entry%sbefore', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) );
216
217 3
		$zone = str_replace( '/', '_', $zone );
218
219
		/**
220
		* @action `gravityview_list_entry_$zone_before` Inside the `entry`, before any rows are rendered. Can be used to insert additional rows.
221
		* @deprecated Use `gravityview/template/list/entry/$zone/before`
222
		* @since 1.0.7
223
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
224
		*/
225 3
		do_action( sprintf( 'gravityview_list_entry%sbefore', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ );
226 3
	}
227
228
	/**
229
	 * `gravityview_list_entry_after` and `gravityview/template/list/entry/after` actions.
230
	 * `gravityview_list_entry_title_after` and `gravityview/template/list/entry/title/after` actions.
231
	 * `gravityview_list_entry_content_after` and `gravityview/template/list/entry/content/after` actions.
232
	 * `gravityview_list_entry_footer_after` and `gravityview/template/list/entry/footer/after` actions.
233
	 *
234
	 * Output inside the `entry` of the list.
235
	 *
236
	 * @param \GV\Entry $entry The entry.
237
	 * @param \GV\Template_Context $context The 2.0 context.
238
	 * @param string $zone The list zone (footer, image, title, etc.).
239
	 *
240
	 * @return void
241
	 */
242 3
	public static function entry_after( $entry, $context, $zone = '' ) {
243 3
		$zone = str_replace( '//', '/', "/$zone/" );
244
245
		/**
246
		 * @action `gravityview/template/list/entry/$zone/after` Output inside the `entry` of the list at the end.
247
		 * @since 2.0
248
		 * @param \GV\Template_Context $context The template context.
249
		 */
250 3
		do_action( sprintf( 'gravityview/template/list/entry%safter', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) );
251
252 3
		$zone = str_replace( '/', '_', $zone );
253
254
		/**
255
		* @action `gravityview_list_entry_$zone_after` Inside the `entry`, after any rows are rendered. Can be used to insert additional rows.
256
		* @deprecated Use `gravityview/template/list/entry/after`
257
		* @since 1.0.7
258
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
259
		*/
260 3
		do_action( sprintf( 'gravityview_list_entry%safter', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ );
261 3
	}
262
}
263