Completed
Push — master ( 65e3b0...b26a8a )
by
unknown
31:46 queued 19:29
created

RootCollection   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getChildForPrincipal() 0 5 1
A getName() 0 3 1
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OCA\DAV\JobStatus;
22
23
use OCA\DAV\JobStatus\Entity\JobStatusMapper;
24
use Sabre\DAVACL\AbstractPrincipalCollection;
25
26
class RootCollection extends AbstractPrincipalCollection {
27
28
	/**
29
	 * @inheritdoc
30
	 */
31
	public function getChildForPrincipal(array $principalInfo) {
32
		/** @var JobStatusMapper $mapper */
33
		$mapper = \OC::$server->query(JobStatusMapper::class);
34
		return new Home($principalInfo, $mapper);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCA\DAV\JobS...rincipalInfo, $mapper); (OCA\DAV\JobStatus\Home) is incompatible with the return type declared by the abstract method Sabre\DAVACL\AbstractPri...n::getChildForPrincipal of type Sabre\DAVACL\IPrincipal.

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...
35
	}
36
37
	/**
38
	 * @inheritdoc
39
	 */
40
	public function getName() {
41
		return 'job-status';
42
	}
43
}
44