Completed
Push — master ( af77e8...7d9d07 )
by Zack
11s
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 17 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 default GravityView View class.
11
 *
12
 * Houses all base View functionality.
13
 *
14
 * Can be accessed as an array for old compatibility's sake
15
 *  in line with the elements inside the \GravityView_View_Data::$views array.
16
 */
17
class View implements \ArrayAccess {
18
19
	/**
20
	 * @var \WP_Post The backing post instance.
21
	 */
22
	private $post;
23
24
	/**
25
	 * @var \GV\View_Settings The settings.
26
	 *
27
	 * @api
28
	 * @since future
29
	 */
30
	public $settings;
31
32
	/**
33
	 * @var \GV\Form The backing form for this view.
34
	 *
35
	 * Contains the form that is sourced for entries in this view.
36
	 *
37
	 * @api
38
	 * @since future
39
	 */
40
	public $form;
41
42
	/**
43
	 * @var \GV\Field_Collection The fields for this view.
44
	 *
45
	 * Contains all the fields that are attached to this view.
46
	 *
47
	 * @api
48
	 * @since future
49
	 */
50
	public $fields;
51
52
	/**
53
	 * @var \GV\View_Template The template attached to this view.
54
	 */
55
	public $template;
56
57
	/**
58
	 * The constructor.
59
	 */
60 2
	public function __construct() {
61 2
		$this->settings = new View_Settings();
62 2
		$this->fields = new Field_Collection();
63 2
	}
64
65
	/**
66
	 * Register the gravityview WordPress Custom Post Type.
67
	 *
68
	 * @internal
69
	 * @return void
70
	 */
71
	public static function register_post_type() {
72
73
		/** Register only once */
74
		if ( post_type_exists( 'gravityview' ) ) {
75
			return;
76
		}
77
78
		/**
79
		 * @filter `gravityview_is_hierarchical` Make GravityView Views hierarchical by returning TRUE
80
		 * This will allow for Views to be nested with Parents and also allows for menu order to be set in the Page Attributes metabox
81
		 * @since 1.13
82
		 * @param boolean $is_hierarchical Default: false
83
		 */
84
		$is_hierarchical = (bool)apply_filters( 'gravityview_is_hierarchical', false );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
85
86
		$supports = array( 'title', 'revisions' );
87
88
		if ( $is_hierarchical ) {
89
			$supports[] = 'page-attributes';
90
		}
91
92
		/**
93
		 * @filter  `gravityview_post_type_supports` Modify post type support values for `gravityview` post type
94
		 * @see add_post_type_support()
95
		 * @since 1.15.2
96
		 * @param array $supports Array of features associated with a functional area of the edit screen. Default: 'title', 'revisions'. If $is_hierarchical, also 'page-attributes'
97
		 * @param[in] boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
98
		 */
99
		$supports = apply_filters( 'gravityview_post_type_support', $supports, $is_hierarchical );
100
101
		/** Register Custom Post Type - gravityview */
102
		$labels = array(
103
			'name'                => _x( 'Views', 'Post Type General Name', 'gravityview' ),
104
			'singular_name'       => _x( 'View', 'Post Type Singular Name', 'gravityview' ),
105
			'menu_name'           => _x( 'Views', 'Menu name', 'gravityview' ),
106
			'parent_item_colon'   => __( 'Parent View:', 'gravityview' ),
107
			'all_items'           => __( 'All Views', 'gravityview' ),
108
			'view_item'           => _x( 'View', 'View Item', 'gravityview' ),
109
			'add_new_item'        => __( 'Add New View', 'gravityview' ),
110
			'add_new'             => __( 'New View', 'gravityview' ),
111
			'edit_item'           => __( 'Edit View', 'gravityview' ),
112
			'update_item'         => __( 'Update View', 'gravityview' ),
113
			'search_items'        => __( 'Search Views', 'gravityview' ),
114
			'not_found'           => \GravityView_Admin::no_views_text(),
115
			'not_found_in_trash'  => __( 'No Views found in Trash', 'gravityview' ),
116
			'filter_items_list'     => __( 'Filter Views list', 'gravityview' ),
117
			'items_list_navigation' => __( 'Views list navigation', 'gravityview' ),
118
			'items_list'            => __( 'Views list', 'gravityview' ),
119
			'view_items'            => __( 'See Views', 'gravityview' ),
120
			'attributes'            => __( 'View Attributes', 'gravityview' ),
121
		);
122
		$args = array(
123
			'label'               => __( 'view', 'gravityview' ),
124
			'description'         => __( 'Create views based on a Gravity Forms form', 'gravityview' ),
125
			'labels'              => $labels,
126
			'supports'            => $supports,
127
			'hierarchical'        => $is_hierarchical,
128
			/**
129
			 * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
130
			 * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
131
			 * @since 1.15.2
132
			 * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded via shortcode. Default: `true`
133
			 * @param int $view_id The ID of the View currently being requested. `0` for general setting
134
			 */
135
			'public'              => apply_filters( 'gravityview_direct_access', gravityview()->plugin->is_compatible(), 0 ),
136
			'show_ui'             => gravityview()->plugin->is_compatible(),
137
			'show_in_menu'        => gravityview()->plugin->is_compatible(),
138
			'show_in_nav_menus'   => true,
139
			'show_in_admin_bar'   => true,
140
			'menu_position'       => 17,
141
			'menu_icon'           => '',
142
			'can_export'          => true,
143
			/**
144
			 * @filter `gravityview_has_archive` Enable Custom Post Type archive?
145
			 * @since 1.7.3
146
			 * @param boolean False: don't have frontend archive; True: yes, have archive. Default: false
147
			 */
148
			'has_archive'         => apply_filters( 'gravityview_has_archive', false ),
149
			'exclude_from_search' => true,
150
			'rewrite'             => array(
151
				/**
152
				 * @filter `gravityview_slug` Modify the url part for a View.
153
				 * @see http://docs.gravityview.co/article/62-changing-the-view-slug
154
				 * @param string $slug The slug shown in the URL
155
				 */
156
				'slug' => apply_filters( 'gravityview_slug', 'view' ),
157
158
				/**
159
				 * @filter `gravityview/post_type/with_front` Should the permalink structure
160
				 *  be prepended with the front base.
161
				 *  (example: if your permalink structure is /blog/, then your links will be: false->/view/, true->/blog/view/).
162
				 *  Defaults to true.
163
				 * @see https://codex.wordpress.org/Function_Reference/register_post_type
164
				 * @since future
165
				 * @param bool $with_front
166
				 */
167
				'with_front' => apply_filters( 'gravityview/post_type/with_front', true ),
168
			),
169
			'capability_type'     => 'gravityview',
170
			'map_meta_cap'        => true,
171
		);
172
173
		register_post_type( 'gravityview', $args );
174
	}
175
176
177
	/**
178
	 * Construct a \GV\View instance from a \WP_Post.
179
	 *
180
	 * @param \WP_Post $post The \WP_Post instance to wrap.
181
	 * @throws \InvalidArgumentException if $post is not of 'gravityview' type.
182
	 *
183
	 * @api
184
	 * @since future
185
	 * @return \GV\View An instance around this \WP_Post.
186
	 */
187 3
	public static function from_post( \WP_Post $post ) {
188 3
		if ( get_post_type( $post ) != 'gravityview' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
189 1
			throw new \InvalidArgumentException( 'Only gravityview post types can be \GV\View instances.' );
190
		}
191
192 3
		$view = new self();
193 3
		$view->post = $post;
194
195
		/** Get connected form. */
196 3
		$view->form = GF_Form::by_id( $view->_gravityview_form_id );
0 ignored issues
show
Documentation introduced by
The property _gravityview_form_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...
197 3
		if ( ! $view->form ) {
198
			gravityview()->log->error( 'View #{view_id} tried attaching non-existent Form #{form_id} to it.', array(
199
				'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...
200
				'form_id' => $view->_gravityview_form_id ? : 0,
0 ignored issues
show
Documentation introduced by
The property _gravityview_form_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...
201
			) );
202
		}
203
204
		/**
205
		* @filter `gravityview/configuration/fields` Filter the View fields' configuration array
206
		* @since 1.6.5
207
		*
208
		* @param $fields array Multi-array of fields with first level being the field zones
209
		* @param $view_id int The View the fields are being pulled for
210
		*/
211 3
		$configuration = apply_filters( 'gravityview/configuration/fields', (array)$view->_gravityview_directory_fields, $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...
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
212
213
		/** Get all fields. */
214 3
		$view->fields = Field_Collection::from_configuration( $configuration );
215
216
		/** The settings. */
217 3
		$view->settings->update( gravityview_get_template_settings( $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...
218
219
		/** Set the template. */
220 3
		$view->template = new \GV\View_Template( $view->_gravityview_directory_template );
0 ignored issues
show
Bug introduced by
The property _gravityview_directory_template does not seem to exist. Did you mean template?

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...
221
222
		/**
223
		 * @deprecated
224
		 *
225
		 * The data here has been moved to various keys in a \GV\View instance.
226
		 * As a compatibilty layer we allow array access over any \GV\View instance with these keys.
227
		 *
228
		 * This data is immutable (for now).
229
		 *
230
		 * @see \GV\View::offsetGet() for internal mappings.
231
		 */
232 3
		$view->_data = array(
0 ignored issues
show
Bug introduced by
The property _data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
233
			/**
234
			 * @deprecated
235
			 * @see \GV\View::$ID
236
			 */
237
			// 'id' => $view->ID,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
238
239
			/**
240
			 * @deprecated
241
			 * @see \GV\View::$ID
242
			 */
243
			// 'view_id' => $view->ID,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
244
245
			/**
246
			 * @deprecated
247
			 * @see \GV\View::$form
248
			 */
249
			// 'form' => gravityview_get_form( $view->_gravityview_form_id ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
250
251
			/**
252
			 * @deprecated
253
			 * @see \GV\View::$form::$ID
254
			 */
255
			// 'form_id' => $view->_gravityview_form_id,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
256
257
			/**
258
			 * @deprecated
259
			 * @see \GV\View::$settings
260
			 */
261
			// 'atts' => $view->settings->as_atts(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
262
263
			/**
264
			 * @deprecated
265
			 * @see \GV\View::$fields
266
			 */
267
			// 'fields' => \GravityView_View_Data::getInstance()->get_fields( $view->ID ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
268
269
			/**
270
			 * @deprecated
271
			 * @see \GV\View::$template::$ID
272
			 */
273
			// 'template_id' => gravityview_get_template_id( $view->ID ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
274
275 3
			'widgets' => gravityview_get_directory_widgets( $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...
276
		);
277
278 3
		return $view;
279
	}
280
281
	/**
282
	 * Construct a \GV\View instance from a post ID.
283
	 *
284
	 * @param int|string $post_id The post ID.
285
	 * @throws \InvalidArgumentException if $post is not of 'gravityview' type.
286
	 *
287
	 * @api
288
	 * @since future
289
	 * @return \GV\View|null An instance around this \WP_Post or null if not found.
290
	 */
291 3
	public static function by_id( $post_id ) {
292 3
		if ( ! $post = get_post( $post_id ) ) {
293
			return null;
294
		}
295 3
		return self::from_post( $post );
296
	}
297
298
	/**
299
	 * Determines if a view exists to begin with.
300
	 *
301
	 * @param int|\WP_Post|null $view_id The WordPress post ID, a \WP_Post object or null for global $post;
0 ignored issues
show
Bug introduced by
There is no parameter named $view_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
302
	 *
303
	 * @api
304
	 * @since future
305
	 * @return bool Whether the post exists or not.
306
	 */
307 3
	public static function exists( $view ) {
308 3
		return get_post_type( $view ) == 'gravityview';
309
	}
310
311
	/**
312
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
313
	 *
314
	 * @internal
315
	 * @deprecated
316
	 * @since future
317
	 * @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys.
318
	 */
319 1
	public function offsetExists( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetExists is in camel caps, but expected offset_exists instead as per the coding standard.
Loading history...
320 1
		$data_keys = array( 'id', 'view_id', 'form_id', 'template_id', 'atts', 'fields', 'widgets', 'form' );
321 1
		return in_array( $offset, $data_keys );
322
	}
323
324
	/**
325
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
326
	 *
327
	 * Maps the old keys to the new data;
328
	 *
329
	 * @internal
330
	 * @deprecated
331
	 * @since future
332
	 *
333
	 * @throws \RuntimeException during tests if called outside of whiteliested cases.
334
	 *
335
	 * @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys.
336
	 */
337 1
	public function offsetGet( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetGet is in camel caps, but expected offset_get instead as per the coding standard.
Loading history...
338
		
339
		/**
340
		 * Moving towards deprecation, let's ensure we never
341
		 * trigger this from core and tests unless we really want to.
342
		 */
343 1
		if ( defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
344
345 1
			if ( ! in_array( $offset, array( 'id', 'view_id', 'form', 'form_id' ) ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
346
				/**
347
				 * Do not throw an exception for keys that we've yet to move around.
348
				 * Add the other keys as they are moved out to ensure we're not using them in core.
349
				 */
350
351 1
			} else if ( ! empty( $GLOBALS['GRAVITYVIEW_TESTS_VIEW_ARRAY_ACCESS_OVERRIDE'] ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
352
				/**
353
				 * Suppress exception if specifically testing for array acess.
354
				 */
355
356
			} else {
357
				/**
358
				 * No code should be coming into here unless we're specifically testing for deprecated array access.
359
				 */
360
				throw new \RuntimeException( 'This is a \GV\View object should not be accessed as an array.' );
361
			}
362
		}
363
364 1
		if ( ! isset( $this[$offset] ) ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
365
			return null;
366
		}
367
368
		switch ( $offset ) {
369 1
			case 'id':
370 1
			case 'view_id':
371 1
				return $this->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...
372 1
			case 'form':
373 1
				return $this->form;
374 1
			case 'form_id':
375 1
				return $this->form ? $this->form->ID : null;
376 1
			case 'atts':
377
				return $this->as_atts();
0 ignored issues
show
Bug introduced by
The method as_atts() does not seem to exist on object<GV\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
378 1
			case 'template_id':
379 1
				return $this->template ? $this->template->ID : null;
380
			default:
381
				/** @todo move the rest out and get rid of _data completely! */
382
				return $this->_data[$offset];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
383
		}
384
	}
385
386
	/**
387
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
388
	 *
389
	 * @internal
390
	 * @deprecated
391
	 * @since future
392
	 *
393
	 * @throws \RuntimeException The old view data is now immutable.
394
	 *
395
	 * @return void
396
	 */
397 1
	public function offsetSet( $offset, $value ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetSet is in camel caps, but expected offset_set instead as per the coding standard.
Loading history...
398 1
		throw new \RuntimeException( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );
399
	}
400
401
	/**
402
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
403
	 *
404
	 * @internal
405
	 * @deprecated
406
	 * @since future
407
	 * @return void
408
	 */
409 1
	public function offsetUnset( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetUnset is in camel caps, but expected offset_unset instead as per the coding standard.
Loading history...
410 1
		throw new \RuntimeException( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );
411
	}
412
413
	/**
414
	 * Be compatible with the old data object.
415
	 *
416
	 * Some external code expects an array (doing things like foreach on this, or array_keys)
417
	 *  so let's return an array in the old format for such cases. Do not use unless using
418
	 *  for back-compatibility.
419
	 *
420
	 * @internal
421
	 * @deprecated
422
	 * @since future
423
	 * @return array
424
	 */
425 1
	public function as_data() {
426 1
		return array_merge(
427 1
			array( 'id' => $this->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...
428 1
			array( 'view_id' => $this->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...
429 1
			array( 'form_id' => $this->form ? $this->form->ID : null ),
430 1
			array( 'form' => $this->form ? gravityview_get_form( $this->form->ID ) : null ),
431 1
			array( 'atts' => $this->settings->as_atts() ),
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
432 1
			array( 'fields' => $this->fields->by_visible()->as_configuration() ),
433 1
			array( 'template_id' => $this->template? $this->template->ID : null ),
434 1
			$this->_data
435
		);
436
	}
437
438 2
	public function __get( $key ) {
439 2
		return $this->post->$key;
440
	}
441
}
442