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

MetaObject   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 70
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 9 9 2
A fromObject() 5 5 1
A add() 9 9 2
A isEmpty() 3 3 2
A toArray() 3 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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