Completed
Pull Request — master (#586)
by Maxence
03:47
created

Report   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A setCircles() 0 5 1
A getCircles() 0 3 1
A setMembers() 0 5 1
A getMembers() 0 3 1
A import() 0 6 1
A jsonSerialize() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Model;
33
34
35
use daita\MySmallPhpTools\IDeserializable;
36
use daita\MySmallPhpTools\Traits\TArrayTools;
37
use JsonSerializable;
38
39
40
/**
41
 * Class Report
42
 *
43
 * @package OCA\Circles\Model
44
 */
45
class Report implements IDeserializable, JsonSerializable {
46
47
48
	use TArrayTools;
49
50
	/** @var Circle[] */
51
	private $circles = [];
52
53
	/** @var Member[] */
54
	private $members = [];
55
56
57
	/**
58
	 * Report constructor.
59
	 */
60
	public function __construct() {
61
	}
62
63
64
	/**
65
	 * @param Circle[] $circles
66
	 */
67
	public function setCircles(array $circles): self {
68
		$this->circles = $circles;
69
70
		return $this;
71
	}
72
73
	/**
74
	 * @return Circle[]
75
	 */
76
	public function getCircles(): array {
77
		return $this->circles;
78
	}
79
80
81
	/**
82
	 * @param Member[] $members
83
	 */
84
	public function setMembers(array $members): self {
85
		$this->members = $members;
86
87
		return $this;
88
	}
89
90
	/**
91
	 * @return Member[]
92
	 */
93
	public function getMembers(): array {
94
		return $this->members;
95
	}
96
97
98
	/**
99
	 * @param array $data
100
	 *
101
	 * @return IDeserializable
102
	 */
103
	public function import(array $data): IDeserializable {
104
		$this->setCircles($this->getArray('circles', $data));
105
		$this->setMembers($this->getArray('members', $data));
106
107
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (OCA\Circles\Model\Report) is incompatible with the return type declared by the interface daita\MySmallPhpTools\IDeserializable::import of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
108
	}
109
110
111
	/**
112
	 * @return array
113
	 */
114
	function jsonSerialize(): array {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
		return [
116
			'circles' => $this->getCircles(),
117
			'members' => $this->getMembers()
118
		];
119
	}
120
121
}
122
123