ManyToManyRelation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 31
c 3
b 0
f 0
dl 0
loc 107
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
use miBadger\ActiveRecord\SchemaBuilder;
9
10
Trait ManyToManyRelation
11
{
12
	// These variables are relevant for internal bookkeeping (constraint generation etc)
13
14
	/** @var string The name of the left column of the relation. */
15
	private $_leftColumnName;
16
17
	/** @var string The name of the right column of the relation. */
18
	private $_rightColumnName;
19
20
	/** @var string The name of the left table of the relation. */
21
	private $_leftEntityTable;
22
23
	/** @var string The name of the right table of the relation. */
24
	private $_rightEntityTable;
25
26
	/** @var \PDO The PDO object. */
27
	protected $pdo;
28
	/**
29
	 * Initializes the the ManyToManyRelation trait on the included object
30
	 * 
31
	 * @param AbstractActiveRecord $leftEntity The left entity of the relation
32
	 * @param int $leftVariable The reference to the variable where the id for the left entity will be stored
33
	 * @param AbstractActiveRecord $rightEntity The left entity of the relation
34
	 * @param int $leftVariable The reference to the variable where the id for the right entity will be stored
35
	 * @return void
36
	 */
37 3
	protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable)
38
	{
39 3
		$this->_leftEntityTable = $leftEntity->getTableName();
40 3
		$this->_rightEntityTable = $rightEntity->getTableName();
41
42 3
		if (get_class($leftEntity) === get_class($rightEntity)) {
43 3
			$this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getTableName());
44 3
			$this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getTableName());
45
		} else {
46
			$this->_leftColumnName = sprintf("id_%s", $leftEntity->getTableName());
47
			$this->_rightColumnName = sprintf("id_%s", $rightEntity->getTableName());
48
		}
49
50 3
		$this->extendTableDefinition($this->_leftColumnName, [
51 3
			'value' => &$leftVariable,
52
			'validate' => null,
53
			'type' => AbstractActiveRecord::COLUMN_TYPE_ID,
54 3
			'properties' => ColumnProperty::NOT_NULL
55
		]);
56
57 3
		$this->extendTableDefinition($this->_rightColumnName, [
58 3
			'value' => &$rightVariable,
59
			'validate' => null,
60
			'type' => AbstractActiveRecord::COLUMN_TYPE_ID,
61 3
			'properties' => ColumnProperty::NOT_NULL
62
		]);
63 3
	}
64
65
	/**
66
	 * Build the constraints for the many-to-many relation table
67
	 * @return void
68
	 */
69 3
	public function createTableConstraints()
70
	{
71 3
		$childTable = $this->getTableName();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $childTable is correct as $this->getTableName() targeting miBadger\ActiveRecord\Tr...elation::getTableName() 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...
72
73 3
		$leftParentTable = $this->_leftEntityTable;
74 3
		$rightParentTable = $this->_rightEntityTable;
75
76 3
		$leftConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($leftParentTable, 'id', $childTable, $this->_leftColumnName);
0 ignored issues
show
Bug introduced by
$childTable of type void is incompatible with the type string expected by parameter $childTable of miBadger\ActiveRecord\Sc...traintOnDeleteCascade(). ( Ignorable by Annotation )

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

76
		$leftConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($leftParentTable, 'id', /** @scrutinizer ignore-type */ $childTable, $this->_leftColumnName);
Loading history...
77 3
		$rightConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($rightParentTable, 'id', $childTable, $this->_rightColumnName);
78
79 3
		$this->pdo->query($leftConstraint);
80 3
		$this->pdo->query($rightConstraint);
81 3
	}
82
83
	/**
84
	 * @return void
85
	 */	
86
	abstract public function getTableName();
87
88
	/**
89
	 * @return void
90
	 */
91
	abstract protected function extendTableDefinition(string $columnName, $definition);
92
	
93
	/**
94
	 * @return void
95
	 */
96
	abstract protected function registerSearchHook(string $columnName, $fn);
97
98
	/**
99
	 * @return void
100
	 */
101
	abstract protected function registerDeleteHook(string $columnName, $fn);
102
103
	/**
104
	 * @return void
105
	 */
106
	abstract protected function registerUpdateHook(string $columnName, $fn);
107
108
	/**
109
	 * @return void
110
	 */
111
	abstract protected function registerReadHook(string $columnName, $fn);
112
113
	/**
114
	 * @return void
115
	 */
116
	abstract protected function registerCreateHook(string $columnName, $fn);
117
118
}
119