Passed
Push — master ( 7972a5...654cd1 )
by Christoph
11:53 queued 12s
created

LegacyProvider::getFileForComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
7
 *
8
 * @author Joas Schilling <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Comments\Search;
28
29
use OCP\Comments\IComment;
30
use OCP\Files\Folder;
31
use OCP\Files\Node;
32
use OCP\Files\NotFoundException;
33
use OCP\IUser;
34
use OCP\Search\Provider;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\Comments\Search\Provider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
35
use function count;
36
37
class LegacyProvider extends Provider {
0 ignored issues
show
Deprecated Code introduced by
The class OCP\Search\Provider has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

37
class LegacyProvider extends /** @scrutinizer ignore-deprecated */ Provider {
Loading history...
38
39
	/**
40
	 * Search for $query
41
	 *
42
	 * @param string $query
43
	 * @return array An array of OCP\Search\Result's
44
	 * @since 7.0.0
45
	 */
46
	public function search($query): array {
47
		$cm = \OC::$server->getCommentsManager();
48
		$us = \OC::$server->getUserSession();
49
50
		$user = $us->getUser();
51
		if (!$user instanceof IUser) {
0 ignored issues
show
introduced by
$user is always a sub-type of OCP\IUser.
Loading history...
52
			return [];
53
		}
54
		$uf = \OC::$server->getUserFolder($user->getUID());
55
56
		if ($uf === null) {
57
			return [];
58
		}
59
60
		$result = [];
61
		$numComments = 50;
62
		$offset = 0;
63
64
		while (count($result) < $numComments) {
65
			/** @var IComment[] $comments */
66
			$comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
67
68
			foreach ($comments as $comment) {
69
				if ($comment->getActorType() !== 'users') {
70
					continue;
71
				}
72
73
				$displayName = $cm->resolveDisplayName('user', $comment->getActorId());
74
75
				try {
76
					$file = $this->getFileForComment($uf, $comment);
77
					$result[] = new Result($query,
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Comments\Search\Result has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
					$result[] = /** @scrutinizer ignore-deprecated */ new Result($query,
Loading history...
78
						$comment,
79
						$displayName,
80
						$file->getPath()
81
					);
82
				} catch (NotFoundException $e) {
83
					continue;
84
				}
85
			}
86
87
			if (count($comments) < $numComments) {
88
				// Didn't find more comments when we tried to get, so there are no more comments.
89
				return $result;
90
			}
91
92
			$offset += $numComments;
93
			$numComments = 50 - count($result);
94
		}
95
96
		return $result;
97
	}
98
99
	/**
100
	 * @param Folder $userFolder
101
	 * @param IComment $comment
102
	 * @return Node
103
	 * @throws NotFoundException
104
	 */
105
	protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
106
		$nodes = $userFolder->getById((int) $comment->getObjectId());
107
		if (empty($nodes)) {
108
			throw new NotFoundException('File not found');
109
		}
110
111
		return array_shift($nodes);
112
	}
113
}
114