Skip to content

Commit 8853bac

Browse files
committed
TECH Get rid of ambiguous constructions
1 parent 7accbec commit 8853bac

17 files changed

+107
-40
lines changed

psalm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<psalm
44
errorLevel="3"
55
resolveFromConfigFile="true"
6+
findUnusedBaselineEntry="true"
7+
findUnusedCode="false"
68
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
79
xmlns="https://getpsalm.org/schema/config"
810
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"

src/Api/Client.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ public function request($request, int $mode = self::RESPONSE_SHORT): XmlResponse
167167
}
168168

169169
if ('sdk' == $this->protocol) {
170-
$version = ('' == $this->version) ? null : $this->version;
171-
$requestXml = new SimpleXMLElement((string) $request);
172-
/** @psalm-suppress UndefinedClass */
173-
$xml = \pm_ApiRpc::getService($version)->call($requestXml->children()[0]->asXml(), $this->login);
170+
$xml = $this->performSdkCall((string) $request);
174171
} else {
175172
$xml = $this->performHttpRequest((string) $request);
176173
}
@@ -179,8 +176,25 @@ public function request($request, int $mode = self::RESPONSE_SHORT): XmlResponse
179176
? call_user_func($this->verifyResponseCallback, $xml)
180177
: $this->verifyResponse($xml);
181178

182-
$result = (self::RESPONSE_FULL === $mode) ? $xml : $xml->xpath('//result')[0];
183-
return new XmlResponse((string) $result->asXML());
179+
$result = (self::RESPONSE_FULL === $mode)
180+
? $xml
181+
: ($xml->xpath('//result') ?: [null])[0];
182+
183+
return new XmlResponse($result ? (string) $result->asXML() : '');
184+
}
185+
186+
private function performSdkCall(string $request): XmlResponse
187+
{
188+
$version = ('' == $this->version) ? null : $this->version;
189+
190+
$requestXml = new SimpleXMLElement($request);
191+
$innerNodes = $requestXml->children();
192+
$innerXml = $innerNodes && count($innerNodes) > 0 && $innerNodes[0] ? $innerNodes[0]->asXml() : '';
193+
194+
/** @psalm-suppress UndefinedClass */
195+
$result = \pm_ApiRpc::getService($version)->call($innerXml, $this->login);
196+
197+
return new XmlResponse($result ? (string) $result->asXML() : '');
184198
}
185199

186200
/**
@@ -265,7 +279,12 @@ private function splitResponseToArray(XmlResponse $responseXml, $mode = self::RE
265279
{
266280
$responses = [];
267281

268-
foreach ($responseXml->children() as $childNode) {
282+
$nodes = $responseXml->children();
283+
if (!$nodes) {
284+
return [];
285+
}
286+
287+
foreach ($nodes as $childNode) {
269288
$dom = $this->getDomDocument($this->getPacket());
270289
if (!$dom) {
271290
continue;
@@ -278,7 +297,13 @@ private function splitResponseToArray(XmlResponse $responseXml, $mode = self::RE
278297
}
279298

280299
$response = simplexml_load_string($dom->saveXML());
281-
$responses[] = (self::RESPONSE_FULL == $mode) ? $response : $response->xpath('//result')[0];
300+
if (!$response) {
301+
return [];
302+
}
303+
304+
$responses[] = (self::RESPONSE_FULL == $mode)
305+
? $response
306+
: ($response->xpath('//result') ?: [null])[0];
282307
}
283308

284309
return $responses;
@@ -330,8 +355,8 @@ private function verifyResponse($xml): void
330355
}
331356

332357
if ($xml->xpath('//status[text()="error"]') && $xml->xpath('//errcode') && $xml->xpath('//errtext')) {
333-
$errorCode = (int) $xml->xpath('//errcode')[0];
334-
$errorMessage = (string) $xml->xpath('//errtext')[0];
358+
$errorCode = (int) ($xml->xpath('//errcode') ?: [null])[0];
359+
$errorMessage = (string) ($xml->xpath('//errtext') ?: [null])[0];
335360

336361
throw new Exception($errorMessage, $errorCode);
337362
}

src/Api/Operator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ protected function getItems($structClass, $infoTag, $field = null, $value = null
8686
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
8787

8888
$items = [];
89-
foreach ($response->xpath('//result') as $xmlResult) {
90-
if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) {
89+
foreach ((array) $response->xpath('//result') as $xmlResult) {
90+
if (!$xmlResult || !isset($xmlResult->data) || !isset($xmlResult->data->$infoTag)) {
9191
continue;
9292
}
93-
if (!isset($xmlResult->data) || !isset($xmlResult->data->$infoTag)) {
93+
if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) {
9494
continue;
9595
}
9696
/** @psalm-suppress InvalidStringClass */

src/Api/Operator/Database.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ public function getAll(string $field, $value): array
7777
{
7878
$response = $this->getBy('get-db', $field, $value);
7979
$items = [];
80-
foreach ($response->xpath('//result') as $xmlResult) {
81-
$items[] = new Struct\Info($xmlResult);
80+
foreach ((array) $response->xpath('//result') as $xmlResult) {
81+
if ($xmlResult) {
82+
$items[] = new Struct\Info($xmlResult);
83+
}
8284
}
8385

8486
return $items;
@@ -94,8 +96,10 @@ public function getAllUsers(string $field, $value): array
9496
{
9597
$response = $this->getBy('get-db-users', $field, $value);
9698
$items = [];
97-
foreach ($response->xpath('//result') as $xmlResult) {
98-
$items[] = new Struct\UserInfo($xmlResult);
99+
foreach ((array) $response->xpath('//result') as $xmlResult) {
100+
if ($xmlResult) {
101+
$items[] = new Struct\UserInfo($xmlResult);
102+
}
99103
}
100104

101105
return $items;

src/Api/Operator/DatabaseServer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ private function getBy($field = null, $value = null): array
5656
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
5757

5858
$items = [];
59-
foreach ($response->xpath('//result') as $xmlResult) {
59+
foreach ((array) $response->xpath('//result') as $xmlResult) {
60+
if (!$xmlResult) {
61+
continue;
62+
}
6063
$item = new Struct\Info($xmlResult->data);
6164
$item->id = (int) $xmlResult->id;
6265
$items[] = $item;

src/Api/Operator/Dns.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ public function bulkCreate(array $records): array
4040

4141
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
4242
$items = [];
43-
foreach ($response->xpath('//result') as $xmlResult) {
44-
$items[] = $xmlResult;
43+
foreach ((array) $response->xpath('//result') as $xmlResult) {
44+
if ($xmlResult) {
45+
$items[] = $xmlResult;
46+
}
4547
}
4648

4749
return $items;
@@ -76,7 +78,10 @@ public function getAll(string $field, $value): array
7678

7779
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
7880
$items = [];
79-
foreach ($response->xpath('//result') as $xmlResult) {
81+
foreach ((array) $response->xpath('//result') as $xmlResult) {
82+
if (!$xmlResult) {
83+
continue;
84+
}
8085
$item = new Struct\Info($xmlResult->data);
8186
$item->id = (int) $xmlResult->id;
8287
$items[] = $item;
@@ -114,8 +119,10 @@ public function bulkDelete(array $recordIds): array
114119

115120
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
116121
$items = [];
117-
foreach ($response->xpath('//result') as $xmlResult) {
118-
$items[] = $xmlResult;
122+
foreach ((array) $response->xpath('//result') as $xmlResult) {
123+
if ($xmlResult) {
124+
$items[] = $xmlResult;
125+
}
119126
}
120127

121128
return $items;

src/Api/Operator/DnsTemplate.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public function getAll($field = null, $value = null): array
5959

6060
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
6161
$items = [];
62-
foreach ($response->xpath('//result') as $xmlResult) {
62+
foreach ((array) $response->xpath('//result') as $xmlResult) {
63+
if (!$xmlResult) {
64+
continue;
65+
}
6366
$item = new Struct\Info($xmlResult->data);
6467
$item->id = (int) $xmlResult->id;
6568
$items[] = $item;

src/Api/Operator/Mail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function getAll(int $siteId, $name = null): array
7676

7777
$response = $this->client->request($packet, Client::RESPONSE_FULL);
7878
$items = [];
79-
foreach ($response->xpath('//result') as $xmlResult) {
80-
if (!isset($xmlResult->mailname)) {
79+
foreach ((array) $response->xpath('//result') as $xmlResult) {
80+
if (!$xmlResult || !isset($xmlResult->mailname)) {
8181
continue;
8282
}
8383
$item = new Struct\GeneralInfo($xmlResult->mailname);

src/Api/Operator/PhpHandler.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PhpHandler extends Operator
1515
*
1616
* @return Info
1717
*/
18-
public function get($field = null, $value = null): Info
18+
public function get($field = null, $value = null): ?Info
1919
{
2020
$packet = $this->client->getPacket();
2121
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
@@ -26,9 +26,9 @@ public function get($field = null, $value = null): Info
2626
}
2727

2828
$response = $this->client->request($packet, Client::RESPONSE_FULL);
29-
$xmlResult = $response->xpath('//result')[0];
29+
$xmlResult = ($response->xpath('//result') ?: [null])[0];
3030

31-
return new Info($xmlResult);
31+
return $xmlResult ? new Info($xmlResult) : null;
3232
}
3333

3434
/**
@@ -49,7 +49,10 @@ public function getAll($field = null, $value = null): array
4949

5050
$response = $this->client->request($packet, Client::RESPONSE_FULL);
5151
$items = [];
52-
foreach ($response->xpath('//result') as $xmlResult) {
52+
foreach ((array) $response->xpath('//result') as $xmlResult) {
53+
if (!$xmlResult) {
54+
continue;
55+
}
5356
$item = new Info($xmlResult);
5457
$items[] = $item;
5558
}

src/Api/Operator/ProtectedDirectory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public function getAll(string $field, $value): array
5757
{
5858
$response = $this->getBy('get', $field, $value);
5959
$items = [];
60-
foreach ($response->xpath('//result/data') as $xmlResult) {
60+
foreach ((array) $response->xpath('//result/data') as $xmlResult) {
61+
if (!$xmlResult) {
62+
continue;
63+
}
6164
$items[] = new Struct\DataInfo($xmlResult);
6265
}
6366

0 commit comments

Comments
 (0)