Passed
Push — master ( e19bbc...21095b )
by Morris
22:29 queued 08:19
created

Php73::getSearchArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020 Arthur Schiwon <[email protected]>
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\User_LDAP\PagedResults;
29
30
/**
31
 * Class Php73
32
 *
33
 * implements paged results support with PHP APIs available from PHP 7.3
34
 *
35
 * @package OCA\User_LDAP\PagedResults
36
 */
37
class Php73 implements IAdapter {
38
	use TLinkId;
39
40
	/** @var array */
41
	protected $linkData = [];
42
43
	public function getResponseCallFunc(): string {
44
		return 'ldap_parse_result';
45
	}
46
47
	public function responseCall($link): bool {
48
		$linkId = $this->getLinkId($link);
49
		return ldap_parse_result(...$this->linkData[$linkId]['responseArgs']);
0 ignored issues
show
Bug introduced by
The call to ldap_parse_result() has too few arguments starting with result. ( Ignorable by Annotation )

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

49
		return /** @scrutinizer ignore-call */ ldap_parse_result(...$this->linkData[$linkId]['responseArgs']);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
50
	}
51
52
	public function getResponseCallArgs(array $originalArgs): array {
53
		$link = array_shift($originalArgs);
54
		$linkId = $this->getLinkId($link);
55
56
		if (!isset($this->linkData[$linkId])) {
57
			$this->linkData[$linkId] = [];
58
		}
59
60
		$this->linkData[$linkId]['responseErrorCode'] = 0;
61
		$this->linkData[$linkId]['responseErrorMessage'] = '';
62
		$this->linkData[$linkId]['serverControls'] = [];
63
		$matchedDn = null;
64
		$referrals = [];
65
66
		$this->linkData[$linkId]['responseArgs'] = [
67
			$link,
68
			array_shift($originalArgs),
69
			&$this->linkData[$linkId]['responseErrorCode'],
70
			$matchedDn,
71
			&$this->linkData[$linkId]['responseErrorMessage'],
72
			$referrals,
73
			&$this->linkData[$linkId]['serverControls']
74
		];
75
76
77
		return $this->linkData[$linkId]['responseArgs'];
78
	}
79
80
	public function getCookie($link): string {
81
		$linkId = $this->getLinkId($link);
82
		return $this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
0 ignored issues
show
Bug introduced by
The constant OCA\User_LDAP\PagedResul...AP_CONTROL_PAGEDRESULTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
83
	}
84
85
	private function resetCookie(int $linkId): void {
86
		if (isset($this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
0 ignored issues
show
Bug introduced by
The constant OCA\User_LDAP\PagedResul...AP_CONTROL_PAGEDRESULTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
87
			$this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] = '';
88
		}
89
	}
90
91
	public function getRequestCallFunc(): ?string {
92
		return null;
93
	}
94
95
	public function setRequestParameters($link, int $pageSize, bool $isCritical): void {
96
		$linkId = $this->getLinkId($link);
97
		if (!isset($this->linkData[$linkId])) {
98
			$this->linkData[$linkId] = [];
99
		}
100
		$this->linkData[$linkId]['requestArgs'] = [];
101
		$this->linkData[$linkId]['requestArgs']['pageSize'] = $pageSize;
102
		$this->linkData[$linkId]['requestArgs']['isCritical'] = $isCritical;
103
104
		if ($pageSize === 0) {
105
			$this->resetCookie($linkId);
106
		}
107
	}
108
109
	public function getRequestCallArgs($link): array {
110
		// no separate call
111
		return [];
112
	}
113
114
	public function requestCall($link): bool {
115
		// no separate call
116
		return false;
117
	}
118
119
	public function setSearchArgs(
120
		$link,
121
		string $baseDN,
122
		string $filter,
123
		array $attr,
124
		int $attrsOnly,
125
		int $limit
126
	): void {
127
		$linkId = $this->getLinkId($link);
128
		if (!isset($this->linkData[$linkId])) {
129
			$this->linkData[$linkId] = [];
130
		}
131
132
		$this->linkData[$linkId]['searchArgs'] = func_get_args();
133
		$this->preparePagesResultsArgs($linkId, 'searchArgs');
134
	}
135
136
	public function getSearchArgs($link): array {
137
		$linkId = $this->getLinkId($link);
138
		return $this->linkData[$linkId]['searchArgs'];
139
	}
140
141
	public function setReadArgs($link, string $baseDN, string $filter, array $attr): void {
142
		$linkId = $this->getLinkId($link);
143
		if (!isset($this->linkData[$linkId])) {
144
			$this->linkData[$linkId] = [];
145
		}
146
147
		$this->linkData[$linkId]['readArgs'] = func_get_args();
148
		$this->linkData[$linkId]['readArgs'][] = 0; // $attrsonly default
149
		$this->linkData[$linkId]['readArgs'][] = -1; // $sizelimit default
150
	}
151
152
	public function getReadArgs($link): array {
153
		$linkId = $this->getLinkId($link);
154
		return $this->linkData[$linkId]['readArgs'];
155
	}
156
157
	protected function preparePagesResultsArgs(int $linkId, string $methodKey): void {
158
		if (!isset($this->linkData[$linkId]['requestArgs'])) {
159
			return;
160
		}
161
162
		$serverControls = [[
163
			'oid' => LDAP_CONTROL_PAGEDRESULTS,
0 ignored issues
show
Bug introduced by
The constant OCA\User_LDAP\PagedResul...AP_CONTROL_PAGEDRESULTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
164
			'value' => [
165
				'size' => $this->linkData[$linkId]['requestArgs']['pageSize'],
166
				'cookie' => $this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '',
167
			]
168
		]];
169
170
		$this->linkData[$linkId][$methodKey][] = -1; // timelimit
171
		$this->linkData[$linkId][$methodKey][] = LDAP_DEREF_NEVER;
172
		$this->linkData[$linkId][$methodKey][] = $serverControls;
173
	}
174
}
175