Skip to content

Commit

Permalink
Fix issues reported by psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Oct 6, 2021
1 parent f226ce3 commit bdaad89
Show file tree
Hide file tree
Showing 71 changed files with 165 additions and 296 deletions.
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
168 changes: 47 additions & 121 deletions src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Client
protected array $_operatorsCache = [];

/**
* @var callable
* @var callable|null
*/
protected $_verifyResponseCallback;

Expand Down Expand Up @@ -169,7 +169,7 @@ public function request($request, $mode = self::RESPONSE_SHORT)
/** @psalm-suppress UndefinedClass */
$xml = \pm_ApiRpc::getService($version)->call($requestXml->children()[0]->asXml(), $this->_login);
} else {
$xml = $this->_performHttpRequest($request);
$xml = $this->_performHttpRequest((string) $request);
}

$this->_verifyResponseCallback
Expand Down Expand Up @@ -212,22 +212,19 @@ private function _performHttpRequest($request)

curl_close($curl);

$xml = new XmlResponse($result);

return $xml;
return new XmlResponse((string) $result);
}

/**
* Perform multiple API requests using single HTTP request.
*
* @param $requests
* @param array $requests
* @param int $mode
*
* @throws Client\Exception
*
* @return array
* @throws Client\Exception
*/
public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
public function multiRequest(array $requests, $mode = self::RESPONSE_SHORT): array
{
$requestXml = $this->getPacket();

Expand All @@ -237,23 +234,32 @@ public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
} else {
if (is_array($request)) {
$request = $this->_arrayToXml($request, $requestXml)->asXML();
if (!$request) {
throw new Client\Exception('Failed to create an XML string for request');
}
} elseif (preg_match('/^[a-z]/', $request)) {
$this->_expandRequestShortSyntax($request, $requestXml);
}
}
$responses[] = $this->request($request);
}

if ('sdk' == $this->_protocol) {
throw new Client\Exception('Multi requests are not supported via SDK.');
} else {
$responseXml = $this->_performHttpRequest($requestXml->asXML());
$xmlString = $requestXml->asXML();
if (!$xmlString) {
throw new Client\Exception('Failed to create an XML string for request');
}
$responseXml = $this->_performHttpRequest($xmlString);
}

$responses = [];
foreach ($responseXml->children() as $childNode) {
$xml = $this->getPacket();
$dom = dom_import_simplexml($xml)->ownerDocument;
if (!$dom) {
continue;
}

$childDomNode = dom_import_simplexml($childNode);
$childDomNode = $dom->importNode($childDomNode, true);
Expand Down Expand Up @@ -371,230 +377,150 @@ protected function _isAssocArray(array $array)
/**
* @param string $name
*
* @return \PleskX\Api\Operator
* @return mixed
*/
protected function _getOperator($name)
protected function _getOperator(string $name)
{
if (!isset($this->_operatorsCache[$name])) {
$className = '\\PleskX\\Api\\Operator\\'.$name;
/** @psalm-suppress InvalidStringClass */
$this->_operatorsCache[$name] = new $className($this);
}

return $this->_operatorsCache[$name];
}

/**
* @return Operator\Server
*/
public function server()
public function server(): Operator\Server
{
return $this->_getOperator('Server');
}

/**
* @return Operator\Customer
*/
public function customer()
public function customer(): Operator\Customer
{
return $this->_getOperator('Customer');
}

/**
* @return Operator\Webspace
*/
public function webspace()
public function webspace(): Operator\Webspace
{
return $this->_getOperator('Webspace');
}

/**
* @return Operator\Subdomain
*/
public function subdomain()
public function subdomain(): Operator\Subdomain
{
return $this->_getOperator('Subdomain');
}

/**
* @return Operator\Dns
*/
public function dns()
public function dns(): Operator\Dns
{
return $this->_getOperator('Dns');
}

/**
* @return Operator\DnsTemplate
*/
public function dnsTemplate()
public function dnsTemplate(): Operator\DnsTemplate
{
return $this->_getOperator('DnsTemplate');
}

/**
* @return Operator\DatabaseServer
*/
public function databaseServer()
public function databaseServer(): Operator\DatabaseServer
{
return $this->_getOperator('DatabaseServer');
}

/**
* @return Operator\Mail
*/
public function mail()
public function mail(): Operator\Mail
{
return $this->_getOperator('Mail');
}

/**
* @return Operator\Certificate
*/
public function certificate()
public function certificate(): Operator\Certificate
{
return $this->_getOperator('Certificate');
}

/**
* @return Operator\SiteAlias
*/
public function siteAlias()
public function siteAlias(): Operator\SiteAlias
{
return $this->_getOperator('SiteAlias');
}

/**
* @return Operator\Ip
*/
public function ip()
public function ip(): Operator\Ip
{
return $this->_getOperator('Ip');
}

/**
* @return Operator\EventLog
*/
public function eventLog()
public function eventLog(): Operator\EventLog
{
return $this->_getOperator('EventLog');
}

/**
* @return Operator\SecretKey
*/
public function secretKey()
public function secretKey(): Operator\SecretKey
{
return $this->_getOperator('SecretKey');
}

/**
* @return Operator\Ui
*/
public function ui()
public function ui(): Operator\Ui
{
return $this->_getOperator('Ui');
}

/**
* @return Operator\ServicePlan
*/
public function servicePlan()
public function servicePlan(): Operator\ServicePlan
{
return $this->_getOperator('ServicePlan');
}

/**
* @return Operator\VirtualDirectory
*/
public function virtualDirectory()
public function virtualDirectory(): Operator\VirtualDirectory
{
return $this->_getOperator('VirtualDirectory');
}

/**
* @return Operator\Database
*/
public function database()
public function database(): Operator\Database
{
return $this->_getOperator('Database');
}

/**
* @return Operator\Session
*/
public function session()
public function session(): Operator\Session
{
return $this->_getOperator('Session');
}

/**
* @return Operator\Locale
*/
public function locale()
public function locale(): Operator\Locale
{
return $this->_getOperator('Locale');
}

/**
* @return Operator\LogRotation
*/
public function logRotation()
public function logRotation(): Operator\LogRotation
{
return $this->_getOperator('LogRotation');
}

/**
* @return Operator\ProtectedDirectory
*/
public function protectedDirectory()
public function protectedDirectory(): Operator\ProtectedDirectory
{
return $this->_getOperator('ProtectedDirectory');
}

/**
* @return Operator\Reseller
*/
public function reseller()
public function reseller(): Operator\Reseller
{
return $this->_getOperator('Reseller');
}

/**
* @return Operator\ResellerPlan
*/
public function resellerPlan()
public function resellerPlan(): Operator\ResellerPlan
{
return $this->_getOperator('ResellerPlan');
}

/**
* @return Operator\Aps
*/
public function aps()
public function aps(): Operator\Aps
{
return $this->_getOperator('Aps');
}

/**
* @return Operator\ServicePlanAddon
*/
public function servicePlanAddon()
public function servicePlanAddon(): Operator\ServicePlanAddon
{
return $this->_getOperator('ServicePlanAddon');
}

/**
* @return Operator\Site
*/
public function site()
public function site(): Operator\Site
{
return $this->_getOperator('Site');
}

/**
* @return Operator\PhpHandler
*/
public function phpHandler()
public function phpHandler(): Operator\PhpHandler
{
return $this->_getOperator('PhpHandler');
}
Expand Down
5 changes: 3 additions & 2 deletions src/Api/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Operator
protected string $_wrapperTag = '';
protected Client $_client;

public function __construct($client)
public function __construct(Client $client)
{
$this->_client = $client;

Expand Down Expand Up @@ -78,7 +78,7 @@ protected function _getItems($structClass, $infoTag, $field = null, $value = nul

$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = $value;
$filterTag->{$field} = (string) $value;
}

$getTag->addChild('dataset')->addChild($infoTag);
Expand All @@ -93,6 +93,7 @@ protected function _getItems($structClass, $infoTag, $field = null, $value = nul
if (!isset($xmlResult->data) || !isset($xmlResult->data->$infoTag)) {
continue;
}
/** @psalm-suppress InvalidStringClass */
$item = new $structClass($xmlResult->data->$infoTag);
if (isset($xmlResult->id) && property_exists($item, 'id')) {
$item->id = (int) $xmlResult->id;
Expand Down
10 changes: 3 additions & 7 deletions src/Api/Operator/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,15 @@ public function getAllUsers($field, $value)
*
* @return \PleskX\Api\XmlResponse
*/
private function _get($command, $field, $value)
private function _get(string $command, string $field, $value)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild($command);

$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = $value;
}

$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$filterTag->{$field} = (string) $value;

return $response;
return $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
}

/**
Expand Down
Loading

0 comments on commit bdaad89

Please sign in to comment.