Failed Conditions
Pull Request — master (#116)
by
unknown
01:25
created

lib/Db/ShareMapper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 *
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Db;
25
26
use OCA\NextNote\Service\NotebookService;
27
use \OCA\NextNote\Utility\Utils;
28
use OCP\AppFramework\Db\Entity;
29
use OCP\IDBConnection;
30
use OCP\AppFramework\Db\Mapper;
31
32
class ShareMapper extends Mapper {
33
	private $utils;
34
35
	public function __construct(IDBConnection $db, Utils $utils) {
36
		parent::__construct($db, 'nextnote_shares');
37
		$this->utils = $utils;
38
	}
39
40
41
	/**
42
	 * @param $share_id
43
	 * @param null $user_id
44
	 * @return Share|Share[]
45
	 */
46
	public function find($share_id, $user_id = null) {
47
		$qb = $this->db->getQueryBuilder();
48
		$qb->select('*')
49
			->from('nextnote_shares')
50
			->where($qb->expr()->eq('id', $qb->createNamedParameter($share_id)));
51
52
		if ($user_id) {
53
			$qb->andWhere($qb->expr()->eq('share_from', $qb->createNamedParameter($user_id)));
54
		}
55
56
		$results = [];
57
		$result = $qb->execute();
58
		while ($item = $result->fetch()) {
59
			/**
60
			 * @var $share Share
61
			 */
62
			$share = $this->makeEntityFromDBResult($item);
63
			$results[] = $share;
64
		}
65
		$result->closeCursor();
66
		if (count($results) === 1) {
67
			return reset($results);
68
		}
69
		return $results;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $results; (array) is incompatible with the return type documented by OCA\NextNote\Db\ShareMapper::find of type OCA\NextNote\Db\Share|OCA\NextNote\Db\Share[].

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...
70
71
	}
72
73
74
	/**
75
	 * @param $userId
76
	 * @return Share[] if not found
77
	 */
78
	public function findSharesFromUser($userId) {
79
		$qb = $this->db->getQueryBuilder();
80
		$qb->select('*')
81
			->from('nextnote_shares')
82
			->where($qb->expr()->eq('share_from', $qb->createNamedParameter($userId)));
83
84
85
		$results = [];
86
		$result = $qb->execute();
87
		while ($item = $result->fetch()) {
88
			/**
89
			 * @var $share Share
90
			 */
91
			$share = $this->makeEntityFromDBResult($item);
92
93
			$results[] = $share;
94
		}
95
		$result->closeCursor();
96
		return $results;
97
	}
98
99
100
	/**
101
	 * Creates a note
102
	 *
103
	 * @param Share|Entity $share
104
	 * @return Share|Entity
105
	 * @internal param $userId
106
	 */
107
	public function insert(Entity $share) {
108
		return parent::insert($share);
109
	}
110
111
	/**
112
	 * Update note
113
	 *
114
	 * @param Share|Entity $share
115
	 * @return Share|Entity
116
	 */
117
	public function updateNote(Entity $share) {
118
		parent::update($share);
119
		return $this->find($share->getId());
120
	}
121
122
	/**
123
	 * Update note
124
	 *
125
	 * @param Share|Entity $share
126
	 * @return Share|Entity
127
	 */
128
	public function delete(Entity $share) {
129
		parent::delete($share);
130
		return $this->find($share->getId());
131
	}
132
133
	public function makeEntityFromDBResult($arr) {
134
		$share = new Share();
135
		$share->setId($arr['id']);
136
		$share->setGuid($arr['guid']);
137
		$share->setShareFrom($arr['share_from']);
138
		$share->setShareTo($arr['share_to']);
139
		$share->setShareType($arr['share_type']);
140
		$share->setShareTarget($arr['share_target']);
141
		$share->setPermissions($arr['permissions']);
142
		$share->setExpireTime($arr['expire_time']);
143
144
		return $share;
145
	}
146
}
147