Completed
Push — develop ( 82fa3b...268ce1 )
by Gennady
14:11
created

Join   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 10
cts 24
cp 0.4167
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 5
A as_query_join() 0 24 5
1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The \GV\Join class.
11
 *
12
 * Contains a join between two Sources on two Fields.
13
 */
14
class Join {
15
16
	/**
17
	 * @var GF_Form|Source|Form
18
	 * @since 2.2 Made private property public
19
	 */
20
	public $join;
21
22
	/**
23
	 * @var GF_Form|Source|Form
24
	 * @since 2.2 Made private property public
25
	 */
26
	public $join_on;
27
28
	/**
29
	 * @var Field
30
	 */
31
	public $join_column;
32
33
	/**
34
	 * @var Field
35
	 */
36
	public $join_on_column;
37
38
	/**
39
	 * Construct a JOIN container.
40
	 *
41
	 * @param \GV\Source $join The form we're joining to.
42
	 * @param \GV\Field $join_column Its column.
43
	 * @param \GV\Source $join_on The form we're joining on.
44
	 * @param \GV\Field $join_on_column Its column.
45
	 */
46 9
	public function __construct( $join, $join_column, $join_on, $join_on_column ) {
47 9
		if ( $join instanceof \GV\Source ) {
48 9
			$this->join = $join;
49
		}
50
51 9
		if ( $join_on instanceof \GV\Source ) {
52 9
			$this->join_on = $join_on;
53
		}
54
55 9
		if ( $join_column instanceof \GV\Field ) {
56 9
			$this->join_column = $join_column;
57
		}
58
59 9
		if ( $join_on_column instanceof \GV\Field ) {
60 9
			$this->join_on_column = $join_on_column;
61
		}
62 9
	}
63
64
	/**
65
	 * Inject this join into the query.
66
	 *
67
	 * @param \GF_Query $query The \GF_Query instance.
68
	 *
69
	 * @return \GF_Query The $query
70
	 */
71
	public function as_query_join( $query ) {
72
73
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
74
			return null;
75
		}
76
77
		if ( ! $query instanceof \GF_Query ) {
0 ignored issues
show
Bug introduced by
The class GF_Query does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
78
			gravityview()->log->error( 'Query not instance of \GF_Query.' );
79
			return null;
80
		}
81
82
		$join_id    = intval( $this->join->ID );
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in GV\Source.

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...
83
		$join_on_id = intval( $this->join_on->ID );
84
85
		if ( empty( $join_id ) || empty( $join_on_id ) ) {
86
			gravityview()->log->error( 'Query join form not an integer.', array( 'data' => $this ) );
87
			return null;
88
		}
89
90
		return $query->join(
91
			new \GF_Query_Column( $this->join_on_column->ID, $join_on_id ),
92
			new \GF_Query_Column( $this->join_column->ID, $join_id )
93
		);
94
	}
95
}
96