Passed
Push — v2 ( ee04d9...67b65f )
by Berend
02:48
created

ManyToManyRelation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 68
ccs 22
cts 24
cp 0.9167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initManyToManyRelation() 0 27 2
A createTableConstraints() 0 13 1
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
	/** @var string The name of the left column of the relation. */
13
	private $_leftColumnName;
14
15
	/** @var string The name of the right column of the relation. */
16
	private $_rightColumnName;
17
18
	/** @var string The name of the left table of the relation. */
19
	private $_leftEntityTable;
20
21
	/** @var string The name of the right table of the relation. */
22
	private $_rightEntityTable;
23
24
	/**
25
	 * Initializes the the ManyToManyRelation trait on the included object
26
	 * 
27
	 * @param AbstractActiveRecord $leftEntity The left entity of the relation
28
	 * @param &variable $leftVariable The variable where the id for the left entity will be stored
0 ignored issues
show
Documentation introduced by
The doc-type &variable could not be parsed: Unknown type name "&variable" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
	 * @param AbstractActiveRecord $rightEntity The left entity of the relation
30
	 * @param &variable $leftVariable The variable where the id for the right entity will be stored
0 ignored issues
show
Documentation introduced by
The doc-type &variable could not be parsed: Unknown type name "&variable" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
	 */
32 3
	protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable)
33
	{
34 3
		$this->_leftEntityTable = $leftEntity->getActiveRecordTable();
0 ignored issues
show
Bug introduced by
The method getActiveRecordTable() cannot be called from this context as it is declared protected in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
35 3
		$this->_rightEntityTable = $rightEntity->getActiveRecordTable();
0 ignored issues
show
Bug introduced by
The method getActiveRecordTable() cannot be called from this context as it is declared protected in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
36
37 3
		if (get_class($leftEntity) === get_class($rightEntity)) {
38 3
			$this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable());
0 ignored issues
show
Bug introduced by
The method getActiveRecordTable() cannot be called from this context as it is declared protected in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
39 3
			$this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable());
0 ignored issues
show
Bug introduced by
The method getActiveRecordTable() cannot be called from this context as it is declared protected in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
40
		} else {
41
			$this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable());
0 ignored issues
show
Bug introduced by
The method getActiveRecordTable() cannot be called from this context as it is declared protected in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
42
			$this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable());
0 ignored issues
show
Bug introduced by
The method getActiveRecordTable() cannot be called from this context as it is declared protected in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
43
		}
44
45 3
		$this->extendTableDefinition($this->_leftColumnName, [
0 ignored issues
show
Bug introduced by
It seems like extendTableDefinition() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46 3
			'value' => &$leftVariable,
47
			'validate' => null,
48
			'type' => AbstractActiveRecord::COLUMN_TYPE_ID,
49 3
			'properties' => ColumnProperty::NOT_NULL
50
		]);
51
52 3
		$this->extendTableDefinition($this->_rightColumnName, [
0 ignored issues
show
Bug introduced by
It seems like extendTableDefinition() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53 3
			'value' => &$rightVariable,
54
			'validate' => null,
55
			'type' => AbstractActiveRecord::COLUMN_TYPE_ID,
56 3
			'properties' => ColumnProperty::NOT_NULL
57
		]);
58 3
	}
59
60
	/**
61
	 * Build the constraints for the many-to-many relation table
62
	 */
63 3
	public function createTableConstraints()
64
	{
65 3
		$childTable = $this->getActiveRecordTable();
0 ignored issues
show
Bug introduced by
It seems like getActiveRecordTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
67 3
		$leftParentTable = $this->_leftEntityTable;
68 3
		$rightParentTable = $this->_rightEntityTable;
69
70 3
		$leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName);
0 ignored issues
show
Bug introduced by
It seems like buildConstraint() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71 3
		$rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName);
0 ignored issues
show
Bug introduced by
It seems like buildConstraint() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
73 3
		$this->pdo->query($leftConstraint);
0 ignored issues
show
Bug introduced by
The property pdo 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...
74 3
		$this->pdo->query($rightConstraint);
75 3
	}
76
}
77