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

AttributesObject   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 11 11 2
A fromObject() 5 5 1
A add() 9 9 2
A getKeys() 3 3 1
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 AttributesObject implements ObjectInterface {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class AttributesObject
Loading history...
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...
11
	use AtMemberManager;
12
	
13
	/** @var array */
14
	protected $attributes = [];
15
	
16
	/**
17
	 * human api
18
	 */
19
	
20
	/**
21
	 * @note if an `id` is set inside $attributes, it is removed from there
22
	 *       it is common to find it inside, and not doing so will cause an exception
23
	 * 
24
	 * @param  array $attributes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
25
	 * @return AttributesObject
26
	 */
27
	public static function fromArray(array $attributes) {
28
		unset($attributes['id']);
29
		
30
		$attributesObject = new self();
31
		
32
		foreach ($attributes as $key => $value) {
33
			$attributesObject->add($key, $value);
34
		}
35
		
36
		return $attributesObject;
37
	}
38
	
39
	/**
40
	 * @param  object $attributes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
	 * @return AttributesObject
42
	 */
43
	public static function fromObject($attributes) {
44
		$array = Converter::objectToArray($attributes);
45
		
46
		return self::fromArray($array);
47
	}
48
	
49
	/**
50
	 * spec api
51
	 */
52
	
53
	/**
54
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
	 * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
57
	public function add($key, $value) {
58
		Validator::checkMemberName($key);
59
		
60
		if (is_object($value)) {
61
			$value = Converter::objectToArray($value);
62
		}
63
		
64
		$this->attributes[$key] = $value;
65
	}
66
	
67
	/**
68
	 * internal api
69
	 */
70
	
71
	/**
72
	 * @internal
73
	 * 
74
	 * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<integer|string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
75
	 */
76
	public function getKeys() {
77
		return array_keys($this->attributes);
78
	}
79
	
80
	/**
81
	 * ObjectInterface
82
	 */
83
	
84
	/**
85
	 * @inheritDoc
86
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
	public function isEmpty() {
88
		return ($this->attributes === [] && $this->hasAtMembers() === false);
89
	}
90
	
91
	/**
92
	 * @inheritDoc
93
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
94
	public function toArray() {
95
		return array_merge($this->getAtMembers(), $this->attributes);
96
	}
97
}
98