Completed
Push — master ( d02afe...589d3f )
by Zack
11s
created

_mocks.php ➔ GravityView_frontend_get_view_entries()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 59
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 6
nop 4
dl 0
loc 59
ccs 21
cts 21
cp 1
crap 5
rs 8.7117
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 15 and the first side effect is on line 130.

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\Mocks;
3
4
/**
5
 * This file contains mock code for deprecated functions.
6
 */
7
8
/**
9
 * @see \GravityView_View_Data::add_view
10
 * @internal
11
 * @since future
12
 *
13
 * @return array|false The old array data, or false on error.
14
 */
15
function GravityView_View_Data_add_view( $view_id, $atts ) {
0 ignored issues
show
Coding Style introduced by
The function name GravityView_View_Data_add_view is in camel caps, but expected _gravity_view_view_data_add_view instead as per the coding standard.
Loading history...
16
	/** Handle array of IDs. */
17 1
	if ( is_array( $view_id ) ) {
18 1
		foreach ( $view_id as $id ) {
19 1
			call_user_func( __FUNCTION__, $id, $atts );
20
		}
21
22 1
		if ( ! gravityview()->request->views->count() ) {
23 1
			return array();
24
		}
25
26 1
		return array_combine(
27
			array_map( function( $view ) { return $view->ID; }, gravityview()->request->views->all() ),
28
			array_map( function( $view ) { return $view->as_data(); }, gravityview()->request->views->all() )
29
		);
30
	}
31
32
	/** View has been set already. */
33 1
	if ( $view = gravityview()->request->views->get( $view_id ) ) {
34 1
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s already exists.', $view_id ) );
35 1
		return $view->as_data();
0 ignored issues
show
Deprecated Code introduced by
The method GV\View::as_data() has been deprecated.

This method has been deprecated.

Loading history...
36
	}
37
38 1
	$view = \GV\View::by_id( $view_id );
39 1
	if ( ! $view ) {
40 1
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s does not exist.', $view_id ) );
41 1
		return false;
42
	}
43
44
	/** Doesn't have a connected form. */
45 1
	if ( ! $view->form ) {
46 1
		do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; Post ID #%s does not have a connected form.', $view_id ) );
47 1
		return false;
48
	}
49
50
	/** Update the settings */
51 1
	if ( is_array( $atts ) ) {
52 1
		$view->settings->update( $atts );
53
	}
54
55 1
	gravityview()->request->views->add( $view );
56
57 1
	return $view->as_data();
0 ignored issues
show
Deprecated Code introduced by
The method GV\View::as_data() has been deprecated.

This method has been deprecated.

Loading history...
58
}
59
60
/**
61
 * @see \GravityView_frontend::get_view_entries
62
 * @internal
63
 * @since future
64
 *
65
 * @return array The old associative array data as returned by
66
 *  \GravityView_frontend::get_view_entries(), the paging parameters
67
 *  and a total count of all entries.
68
 */
69
function GravityView_frontend_get_view_entries( $args, $form_id, $parameters, $count ) {
0 ignored issues
show
Coding Style introduced by
The function name GravityView_frontend_get_view_entries is in camel caps, but expected _gravity_view_frontend_get_view_entries instead as per the coding standard.
Loading history...
70 1
	$form = \GV\GF_Form::by_id( $form_id );
71
72
	/**
73
	 * Kick off all advanced filters.
74
	 *
75
	 * Parameters and criteria are pretty much the same thing here, just
76
	 *  different naming, where `$parameters` are the initial parameters
77
	 *  calculated for hte view, and `$criteria` are the filtered ones
78
	 *  retrieved via `GVCommon::calculate_get_entries_criteria`.
79
	 */
80 1
	$criteria = \GVCommon::calculate_get_entries_criteria( $parameters, $form->ID );
81
82
	/** ...and all the (now deprectated) filters that usually follow `gravityview_get_entries` */
83
84
	/**
85
	 * @deprecated
86
	 * Do not use this filter anymore.
87
	 */
88 1
	$entries = apply_filters( 'gravityview_before_get_entries', null, $criteria, $parameters, $count );
89 1
	if ( ! is_null( $entries ) ) {
90
		/**
91
		 * We've been given an entries result that we can return,
92
		 *  just set the paging and we're good to go.
93
		 */
94 1
		$paging = rgar( $parameters, 'paging' );
95
	} else {
96 1
		$entries = $form->entries
0 ignored issues
show
Documentation introduced by
The property entries does not exist on object<GV\GF_Form>. 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...
97 1
			->filter( \GV\GF_Entry_Filter::from_search_criteria( $criteria['search_criteria'] ) )
98 1
			->offset( $args['offset'] )
99 1
			->limit( $criteria['paging']['page_size'] )
100 1
			->page( ( ( $criteria['paging']['offset'] - $args['offset'] ) / $criteria['paging']['page_size'] ) + 1 );
101 1
		if ( ! empty( $criteria['sorting'] ) ) {
102 1
			$field = new \GV\Field();
103 1
			$field->ID = $criteria['sorting']['key'];
104 1
			$direction = strtolower( $criteria['sorting']['direction'] ) == 'asc' ? \GV\Entry_Sort::ASC : \GV\Entry_Sort::DESC;
105 1
			$mode = $criteria['sorting']['is_numeric'] ? \GV\Entry_Sort::NUMERIC : \GV\Entry_Sort::ALPHA;
106 1
			$entries = $entries->sort( new \GV\Entry_Sort( $field, $direction, $mode ) );
107
		}
108
109
		/** Set paging, count and unwrap the entries. */
110
		$paging = array(
111 1
			'offset' => ( $entries->current_page - 1 ) * $entries->limit,
112 1
			'page_size' => $entries->limit,
113
		);
114 1
		$count = $entries->total();
115
		$entries = array_map( function( $e ) { return $e->as_entry(); }, $entries->all() );
116
	}
117
118
	/** Just one more filter, for compatibility's sake! */
119
120
	/**
121
	 * @deprecated
122
	 * Do not use this filter anymore.
123
	 */
124 1
	$entries = apply_filters( 'gravityview_entries', $entries, $criteria, $parameters, $count );
125
126 1
	return array( $entries, $paging, $count );
127
}
128
129
/** Add some global fix for field capability discrepancies. */
130
add_filter( 'gravityview/configuration/fields', function( $fields ) {
131 3
	if ( empty( $fields  ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
132
		return $fields;
133
	}
134
135
	/**
136
	 * Each view field is saved in a weird capability state by default.
137
	 *
138
	 * With loggedin set to false, but a capability of 'read' it introduces
139
	 *  some logical issues and is not robust. Fix this behavior throughout
140
	 *  core by making sure capability is '' if log in is not required.
141
	 *
142
	 * Perhaps in the UI a fix would be to unite the two fields (as our new
143
	 *  \GV\Field class already does) into one dropdown:
144
	 *
145
	 * Anyone, Logged In Only, ... etc. etc.
146
	 *
147
	 * The two "settings" should be as tightly coupled as possible to avoid
148
	 *  split logic scenarios. Uniting them into one field is the way to go.
149
	 */
150
151 3
	foreach ( $fields as $position => &$_fields ) {
152
153 3
		if ( empty( $_fields ) ) {
154
			continue;
155
		}
156
157 3
		foreach ( $_fields as $uid => &$_field ) {
158 3
			if ( ! isset( $_field['only_loggedin'] ) ) {
159
				continue;
160
			}
161
			/** If we do not require login, we don't require a cap. */
162 3
			$_field['only_loggedin'] != '1' && ( $_field['only_loggedin_cap'] = '' );
163
		}
164
	}
165 3
	return $fields;
166
} );
167