Completed
Push — master ( b11340...af41d1 )
by Zack
39:23 queued 35:10
created

Join   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 79.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 80
ccs 19
cts 24
cp 0.7917
rs 10
wmc 10
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 5
B as_query_join() 0 24 5
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 \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
	 */
19
	private $join;
20
21
	/**
22
	 * @var GF_Form|Source|Form
23
	 */
24
	private $join_on;
25
26
	/**
27
	 * @var Field
28
	 */
29
	private $join_column;
30
31
	/**
32
	 * @var Field
33
	 */
34
	private $join_on_column;
35
36
	/**
37
	 * Construct a JOIN container.
38
	 *
39
	 * @param \GV\Source $join The form we're joining to.
40
	 * @param \GV\Field $join_column Its column.
41
	 * @param \GV\Source $join_on The form we're joining on.
42
	 * @param \GV\Field $join_on_column Its column.
43
	 */
44 4
	public function __construct( $join, $join_column, $join_on, $join_on_column ) {
45 4
		if ( $join instanceof \GV\Source ) {
46 4
			$this->join = $join;
47
		}
48
49 4
		if ( $join_on instanceof \GV\Source ) {
50 4
			$this->join_on = $join_on;
51
		}
52
53 4
		if ( $join_column instanceof \GV\Field ) {
54 4
			$this->join_column = $join_column;
55
		}
56
57 4
		if ( $join_on_column instanceof \GV\Field ) {
58 4
			$this->join_on_column = $join_on_column;
59
		}
60 4
	}
61
62
	/**
63
	 * Inject this join into the query.
64
	 *
65
	 * @param \GF_Query $query The \GF_Query instance.
66
	 *
67
	 * @return \GF_Query The $query
68
	 */
69 4
	public function as_query_join( $query ) {
70
71 4
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
72
			return null;
73
		}
74
75 4
		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...
76
			gravityview()->log->error( 'Query not instance of \GF_Query.' );
77
			return null;
78
		}
79
80 4
		$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...
81 4
		$join_on_id = intval( $this->join_on->ID );
82
83 4
		if ( empty( $join_id ) || empty( $join_on_id ) ) {
84
			gravityview()->log->error( 'Query join form not an integer.', array( 'data' => $this ) );
85
			return null;
86
		}
87
88 4
		return $query->join(
89 4
			new \GF_Query_Column( $this->join_on_column->ID, $join_on_id ),
90 4
			new \GF_Query_Column( $this->join_column->ID, $join_id )
91
		);
92
	}
93
}
94