Completed
Pull Request — master (#936)
by Zack
20:10 queued 09:19
created

GF_Form::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 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 Gravity Forms Form class implementation.
11
 *
12
 * Accessible as an array for back-compatibility.
13
 */
14
class GF_Form extends Form implements \ArrayAccess {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
15
16
	/**
17
	 * @var string The identifier of the backend used for this form.
18
	 * @api
19
	 * @since future
20
	 */
21
	public static $backend = self::BACKEND_GRAVITYFORMS;
22
23
	/**
24
	 * Initialization.
25
	 */
26 3
	private function __construct() {
27 3
		if ( ! class_exists( 'GFAPI' ) ) {
28
			gravityview()->log->error( 'Gravity Forms plugin is not active.' );
29
		}
30 3
	}
31
32
	/**
33
	 * Construct a \GV\GF_Form instance by ID.
34
	 *
35
	 * @param int|string $form_id The internal form ID.
36
	 *
37
	 * @api
38
	 * @since future
39
	 * @return \GV\GF_Form|null An instance of this form or null if not found.
40
	 */
41 4
	public static function by_id( $form_id ) {
42
43 4
		$form = wp_cache_get( 'gf_form_' . $form_id, 'gravityview' );
44
45 4
		if ( ! $form ) {
46 4
			$form = \GFAPI::get_form( $form_id );
47
		}
48
49 4
		if ( ! $form ) {
50 1
			return null;
51
		}
52
53 4
		wp_cache_set( 'gf_form_' . $form_id, $form, 'gravityview' );
54
55 4
		$self = new self();
56 4
		$self->form = $form;
0 ignored issues
show
Bug introduced by
The property form cannot be accessed from this context as it is declared private in class GV\Form.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
57
58 4
		$self->ID = $self->form['id'];
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...
59
60 4
		return $self;
61
	}
62
63
	/**
64
	 * Get all entries for this form.
65
	 *
66
	 * @api
67
	 * @since future
68
	 *
69
	 * @return \GV\Entry_Collection The \GV\Entry_Collection
70
	 */
71
	public function get_entries() {
72
		$entries = new \GV\Entry_Collection();
73
74
		$form = &$this;
75
76
		/** Add the fetcher lazy callback. */
77
		$entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) {
78
			$entries = new \GV\Entry_Collection();
79
80
			$search_criteria = array();
81
			$sorting = array();
82
			$paging = array();
83
84
			/** Apply the filters */
85
			foreach ( $filters as $filter ) {
86
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
87
			}
88
89
			/** Apply the sorts */
90
			foreach ( $sorts as $sort ) {
91
				/** Gravity Forms does not have multi-sorting, so just overwrite. */
92
				$sorting = array(
93
					'key' => $sort->field->ID,
94
					'direction' => $sort->direction,
95
					'is_numeric' => $sort->mode == Entry_Sort::NUMERIC,
96
				);
97
			}
98
99
			/** The offset and limit */
100
			if ( ! empty( $offset->limit ) ) {
101
				$paging['page_size'] = $offset->limit;
102
			}
103
104
			if ( ! empty( $offset->offset ) ) {
105
				$paging['offset'] = $offset->offset;
106
			}
107
108
			foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) {
109
				$entries->add( \GV\GF_Entry::from_entry( $entry ) );
110
			}
111
112
			return $entries;
113
		} );
114
115
		/** Add the counter lazy callback. */
116
		$entries->add_count_callback( function( $filters ) use ( $form ) {
117
118
			$search_criteria = array();
119
			$sorting = array();
120
121
			/** Apply the filters */
122
			foreach ( $filters as $filter ) {
123
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
124
			}
125
126
			return \GFAPI::count_entries( $form->ID, $search_criteria );
127
		} );
128
129
		return $entries;
130
	}
131
132
	/**
133
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
134
	 *
135
	 * @internal
136
	 * @deprecated
137
	 * @since future
138
	 * @return bool Whether the offset exists or not.
139
	 */
140
	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...
141
		return isset( $this->form[$offset] );
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...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
142
	}
143
144
	/**
145
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
146
	 *
147
	 * Maps the old keys to the new data;
148
	 *
149
	 * @internal
150
	 * @deprecated
151
	 * @since future
152
	 *
153
	 * @return mixed The value of the requested form data.
154
	 */
155
	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...
156
		return $this->form[$offset];
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...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
157
	}
158
159
	/**
160
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
161
	 *
162
	 * @internal
163
	 * @deprecated
164
	 * @since future
165
	 *
166
	 * @return void
167
	 */
168
	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...
169
		gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
170
	}
171
172
	/**
173
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
174
	 *
175
	 * @internal
176
	 * @deprecated
177
	 * @since future
178
	 * @return void
179
	 */
180
	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...
181
		gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
182
	}
183
}
184