Skip to content

Commit

Permalink
Update to use PHP 8.1 syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <[email protected]>
  • Loading branch information
samsonasik committed Nov 3, 2024
1 parent 351f80d commit c2aaa9b
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 100 deletions.
44 changes: 2 additions & 42 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,6 @@
<MixedOperand>
<code><![CDATA[$value]]></code>
</MixedOperand>
<PossiblyUnusedMethod>
<code><![CDATA[getFullRequest]]></code>
</PossiblyUnusedMethod>
<PropertyNotSetInConstructor>
<code><![CDATA[$headers]]></code>
<code><![CDATA[$xml]]></code>
Expand Down Expand Up @@ -430,7 +427,8 @@
<code><![CDATA[new $request()]]></code>
</MixedMethodCall>
<MixedReturnStatement>
<code><![CDATA[$this->typeMap[$type]]]></code>
<code><![CDATA[$this->typeMap[$type] ?? 'void']]></code>
<code><![CDATA[$this->typeMap[$type] ?? 'void']]></code>
</MixedReturnStatement>
<MoreSpecificImplementedParamType>
<code><![CDATA[$class]]></code>
Expand Down Expand Up @@ -694,9 +692,6 @@
<code><![CDATA[setServerResponseTo]]></code>
</MissingReturnType>
<MixedMethodCall>
<code><![CDATA[expects]]></code>
<code><![CDATA[expects]]></code>
<code><![CDATA[expects]]></code>
<code><![CDATA[getValue]]></code>
<code><![CDATA[getValue]]></code>
<code><![CDATA[getValue]]></code>
Expand All @@ -705,15 +700,6 @@
<code><![CDATA[getValue]]></code>
<code><![CDATA[getValue]]></code>
<code><![CDATA[getValue]]></code>
<code><![CDATA[method]]></code>
<code><![CDATA[method]]></code>
<code><![CDATA[method]]></code>
<code><![CDATA[willReturn]]></code>
<code><![CDATA[willReturn]]></code>
<code><![CDATA[willReturn]]></code>
<code><![CDATA[with]]></code>
<code><![CDATA[with]]></code>
<code><![CDATA[with]]></code>
</MixedMethodCall>
<UndefinedInterfaceMethod>
<code><![CDATA[addResponse]]></code>
Expand All @@ -732,9 +718,6 @@
<code><![CDATA[setResponse]]></code>
<code><![CDATA[setResponse]]></code>
</UndefinedInterfaceMethod>
<UndefinedThisPropertyAssignment>
<code><![CDATA[$this->mockedIntrospector]]></code>
</UndefinedThisPropertyAssignment>
<UnusedVariable>
<code><![CDATA[$signature]]></code>
<code><![CDATA[$time]]></code>
Expand Down Expand Up @@ -887,32 +870,9 @@
<MixedAssignment>
<code><![CDATA[[$path, $mode]]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[getFullRequest]]></code>
<code><![CDATA[getHeaders]]></code>
<code><![CDATA[getRawRequest]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$this->xml]]></code>
</MixedOperand>
<PossiblyNullReference>
<code><![CDATA[getCode]]></code>
</PossiblyNullReference>
<UndefinedThisPropertyAssignment>
<code><![CDATA[$this->request]]></code>
<code><![CDATA[$this->server]]></code>
<code><![CDATA[$this->xml]]></code>
</UndefinedThisPropertyAssignment>
<UndefinedThisPropertyFetch>
<code><![CDATA[$this->request]]></code>
<code><![CDATA[$this->request]]></code>
<code><![CDATA[$this->request]]></code>
<code><![CDATA[$this->request]]></code>
<code><![CDATA[$this->server]]></code>
<code><![CDATA[$this->xml]]></code>
<code><![CDATA[$this->xml]]></code>
<code><![CDATA[$this->xml]]></code>
</UndefinedThisPropertyFetch>
<UnusedForeachValue>
<code><![CDATA[$value]]></code>
</UnusedForeachValue>
Expand Down
30 changes: 14 additions & 16 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use function ini_set;
use function is_array;
use function is_string;
use function substr;
use function str_starts_with;
use function trim;

use const PHP_VERSION_ID;
Expand All @@ -30,14 +30,6 @@
*/
class Client implements ServerClient
{
/**
* Full address of the XML-RPC service
*
* @var string
* @example http://time.xmlrpc.com/RPC2
*/
protected $serverAddress;

/**
* HTTP Client to use for requests
*
Expand Down Expand Up @@ -83,20 +75,26 @@ class Client implements ServerClient
/**
* Create a new XML-RPC client to a remote server
*
* @param string $server Full address of the XML-RPC service
* @param string $serverAddress Full address of the XML-RPC service
* (e.g. http://time.xmlrpc.com/RPC2)
* @param \Laminas\Http\Client $httpClient HTTP Client to use for requests
*/
public function __construct($server, ?Http\Client $httpClient = null)
{
public function __construct(
/**
* Full address of the XML-RPC service
*
* @example http://time.xmlrpc.com/RPC2
*/
protected $serverAddress,
?Http\Client $httpClient = null
) {
if ($httpClient === null) {
$this->httpClient = new Http\Client();
} else {
$this->httpClient = $httpClient;
}

$this->introspector = new ServerIntrospection($this);
$this->serverAddress = $server;
$this->introspector = new ServerIntrospection($this);
}

/**
Expand Down Expand Up @@ -276,13 +274,13 @@ public function doRequest($request, $response = null, int $libXmlOptions = 0)
*/
public function call($method, $params = [])
{
if (! $this->skipSystemLookup() && ('system.' !== substr($method, 0, 7))) {
if (! $this->skipSystemLookup() && (! str_starts_with($method, 'system.'))) {
// Ensure empty array/struct params are cast correctly
// If system.* methods are not available, bypass. (Laminas-2978)
$success = true;
try {
$signatures = $this->getIntrospector()->getMethodSignature($method);
} catch (ExceptionInterface $e) {
} catch (ExceptionInterface) {
$success = false;
}
if ($success) {
Expand Down
4 changes: 2 additions & 2 deletions src/Client/ServerIntrospection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class ServerIntrospection
{
private ServerProxy $system;
private readonly ServerProxy $system;

public function __construct(XMLRPCClient $client)
{
Expand All @@ -33,7 +33,7 @@ public function getSignatureForEachMethod()

try {
$signatures = $this->getSignatureForEachMethodByMulticall($methods);
} catch (Exception\FaultException $e) {
} catch (Exception\FaultException) {
// degrade to looping
}

Expand Down
11 changes: 1 addition & 10 deletions src/Client/ServerProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,11 @@
*/
class ServerProxy
{
private XMLRPCClient $client;

private string $namespace = '';

/** @var array of \Laminas\XmlRpc\Client\ServerProxy */
private array $cache = [];

/**
* @param string $namespace
*/
public function __construct(XMLRPCClient $client, $namespace = '')
public function __construct(private readonly XMLRPCClient $client, private readonly string $namespace = '')
{
$this->client = $client;
$this->namespace = $namespace;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Fault.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laminas\Xml\Exception\RuntimeException;
use Laminas\Xml\Security as XmlSecurity;
use SimpleXMLElement;
use Stringable;

use function array_reduce;
use function is_string;
Expand All @@ -21,7 +22,7 @@
* To allow method chaining, you may only use the {@link getInstance()} factory
* to instantiate a Laminas\XmlRpc\Server\Fault.
*/
class Fault
class Fault implements Stringable
{
/**
* Fault code
Expand Down Expand Up @@ -259,7 +260,7 @@ public static function isFault($xml)
$fault = new static();
try {
$isFault = $fault->loadXml($xml);
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
$isFault = false;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laminas\Stdlib\ErrorHandler;
use Laminas\XmlRpc\Exception\ValueException;
use SimpleXMLElement;
use Stringable;

use function assert;
use function count;
Expand All @@ -31,7 +32,7 @@
* generated and stored in {@link $fault}; developers may check for it using
* {@link isFault()} and {@link getFault()}.
*/
class Request
class Request implements Stringable
{
/**
* Request character encoding
Expand Down Expand Up @@ -317,7 +318,7 @@ public function loadXml($request, int $libXmlOptions = 0)
$xml = simplexml_import_dom($dom);
$error = ErrorHandler::stop();
libxml_use_internal_errors($xmlErrorsFlag);
} catch (Exception $e) {
} catch (Exception) {
// Not valid XML
$this->fault = new Fault(631);
$this->fault->setEncoding($this->getEncoding());
Expand Down Expand Up @@ -359,7 +360,7 @@ public function loadXml($request, int $libXmlOptions = 0)
$param = AbstractValue::getXmlRpcValue($param->value, AbstractValue::XML_STRING);
$types[] = $param->getType();
$argv[] = $param->getValue();
} catch (Exception $e) {
} catch (Exception) {
$this->fault = new Fault(636);
$this->fault->setEncoding($this->getEncoding());
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/Request/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use function file_get_contents;
use function str_replace;
use function str_starts_with;
use function strtolower;
use function substr;
use function ucwords;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function getHeaders()
if (null === $this->headers) {
$this->headers = [];
foreach ($_SERVER as $key => $value) {
if ('HTTP_' === substr($key, 0, 5)) {
if (str_starts_with($key, 'HTTP_')) {
$header = str_replace(
' ',
'-',
Expand Down
5 changes: 3 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laminas\Xml\Exception\RuntimeException;
use Laminas\Xml\Security as XmlSecurity;
use Stringable;

use function is_object;
use function is_string;
Expand All @@ -13,7 +14,7 @@
*
* Container for accessing an XMLRPC return value and creating the XML response.
*/
class Response
class Response implements Stringable
{
/**
* Return value
Expand Down Expand Up @@ -188,7 +189,7 @@ public function loadXml($response, int $libXmlOptions = 0)
}
$valueXml = $xml->params->param->value->asXML();
$value = AbstractValue::getXmlRpcValue($valueXml, AbstractValue::XML_STRING, $libXmlOptions);
} catch (Exception\ValueException $e) {
} catch (Exception\ValueException) {
$this->fault = new Fault(653);
$this->fault->setEncoding($this->getEncoding());
return false;
Expand Down
9 changes: 3 additions & 6 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use function is_string;
use function is_subclass_of;
use function method_exists;
use function substr;
use function str_starts_with;

/**
* An XML-RPC server implementation
Expand Down Expand Up @@ -371,7 +371,7 @@ public function loadFunctions($definition)
}

foreach ($definition as $key => $method) {
if ('system.' === substr($key, 0, 7)) {
if (str_starts_with($key, 'system.')) {
continue;
}
$this->table->addMethod($method, $key);
Expand Down Expand Up @@ -543,10 +543,7 @@ public function sendArgumentsToAllMethods($flag = null)
*/
protected function _fixType($type)
{
if (isset($this->typeMap[$type])) {
return $this->typeMap[$type];
}
return 'void';
return $this->typeMap[$type] ?? 'void';
}
// @codingStandardsIgnoreEnd

Expand Down
6 changes: 4 additions & 2 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
use Laminas\XmlRpc\Request;
use Laminas\XmlRpc\Response;
use Laminas\XmlRpc\Value;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function count;
use function dirname;
use function file_get_contents;
use function implode;
use function strlen;
Expand All @@ -39,6 +39,8 @@ class ClientTest extends TestCase
/** @var Client */
protected $xmlrpcClient;

private MockObject $mockedIntrospector;

protected function setUp(): void
{
$this->httpAdapter = new Adapter\Test();
Expand Down Expand Up @@ -697,7 +699,7 @@ public function testHandlesLeadingOrTrailingWhitespaceInChunkedResponseProperly(
$this->httpAdapter = new Adapter\Test();
$this->httpClient = new HttpClient(null, ['adapter' => $this->httpAdapter]);

$respBody = file_get_contents(dirname(__FILE__) . "/_files/Laminas1897-response-chunked.txt");
$respBody = file_get_contents(__DIR__ . "/_files/Laminas1897-response-chunked.txt");
$this->httpAdapter->setResponse($respBody);

$this->xmlrpcClient = new Client($baseUri);
Expand Down
2 changes: 1 addition & 1 deletion test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testSpecialCharsAreEncoded(Generator $generator)
$variant2 = '<element>&lt;&gt;&amp;&quot;\'€</element>';
try {
$this->assertXml($variant1, $generator);
} catch (ExpectationFailedException $e) {
} catch (ExpectationFailedException) {
$this->assertXml($variant2, $generator);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/PhpInputMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PhpInputMock
public static function mockInput($data)
{
stream_wrapper_unregister('php');
stream_wrapper_register('php', 'LaminasTest\XmlRpc\PhpInputMock');
stream_wrapper_register('php', \LaminasTest\XmlRpc\PhpInputMock::class);
static::$data = $data;
}

Expand Down
Loading

0 comments on commit c2aaa9b

Please sign in to comment.