Completed
Push — master ( 7dd2dd...7e98b2 )
by Adam
03:24 queued 01:04
created

Resource   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 142
Duplicated Lines 9.86 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 26.67%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 14
loc 142
ccs 8
cts 30
cp 0.2667
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setParent() 0 4 1
A getParent() 0 4 1
A setChildren() 0 10 3
A addChild() 0 6 2
A getChildren() 14 14 2
A getResourceId() 0 4 1
A setName() 0 4 1
A getName() 0 4 1
A setComment() 0 4 1
A getComment() 0 4 1
A __toString() 0 4 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
 * IResource.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Permissions!
9
 * @subpackage     Entities
10
 * @since          2.0.0
11
 *
12
 * @date           30.11.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Permissions\Entities;
18
19
use Nette;
20
21 1
class Resource extends Nette\Object implements IResource
22
{
23
	/**
24
	 * @var string
25
	 */
26
	protected $id;
27
28
	/**
29
	 * @var IResource|NULL
30
	 */
31
	protected $parent;
32
33
	/**
34
	 * @var IResource[]
35
	 */
36
	protected $children;
37
38
	/**
39
	 * @var string|NULL
40
	 */
41
	protected $name;
42
43
	/**
44
	 * @var string|NULL
45
	 */
46
	protected $comment;
47
48
	public function __construct(string $id)
49
	{
50 1
		$this->id = $id;
51
52 1
		$this->children = new \SplObjectStorage();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplObjectStorage() of type object<SplObjectStorage> is incompatible with the declared type array<integer,object<IPu...ns\Entities\IResource>> of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53 1
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function setParent(IResource $parent = NULL)
59
	{
60
		$this->parent = $parent;
61
	}
62
63
	/**
64
	 * {@inheritdoc}
65
	 */
66
	public function getParent()
67
	{
68 1
		return $this->parent;
69
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public function setChildren(array $resources)
75
	{
76
		$this->children = new \SplObjectStorage();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplObjectStorage() of type object<SplObjectStorage> is incompatible with the declared type array<integer,object<IPu...ns\Entities\IResource>> of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
78
		foreach ($resources as $child) {
79
			if ($child instanceof IResource) {
80
				$this->children->attach($child);
81
			}
82
		}
83
	}
84
85
	/**
86
	 * {@inheritdoc}
87
	 */
88
	public function addChild(IResource $resource)
89
	{
90
		if (!$this->children->contains($resource)) {
0 ignored issues
show
Bug introduced by
The method contains cannot be called on $this->children (of type array<integer,object<IPu...ns\Entities\IResource>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
91
			$this->children->attach($resource);
0 ignored issues
show
Bug introduced by
The method attach cannot be called on $this->children (of type array<integer,object<IPu...ns\Entities\IResource>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
92
		}
93
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98 View Code Duplication
	public function getChildren()
0 ignored issues
show
Duplication introduced by
This method 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...
99
	{
100
		$children = [];
101
102
		$this->children->rewind();
0 ignored issues
show
Bug introduced by
The method rewind cannot be called on $this->children (of type array<integer,object<IPu...ns\Entities\IResource>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
103
104
		while ($this->children->valid())
0 ignored issues
show
Bug introduced by
The method valid cannot be called on $this->children (of type array<integer,object<IPu...ns\Entities\IResource>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
105
		{
106
			$children[] = $this->children->current();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->children (of type array<integer,object<IPu...ns\Entities\IResource>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
107
			$this->children->next();
0 ignored issues
show
Bug introduced by
The method next cannot be called on $this->children (of type array<integer,object<IPu...ns\Entities\IResource>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
108
		}
109
110
		return $children;
111
	}
112
113
	/**
114
	 * {@inheritdoc}
115
	 */
116
	public function getResourceId() : string
117
	{
118 1
		return $this->id;
119
	}
120
121
	/**
122
	 * {@inheritdoc}
123
	 */
124
	public function setName(string $name)
125
	{
126
		$this->name = $name;
127
	}
128
129
	/**
130
	 * {@inheritdoc}
131
	 */
132
	public function getName()
133
	{
134
		return $this->name;
135
	}
136
137
	/**
138
	 * {@inheritdoc}
139
	 */
140
	public function setComment(string $comment)
141
	{
142
		$this->comment = $comment;
143
	}
144
145
	/**
146
	 * {@inheritdoc}
147
	 */
148
	public function getComment()
149
	{
150
		return $this->comment;
151
	}
152
153
	/**
154
	 * Convert resource object to string
155
	 *
156
	 * @return string
157
	 */
158
	public function __toString()
159
	{
160 1
		return $this->id;
161
	}
162
}
163