Completed
Pull Request — master (#586)
by Maxence
03:15 queued 15s
created

Report::import()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 string */
51
	private $source = '';
52
53
	/** @var Circle[] */
54
	private $circles = [];
55
56
	/** @var array */
57
	private $obfuscated = [];
58
59
60
	/**
61
	 * Report constructor.
62
	 */
63
	public function __construct() {
64
	}
65
66
67
	/**
68
	 * @param string $source
69
	 *
70
	 * @return Report
71
	 */
72
	public function setSource(string $source): self {
73
		$this->source = $source;
74
75
		return $this;
76
	}
77
78
	/**
79
	 * @return string
80
	 */
81
	public function getSource(): string {
82
		return $this->source;
83
	}
84
85
86
	/**
87
	 * @param Circle[] $circles
88
	 *
89
	 * @return $this
90
	 */
91
	public function setCircles(array $circles): self {
92
		$this->circles = $circles;
93
94
		return $this;
95
	}
96
97
	/**
98
	 * @return Circle[]
99
	 */
100
	public function getCircles(): array {
101
		return $this->circles;
102
	}
103
104
105
	/**
106
	 * @param array $obfuscated
107
	 *
108
	 * @return $this
109
	 */
110
	public function setObfuscated(array $obfuscated): self {
111
		$this->obfuscated = $obfuscated;
112
113
		return $this;
114
	}
115
116
	/**
117
	 * @return array
118
	 */
119
	public function getObfuscated(): array {
120
		return $this->obfuscated;
121
	}
122
123
124
	/**
125
	 * @param array $data
126
	 *
127
	 * @return IDeserializable
128
	 */
129
	public function import(array $data): IDeserializable {
130
		$this->setSource($this->get('source', $data));
131
		$this->setCircles($this->getArray('circles', $data));
132
		$this->setObfuscated($this->getArray('obfuscated', $data));
133
134
		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...
135
	}
136
137
138
	/**
139
	 * @return array
140
	 */
141
	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...
142
		return [
143
			'source'     => $this->getSource(),
144
			'circles'    => $this->getCircles(),
145
			'obfuscated' => $this->getObfuscated()
146
		];
147
	}
148
149
}
150
151