Completed
Push — develop ( df88cb...e66816 )
by Gennady
20:29 queued 01:32
created

Multi_Entry   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 56.41%

Importance

Changes 0
Metric Value
dl 0
loc 143
ccs 22
cts 39
cp 0.5641
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A from_entries() 0 10 3
A as_entry() 0 14 3
A get_permalink() 0 15 2
A offsetExists() 0 3 1
A offsetGet() 0 6 2
A offsetSet() 0 3 1
A offsetUnset() 0 3 1
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 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 multi-entry Entry implementation.
11
 *
12
 * An entry that is really a join of 2+ entries.
13
 * Used for JOINS in the \GF_Query component.
14
 */
15
class Multi_Entry extends Entry 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...
16
	/**
17
	 * The entries in this form.
18
	 */
19
	public $entries = array();
20
21
	/**
22
	 * @var string The identifier of the backend used for this entry.
23
	 * @api
24
	 * @since 2.0
25
	 */
26
	public static $backend = 'multi';
27
28
	/**
29
	 * Initialization.
30
	 */
31 5
	private function __construct() {
32 5
	}
33
34
	/**
35
	 * Construct a multientry from an array of entries.
36
	 *
37
	 * @param \GV\Entry[] $entries The entries.
38
	 *
39
	 * @return \GV\Multi_Entry A multientry object.
40
	 */
41 5
	public static function from_entries( $entries ) {
42 5
		$_entry = new self();
43 5
		foreach ( $entries as &$entry ) {
44 5
			if ( ! $entry instanceof Entry ) {
45
				continue;
46
			}
47 5
			$_entry->entries[ $entry['form_id'] ]  = &$entry;
48
		}
49 5
		return $_entry;
50
	}
51
52
	/**
53
	 * Fake legacy template support.
54
	 *
55
	 * Take the first entry and set it as the current entry.
56
	 * But support nesting.
57
	 *
58
	 * @return array See \GV\Entry::as_entry()
59
	 */
60 4
	public function as_entry() {
61 4
		$_entry = array();
62
63 4
		if ( $entry = reset( $this->entries ) ) {
64 4
			$_entry = $entry->as_entry();
65
66 4
			foreach ( $this->entries as $entry ) {
67 4
				$entry = $entry->as_entry();
68 4
				$_entry['_multi'][ $entry['form_id'] ] = $entry;
69
			}
70
		}
71
72 4
		return $_entry;
73
	}
74
75
	/**
76
	 * Return the link to this multi entry in the supplied context.
77
	 *
78
	 * @api
79
	 * @since 2.0
80
	 *
81
	 * @param \GV\View|null $view The View context.
82
	 * @param \GV\Request $request The Request (current if null).
83
	 * @param boolean $track_directory Keep the housing directory arguments intact (used for breadcrumbs, for example). Default: true.
84
	 *
85
	 * @return string The permalink to this entry.
86
	 */
87
	public function get_permalink( \GV\View $view = null, \GV\Request $request = null, $track_directory = true ) {
88
		$slugs = array();
89
		add_filter( 'gravityview/entry/slug', $callback = function( $slug ) use ( &$slugs ) {
90
			$slugs[] = $slug;
91
			return implode( ',', $slugs );
92
		}, 10, 1 );
93
94
		foreach ( $this->entries as $entry ) {
95
			$permalink = call_user_func_array( array( $entry, __FUNCTION__ ), func_get_args() );
96
		}
97
98
		remove_filter( 'gravityview/entry/slug', $callback );
99
100
		return $permalink;
0 ignored issues
show
Bug introduced by
The variable $permalink does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
101
	}
102
103
	/**
104
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
105
	 *
106
	 * @internal
107
	 * @deprecated
108
	 * @since 2.0
109
	 * @return bool Whether the offset exists or not.
110
	 */
111 3
	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...
112 3
		return isset( $this->entries[ $offset ] );
113
	}
114
115
	/**
116
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
117
	 *
118
	 * Maps the old keys to the new data;
119
	 *
120
	 * @internal
121
	 * @deprecated
122
	 * @since 2.0
123
	 *
124
	 * @return mixed The value of the requested entry data.
125
	 */
126 4
	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...
127 4
		if ( ! $this->offsetExists( $offset ) ) {
0 ignored issues
show
Deprecated Code introduced by
The method GV\Multi_Entry::offsetExists() has been deprecated.

This method has been deprecated.

Loading history...
128 1
			return null;
129
		}
130 4
		return $this->entries[ $offset ];
131
	}
132
133
	/**
134
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
135
	 *
136
	 * @internal
137
	 * @deprecated
138
	 * @since 2.0
139
	 *
140
	 * @return void
141
	 */
142
	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...
143
		gravityview()->log->error( 'The underlying multi entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
144
	}
145
146
	/**
147
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
148
	 *
149
	 * @internal
150
	 * @deprecated
151
	 * @since 2.0
152
	 * @return void
153
	 */
154
	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...
155
		gravityview()->log->error( 'The underlying multi entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
156
	}
157
}
158