Completed
Push — develop ( 3404ef...87ac16 )
by Zack
15:50
created

_mocks.php ➔ GravityView_frontend_get_view_entries()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 77
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8.004

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 15
nop 4
dl 0
loc 77
ccs 24
cts 25
cp 0.96
crap 8.004
rs 6.1476
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 148.

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 1
	do_action( 'gravityview_log_debug', '[gravityview_get_entries] Final Parameters', $criteria );
83
84
	/** ...and all the (now deprectated) filters that usually follow `gravityview_get_entries` */
85
86
	/**
87
	 * @deprecated
88
	 * Do not use this filter anymore.
89
	 */
90 1
	$entries = apply_filters( 'gravityview_before_get_entries', null, $criteria, $parameters, $count );
91 1
	if ( ! is_null( $entries ) ) {
92
		/**
93
		 * We've been given an entries result that we can return,
94
		 *  just set the paging and we're good to go.
95
		 */
96 1
		$paging = rgar( $parameters, 'paging' );
97
	} else {
98 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...
99 1
			->filter( \GV\GF_Entry_Filter::from_search_criteria( $criteria['search_criteria'] ) )
100 1
			->offset( $args['offset'] )
101 1
			->limit( $criteria['paging']['page_size'] );
102
103 1
		if ( $criteria['paging']['page_size'] ) {
104 1
			$entries = $entries->page( ( ( $criteria['paging']['offset'] - $args['offset'] ) / $criteria['paging']['page_size'] ) + 1 );
105
		}
106
107 1
		if ( ! empty( $criteria['sorting'] ) ) {
108 1
			$field = new \GV\Field();
109 1
			$field->ID = $criteria['sorting']['key'];
110 1
			switch( strtolower( $criteria['sorting']['direction'] ) ) {
111 1
				case 'asc':
112 1
					$direction = \GV\Entry_Sort::ASC;
113
					break;
114
				case 'rand':
115
					$direction = \GV\Entry_Sort::RAND;
116
					break;
117 1
				default:
118 1
				case 'desc':
0 ignored issues
show
Unused Code introduced by
case 'desc': $direct..._Sort::DESC; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
119
					$direction = \GV\Entry_Sort::DESC;
120 1
					break;
121
			}
122
123
			$mode = $criteria['sorting']['is_numeric'] ? \GV\Entry_Sort::NUMERIC : \GV\Entry_Sort::ALPHA;
124
			$entries = $entries->sort( new \GV\Entry_Sort( $field, $direction, $mode ) );
125
		}
126
127
		/** Set paging, count and unwrap the entries. */
128
		$paging = array(
129
			'offset' => ( $entries->current_page - 1 ) * $entries->limit,
130 1
			'page_size' => $entries->limit,
131
		);
132 1
		$count = $entries->total();
133
		$entries = array_map( function( $e ) { return $e->as_entry(); }, $entries->all() );
134
	}
135
136
	/** Just one more filter, for compatibility's sake! */
137 3
138
	/**
139
	 * @deprecated
140
	 * Do not use this filter anymore.
141
	 */
142
	$entries = apply_filters( 'gravityview_entries', $entries, $criteria, $parameters, $count );
143
144
	return array( $entries, $paging, $count );
145
}
146
147
/** Add some global fix for field capability discrepancies. */
148
add_filter( 'gravityview/configuration/fields', function( $fields ) {
149
	if ( empty( $fields  ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
150
		return $fields;
151
	}
152
153
	/**
154
	 * Each view field is saved in a weird capability state by default.
155
	 *
156
	 * With loggedin set to false, but a capability of 'read' it introduces
157 3
	 *  some logical issues and is not robust. Fix this behavior throughout
158
	 *  core by making sure capability is '' if log in is not required.
159 3
	 *
160
	 * Perhaps in the UI a fix would be to unite the two fields (as our new
161
	 *  \GV\Field class already does) into one dropdown:
162
	 *
163 3
	 * Anyone, Logged In Only, ... etc. etc.
164 3
	 *
165
	 * The two "settings" should be as tightly coupled as possible to avoid
166
	 *  split logic scenarios. Uniting them into one field is the way to go.
167
	 */
168 3
169
	foreach ( $fields as $position => &$_fields ) {
170
171 3
		if ( empty( $_fields ) ) {
172
			continue;
173
		}
174
175
		foreach ( $_fields as $uid => &$_field ) {
176
			if ( ! isset( $_field['only_loggedin'] ) ) {
177
				continue;
178
			}
179
			/** If we do not require login, we don't require a cap. */
180
			$_field['only_loggedin'] != '1' && ( $_field['only_loggedin_cap'] = '' );
181
		}
182
	}
183
	return $fields;
184
} );
185