Completed
Push — master ( edd944...d89c76 )
by Lukas
146:32 queued 124:11
created

ContactsMenuController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Christoph Wurst <[email protected]>
5
 *
6
 * @author 2017 Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Core\Controller;
26
27
use OC\Contacts\ContactsMenu\Manager;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\IRequest;
32
use OCP\IUserSession;
33
34
class ContactsMenuController extends Controller {
35
36
	/** @var Manager */
37
	private $manager;
38
39
	/** @var IUserSession */
40
	private $userSession;
41
42
	/**
43
	 * @param IRequest $request
44
	 * @param IUserSession $userSession
45
	 * @param Manager $manager
46
	 */
47
	public function __construct(IRequest $request, IUserSession $userSession, Manager $manager) {
48
		parent::__construct('core', $request);
49
		$this->userSession = $userSession;
50
		$this->manager = $manager;
51
	}
52
53
	/**
54
	 * @NoAdminRequired
55
	 *
56
	 * @param string|null filter
57
	 * @return JSONResponse
58
	 */
59
	public function index($filter = null) {
60
		return $this->manager->getEntries($this->userSession->getUser(), $filter);
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can be null; however, getEntries() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
61
	}
62
63
	/**
64
	 * @NoAdminRequired
65
	 *
66
	 * @param integer $shareType
67
	 * @param string $shareWith
68
	 * @return JSONResponse
69
	 */
70
	public function findOne($shareType, $shareWith) {
71
		$contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can be null; however, findOne() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
72
73
		if ($contact) {
74
			return $contact;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $contact; (OC\Contacts\ContactsMenu\Entry) is incompatible with the return type documented by OC\Core\Controller\ContactsMenuController::findOne of type OCP\AppFramework\Http\JSONResponse.

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...
75
		} else {
76
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
77
		}
78
	}
79
}
80