Completed
Push — v2 ( 7e0bb1...9c04df )
by Berend
09:41
created

ManyToManyRelation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 3
eloc 31
dl 0
loc 112
c 0
b 0
f 0
ccs 22
cts 24
cp 0.9167
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createTableConstraints() 0 12 1
A initManyToManyRelation() 0 25 2
1
<?php
2
3
namespace miBadger\ActiveRecord\Traits;
4
5
use miBadger\Query\Query;
6
use miBadger\ActiveRecord\ColumnProperty;
7
use miBadger\ActiveRecord\AbstractActiveRecord;
8
9
Trait ManyToManyRelation
10
{
11
	// These variables are relevant for internal bookkeeping (constraint generation etc)
12
13
	/** @var string The name of the left column of the relation. */
14
	private $_leftColumnName;
15
16
	/** @var string The name of the right column of the relation. */
17
	private $_rightColumnName;
18
19
	/** @var string The name of the left table of the relation. */
20
	private $_leftEntityTable;
21
22
	/** @var string The name of the right table of the relation. */
23
	private $_rightEntityTable;
24
25
	/** @var \PDO The PDO object. */
26
	protected $pdo;
27
	/**
28
	 * Initializes the the ManyToManyRelation trait on the included object
29
	 * 
30
	 * @param AbstractActiveRecord $leftEntity The left entity of the relation
31
	 * @param int $leftVariable The reference to the variable where the id for the left entity will be stored
32
	 * @param AbstractActiveRecord $rightEntity The left entity of the relation
33
	 * @param int $leftVariable The reference to the variable where the id for the right entity will be stored
34
	 * @return void
35
	 */
36 3
	protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable)
37
	{
38 3
		$this->_leftEntityTable = $leftEntity->getActiveRecordTable();
39 3
		$this->_rightEntityTable = $rightEntity->getActiveRecordTable();
40
41 3
		if (get_class($leftEntity) === get_class($rightEntity)) {
42 3
			$this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable());
43 3
			$this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable());
44
		} else {
45
			$this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable());
46
			$this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable());
47
		}
48
49 3
		$this->extendTableDefinition($this->_leftColumnName, [
50 3
			'value' => &$leftVariable,
51
			'validate' => null,
52
			'type' => AbstractActiveRecord::COLUMN_TYPE_ID,
53 3
			'properties' => ColumnProperty::NOT_NULL
54
		]);
55
56 3
		$this->extendTableDefinition($this->_rightColumnName, [
57 3
			'value' => &$rightVariable,
58
			'validate' => null,
59
			'type' => AbstractActiveRecord::COLUMN_TYPE_ID,
60 3
			'properties' => ColumnProperty::NOT_NULL
61
		]);
62 3
	}
63
64
	/**
65
	 * Build the constraints for the many-to-many relation table
66
	 * @return void
67
	 */
68 3
	public function createTableConstraints()
69
	{
70 3
		$childTable = $this->getActiveRecordTable();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $childTable is correct as $this->getActiveRecordTable() targeting miBadger\ActiveRecord\Tr...:getActiveRecordTable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
72 3
		$leftParentTable = $this->_leftEntityTable;
73 3
		$rightParentTable = $this->_rightEntityTable;
74
75 3
		$leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $leftConstraint is correct as $this->buildConstraint($...$this->_leftColumnName) targeting miBadger\ActiveRecord\Tr...tion::buildConstraint() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76 3
		$rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $rightConstraint is correct as $this->buildConstraint($...this->_rightColumnName) targeting miBadger\ActiveRecord\Tr...tion::buildConstraint() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
77
78 3
		$this->pdo->query($leftConstraint);
0 ignored issues
show
Bug introduced by
$leftConstraint of type void is incompatible with the type string expected by parameter $statement of PDO::query(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
		$this->pdo->query(/** @scrutinizer ignore-type */ $leftConstraint);
Loading history...
79 3
		$this->pdo->query($rightConstraint);
80 3
	}
81
82
	/**
83
	 * @return void
84
	 */	
85
	abstract function getActiveRecordTable();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
86
87
	/**
88
	 * @return void
89
	 */
90
	abstract function buildConstraint($parentTable, $parentColumn, $childTable, $childColumn);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
92
	/**
93
	 * @return void
94
	 */
95
	abstract function extendTableDefinition($columnName, $definition);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
	
97
	/**
98
	 * @return void
99
	 */
100
	abstract function registerSearchHook($columnName, $fn);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101
102
	/**
103
	 * @return void
104
	 */
105
	abstract function registerDeleteHook($columnName, $fn);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
107
	/**
108
	 * @return void
109
	 */
110
	abstract function registerUpdateHook($columnName, $fn);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
111
112
	/**
113
	 * @return void
114
	 */
115
	abstract function registerReadHook($columnName, $fn);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
117
	/**
118
	 * @return void
119
	 */
120
	abstract function registerCreateHook($columnName, $fn);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
122
}
123