Many::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php 
2
namespace Maphper\Relation;
3
class Many implements \Maphper\Relation {
4
	private $mapper;
5
	private $parentField;
6
	private $localField;
7
	
8
	public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $critiera = []) {
9
		if ($critiera) $mapper->filter($critiera);
0 ignored issues
show
Bug introduced by
The method filter() does not exist on Maphper\Maphper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

9
		if ($critiera) $mapper->/** @scrutinizer ignore-call */ filter($critiera);
Loading history...
Bug Best Practice introduced by
The expression $critiera of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
10
		$this->mapper = $mapper;
11
		$this->parentField = $parentField;
12
		$this->localField = $localField;		
13
	}
14
	
15
	
16
	public function getData($parentObject) {
17
		if (!isset($parentObject->{$this->parentField})) $mapper = $this->mapper;
18
		else $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]);
19
		
20
		return $mapper;
21
	}
22
23
	
24
	public function overwrite($key, &$mapper) {
25
		if (!isset($key->{$this->parentField})) return false;
26
		foreach ($mapper as $k => $val) {
27
			if (!empty($val->{$this->localField}) && $val->{$this->localField} != $key->{$this->parentField}) continue;
28
			$val->{$this->localField} = $key->{$this->parentField};
29
			$this->mapper[] = $val;
30
		}
31
	}
32
33
34
}