Completed
Push — develop ( fae07d...25e00b )
by Zack
19:01
created

Entry_Table_Template::the_entry()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.002

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 0
dl 0
loc 66
ccs 25
cts 26
cp 0.9615
crap 6.002
rs 8.1195
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 Entry Table Template class .
11
 *
12
 * Renders a \GV\Entry using a \GV\Entry_Renderer.
13
 */
14
class Entry_Table_Template extends Entry_Template {
15
	/**
16
	 * @var string The template slug to be loaded (like "table", "list")
17
	 */
18
	public static $slug = 'table';
19
20
	/**
21
	 * Output a field cell.
22
	 *
23
	 * @param \GV\Field $field The field to be ouput.
24
	 *
25
	 * @return string|false The field output or false if "hide_empty" is set.
26
	 */
27 10
	public function the_field( \GV\Field $field ) {
28 10
		$renderer = new Field_Renderer();
29 10
		$source = is_numeric( $field->ID ) ? ( GF_Form::by_id( $field->form_id ) ? : $this->view->form ) : new Internal_Source();
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...
30
31 10
		return $renderer->render( $field, $this->view, $source, $this->entry->from_field( $field ), $this->request );
32
	}
33
34
	/**
35
	 * Out the single entry table body.
36
	 *
37
	 * @return void
38
	 */
39 10
	public function the_entry() {
40
41
		/** @var \GV\Field_Collection $fields */
42 10
		$fields = $this->view->fields->by_position( 'single_table-columns' )->by_visible();
43
44 10
		$context = Template_Context::from_template( $this, compact( 'fields' ) );
45
46
		/**
47
		 * @filter `gravityview_table_cells` Modify the fields displayed in a table
48
		 * @param array $fields
49
		 * @param \GravityView_View $this
50
		 * @deprecated Use `gravityview/template/table/fields`
51
		 */
52 10
		$fields = apply_filters( 'gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance() );
53 10
		$fields = Field_Collection::from_configuration( $fields );
54
55
		/**
56
		 * @filter `gravityview/template/table/fields` Modify the fields displayed in this tables.
57
		 * @param \GV\Field_Collection $fields The fields.
58
		 * @param \GV\Template_Context $context The context.
59
		 * @since 2.0
60
		 */
61 10
		$fields = apply_filters( 'gravityview/template/table/fields', $fields, $context );
62
63 10
		foreach ( $fields->all() as $field ) {
64 10
			$context = Template_Context::from_template( $this, compact( 'field' ) );
65
66 10
			$form = \GV\GF_Form::by_id( $field->form_id ) ? : $this->view->form;
67 10
			$entry = $this->entry->from_field( $field );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entry is correct as $this->entry->from_field($field) (which targets GV\Entry::from_field()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
69 10
			if ( ! $entry ) {
70
				continue;
71
			}
72
73
			/**
74
			 * @deprecated Here for back-compatibility.
75
			 */
76 10
			$column_label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form, $entry ), $field->as_configuration() );
77 10
			$column_label = apply_filters( 'gravityview/template/field_label', $column_label, $field->as_configuration(), $form->form ? $form->form : null, $entry->as_entry() );
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...
78
79
			/**
80
			 * @filter `gravityview/template/field/label` Override the field label.
81
			 * @since 2.0
82
			 * @param[in,out] string $column_label The label to override.
83
			 * @param \GV\Template_Context $context The context.
84
			 */
85 10
			$column_label = apply_filters( 'gravityview/template/field/label', $column_label, $context );
86
87
			/**
88
			 * @filter `gravityview/template/table/entry/hide_empty`
89
			 * @param boolean $hide_empty Should the row be hidden if the value is empty? Default: don't hide.
90
			 * @param \GV\Template_Context $context The context ;) Love it, cherish it. And don't you dare modify it!
91
			 */
92 10
			$hide_empty = apply_filters( 'gravityview/render/hide-empty-zone', $this->view->settings->get( 'hide_empty', false ), $context );
93
94 10
			echo \gravityview_field_output( array(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
95 10
				'entry' => $this->entry->as_entry(),
96 10
				'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
97 10
				'label' => $column_label,
98 10
				'value' => $this->the_field( $field ),
99 10
				'markup' => '<tr id="{{ field_id }}" class="{{ class }}"><th scope="row">{{ label }}</th><td>{{ value }}</td></tr>',
100 10
				'hide_empty' => $hide_empty,
101 10
				'zone_id' => 'single_table-columns',
102 10
			), $context );
103
		}
104 10
	}
105
}
106