Test Failed
Pull Request — main (#64)
by Lode
08:15
created

MetaObject::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace alsvanzelf\jsonapi\objects;
4
5
use alsvanzelf\jsonapi\helpers\AtMemberManager;
6
use alsvanzelf\jsonapi\helpers\Converter;
7
use alsvanzelf\jsonapi\helpers\Validator;
8
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
9
10 View Code Duplication
class MetaObject implements ObjectInterface {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
Missing doc comment for class MetaObject
Loading history...
11
	use AtMemberManager;
12
	
13
	/** @var array */
14
	protected $meta = [];
15
	
16
	/**
17
	 * human api
18
	 */
19
	
20
	/**
21
	 * @param  array $meta
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
22
	 * @return MetaObject
23
	 */
24
	public static function fromArray(array $meta) {
25
		$metaObject = new self();
26
		
27
		foreach ($meta as $key => $value) {
28
			$metaObject->add($key, $value);
29
		}
30
		
31
		return $metaObject;
32
	}
33
	
34
	/**
35
	 * @param  object $meta
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
	 * @return MetaObject
37
	 */
38
	public static function fromObject($meta) {
39
		$array = Converter::objectToArray($meta);
40
		
41
		return self::fromArray($array);
42
	}
43
	
44
	/**
45
	 * spec api
46
	 */
47
	
48
	/**
49
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
	 * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
	public function add($key, $value) {
53
		Validator::checkMemberName($key);
54
		
55
		if (is_object($value)) {
56
			$value = Converter::objectToArray($value);
57
		}
58
		
59
		$this->meta[$key] = $value;
60
	}
61
	
62
	/**
63
	 * ObjectInterface
64
	 */
65
	
66
	/**
67
	 * @inheritDoc
68
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
	public function isEmpty() {
70
		return ($this->meta === [] && $this->hasAtMembers() === false);
71
	}
72
	
73
	/**
74
	 * @inheritDoc
75
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
	public function toArray() {
77
		return array_merge($this->getAtMembers(), $this->meta);
78
	}
79
}
80