Completed
Push — develop ( e6a1df...3f5fef )
by Zack
04:16
created

Join::as_query_join()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.1158

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 5
cts 6
cp 0.8333
crap 5.1158
rs 8.5125
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
	 * Construct a JOIN container.
17
	 *
18
	 * @param \GV\Source $join The form we're joining to.
19
	 * @param \GV\Field $join_column Its column.
20
	 * @param \GV\Source $join_on The form we're joining on.
21
	 * @param \GV\Field $join_on_column Its column.
22
	 *
23
	 * @return \GV\Joins $this
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
24
	 */
25 4
	public function __construct( $join, $join_column, $join_on, $join_on_column ) {
26 4
		if ( $join instanceof \GV\Source ) {
27 4
			$this->join = $join;
0 ignored issues
show
Bug introduced by
The property join does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
		}
29
30 4
		if ( $join_on instanceof \GV\Source ) {
31 4
			$this->join_on = $join_on;
0 ignored issues
show
Bug introduced by
The property join_on does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
		}
33
34 4
		if ( $join_column instanceof \GV\Field ) {
35 4
			$this->join_column = $join_column;
0 ignored issues
show
Bug introduced by
The property join_column does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
		}
37
38 4
		if ( $join_on_column instanceof \GV\Field ) {
39 4
			$this->join_on_column = $join_on_column;
0 ignored issues
show
Bug introduced by
The property join_on_column does not seem to exist. Did you mean join_on?

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...
40
		}
41 4
	}
42
43
	/**
44
	 * Inject this join into the query.
45
	 *
46
	 * @param \GF_Query $query The \GF_Query instance.
47
	 *
48
	 * @return \GF_Query The $query
49
	 */
50 4
	public function as_query_join( $query ) {
51 4
52
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
53
			return null;
54
		}
55 4
56 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...
57 4
			gravityview()->log->error( 'Query not instance of \GF_Query.' );
58
			return null;
59
		}
60
61
		$join_id    = intval( $this->join->ID );
62
		$join_on_id = intval( $this->join_on->ID );
63
64
		if ( empty( $join_id ) || empty( $join_on_id ) ) {
65
			gravityview()->log->error( 'Query join form not an integer.', array( 'data' => $this ) );
66
			return null;
67
		}
68
69
		return $query->join(
70
			new \GF_Query_Column( $this->join_on_column->ID, $join_on_id ),
0 ignored issues
show
Bug introduced by
The property join_on_column does not seem to exist. Did you mean join_on?

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...
71
			new \GF_Query_Column( $this->join_column->ID, $join_id )
72
		);
73
	}
74
}
75