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

GF_Form::offsetGet()   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 0
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
	 * @throws \RuntimeException if the Gravity Forms plugin is not active.
27
	 */
28 2
	private function __construct() {
29 2
		if ( ! class_exists( 'GFAPI' ) ) {
30
			throw new \RuntimeException( 'Gravity Forms plugin not active.' );
31
		}
32 2
	}
33
34
	/**
35
	 * Construct a \GV\GF_Form instance by ID.
36
	 *
37
	 * @param int|string $form_id The internal form ID.
38
	 *
39
	 * @api
40
	 * @since future
41
	 * @return \GV\GF_Form|null An instance of this form or null if not found.
42
	 */
43 3
	public static function by_id( $form_id ) {
44 3
		$form = \GFAPI::get_form( $form_id );
45 3
		if ( !$form ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
46 1
			return null;
47
		}
48
49 3
		$self = new self();
50 3
		$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...
51
52 3
		$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...
53
54 3
		return $self;
55
	}
56
57
	/**
58
	 * Get all entries for this form.
59
	 *
60
	 * @api
61
	 * @since future
62
	 *
63
	 * @return \GV\Entry_Collection The \GV\Entry_Collection
64
	 */
65
	public function get_entries() {
66
		$entries = new \GV\Entry_Collection();
67
68
		$form = &$this;
69
70
		/** Add the fetcher lazy callback. */
71
		$entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) {
72
			$entries = new \GV\Entry_Collection();
73
74
			$search_criteria = array();
75
			$sorting = array();
76
			$paging = array();
77
78
			/** Apply the filters */
79
			foreach ( $filters as $filter ) {
80
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
81
			}
82
83
			/** Apply the sorts */
84
			foreach ( $sorts as $sort ) {
85
				/** Gravity Forms does not have multi-sorting, so just overwrite. */
86
				$sorting = array(
87
					'key' => $sort->field->ID,
88
					'direction' => $sort->direction,
89
				);
90
			}
91
92
			/** The offset and limit */
93
			if ( ! empty( $offset->limit ) ) {
94
				$paging['page_size'] = $offset->limit;
95
			}
96
97
			if ( ! empty( $offset->offset ) ) {
98
				$paging['offset'] = $offset->offset;
99
			}
100
101
			foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) {
102
				$entries->add( \GV\GF_Entry::by_id( $entry['id'] ) );
103
			}
104
105
			return $entries;
106
		} );
107
108
		/** Add the counter lazy callback. */
109
		$entries->add_count_callback( function( $filters ) use ( $form ) {
110
111
			$search_criteria = array();
112
			$sorting = array();
113
114
			/** Apply the filters */
115
			foreach ( $filters as $filter ) {
116
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
117
			}
118
119
			return \GFAPI::count_entries( $form->ID, $search_criteria );
120
		} );
121
122
		return $entries;
123
	}
124
125
	/**
126
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
127
	 *
128
	 * @internal
129
	 * @deprecated
130
	 * @since future
131
	 * @return bool Whether the offset exists or not.
132
	 */
133
	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...
134
		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...
135
	}
136
137
	/**
138
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
139
	 *
140
	 * Maps the old keys to the new data;
141
	 *
142
	 * @internal
143
	 * @deprecated
144
	 * @since future
145
	 *
146
	 * @throws \RuntimeException during tests if called outside of whiteliested cases.
147
	 *
148
	 * @return mixed The value of the requested form data.
149
	 */
150
	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...
151
		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...
152
	}
153
154
	/**
155
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
156
	 *
157
	 * @internal
158
	 * @deprecated
159
	 * @since future
160
	 *
161
	 * @throws \RuntimeException The underlying form data is immutable.
162
	 *
163
	 * @return void
164
	 */
165
	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...
166
		throw new \RuntimeException( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
167
	}
168
169
	/**
170
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
171
	 *
172
	 * @internal
173
	 * @deprecated
174
	 * @since future
175
	 * @return void
176
	 */
177
	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...
178
		throw new \RuntimeException( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
179
	}
180
}
181