Skip to content

Commit

Permalink
Merge pull request #7 from the-kbA-team/6-apply-psr12-coding-standard
Browse files Browse the repository at this point in the history
apply psr12 coding standard
  • Loading branch information
gregor-j authored Mar 20, 2024
2 parents 4989b2d + 90a8ad1 commit 0679356
Show file tree
Hide file tree
Showing 29 changed files with 126 additions and 92 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ jobs:
- uses: actions/checkout@v3
- name: Check PHP syntax
run: php -l src/ tests/
# PHP coding standard validation
phpcs:
name: PHP coding standard
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check PHP coding standard
run: vendor/bin/phpcs
# run PHP unit tests
phpunit:
name: PHPUnit tests
Expand Down
21 changes: 21 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="PSR12-src">
<description>PSR12 programming standard</description>
<!-- display progress -->
<arg value="p"/>
<arg value="n"/>
<arg value="s"/>
<!-- directories and files to check -->
<file>./src</file>
<file>./tests</file>
<!-- Use PSR12 as a standard -->
<rule ref="PSR12">
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
<exclude-pattern>src/*</exclude-pattern>
</rule>
<rule ref="PSR12">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.9"
}
}
2 changes: 1 addition & 1 deletion src/JsonRPC/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function send()
{
$this->isBatch = false;

return $this->sendPayload('['.implode(', ', $this->batch).']');
return $this->sendPayload('[' . implode(', ', $this->batch) . ']');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public function handleExceptions(array $headers, $isJsonResponse = false)
return;
}

$errors = array_filter($headers, function($value) {
$errors = array_filter($headers, function ($value) {
return preg_match('/HTTP.*[4-5]\d{2}\s\w/', $value);
});

Expand Down
6 changes: 3 additions & 3 deletions src/JsonRPC/ProcedureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function executeCallback(Closure $callback, $params)
*/
public function executeMethod($class, $method, $params)
{
$instance = is_string($class) ? new $class : $class;
$instance = is_string($class) ? new $class() : $class;
$reflection = new ReflectionMethod($class, $method);

$this->executeBeforeMethod($instance, $method);
Expand Down Expand Up @@ -295,13 +295,13 @@ public function getNamedArguments(array $requestParams, array $methodParams)
} elseif ($p->isDefaultValueAvailable()) {
$params[$name] = $p->getDefaultValue();
} else {
throw new InvalidArgumentException('Missing argument: '.$name);
throw new InvalidArgumentException('Missing argument: ' . $name);
}
}

if ($undefinedRequestParams = array_diff_key($requestParams, $params)) {
throw new InvalidArgumentException(
'Undefined arguments: '.implode(', ', array_keys($undefinedRequestParams))
'Undefined arguments: ' . implode(', ', array_keys($undefinedRequestParams))
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/Request/BatchRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function parse()
}

$responses = array_filter($responses);
return empty($responses) ? '' : '['.implode(',', $responses).']';
return empty($responses) ? '' : '[' . implode(',', $responses) . ']';
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/JsonRPC/Request/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function withLocalException($exception)
} else {
$this->localExceptions[] = $exception;
}

return $this;
}

Expand Down Expand Up @@ -126,7 +126,6 @@ public function withMiddlewareHandler(MiddlewareHandler $middlewareHandler)
public function parse()
{
try {

JsonFormatValidator::validate($this->payload);
RpcFormatValidator::validate($this->payload);

Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/Response/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function sendHeaders()
}

foreach ($this->headers as $name => $value) {
header($name.': '.$value);
header($name . ': ' . $value);
}

return $this;
Expand Down
8 changes: 4 additions & 4 deletions src/JsonRPC/Response/ResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ private function handleExceptions()
{
switch ($this->payload['error']['code']) {
case -32700:
throw new InvalidJsonFormatException('Parse error: '.$this->payload['error']['message']);
throw new InvalidJsonFormatException('Parse error: ' . $this->payload['error']['message']);
case -32600:
throw new InvalidJsonRpcFormatException('Invalid Request: '.$this->payload['error']['message']);
throw new InvalidJsonRpcFormatException('Invalid Request: ' . $this->payload['error']['message']);
case -32601:
throw new BadFunctionCallException('Procedure not found: '.$this->payload['error']['message']);
throw new BadFunctionCallException('Procedure not found: ' . $this->payload['error']['message']);
case -32602:
throw new InvalidArgumentException('Invalid arguments: '.$this->payload['error']['message']);
throw new InvalidArgumentException('Invalid arguments: ' . $this->payload['error']['message']);
default:
throw new ResponseException(
$this->payload['error']['message'],
Expand Down
3 changes: 1 addition & 2 deletions src/JsonRPC/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function __construct(
public function setAuthenticationHeader($header)
{
if (! empty($header)) {
$header = 'HTTP_'.str_replace('-', '_', strtoupper($header));
$header = 'HTTP_' . str_replace('-', '_', strtoupper($header));
$value = $this->getServerVariable($header);

if (! empty($value)) {
Expand Down Expand Up @@ -309,7 +309,6 @@ public function execute()
;

$response = $this->parseRequest();

} catch (Exception $e) {
$response = $this->handleExceptions($e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/Validator/HostValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function validate(array $hosts, $remoteAddress)
throw new AccessDeniedException('Access Forbidden');
}
}

/**
* Validate remoteAddress match host
*
Expand Down
1 change: 0 additions & 1 deletion src/JsonRPC/Validator/JsonFormatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ public static function validate($payload)
}
}
}

8 changes: 4 additions & 4 deletions src/JsonRPC/Validator/RpcFormatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class RpcFormatValidator
*/
public static function validate(array $payload)
{
if (! isset($payload['jsonrpc']) ||
if (
! isset($payload['jsonrpc']) ||
! isset($payload['method']) ||
! is_string($payload['method']) ||
$payload['jsonrpc'] !== '2.0' ||
(isset($payload['params']) && ! is_array($payload['params']))) {

(isset($payload['params']) && ! is_array($payload['params']))
) {
throw new InvalidJsonRpcFormatException('Invalid JSON RPC payload');
}
}
}

2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use JsonRPC\Client;
use PHPUnit\Framework\TestCase;

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

class ClientTest extends TestCase
{
Expand Down
31 changes: 19 additions & 12 deletions tests/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

defined('CURLOPT_URL') || define('CURLOPT_URL', 10002);
defined('CURLOPT_RETURNTRANSFER') || define('CURLOPT_RETURNTRANSFER', 19913);
Expand All @@ -17,7 +17,8 @@
defined('CURLOPT_HEADERFUNCTION') || define('CURLOPT_HEADERFUNCTION', 20079);
defined('CURLOPT_CAINFO') || define('CURLOPT_CAINFO', 10065);

function extension_loaded($extension) {
function extension_loaded($extension)
{
return HttpClientTest::$functions->extension_loaded($extension);
}

Expand All @@ -31,27 +32,33 @@ function stream_context_create(array $params)
return HttpClientTest::$functions->stream_context_create($params);
}

function curl_init() {
function curl_init()
{
return HttpClientTest::$functions->curl_init();
}

function curl_setopt_array($ch, array $params) {
function curl_setopt_array($ch, array $params)
{
HttpClientTest::$functions->curl_setopt_array($ch, $params);
}

function curl_setopt($ch, $option, $value) {
function curl_setopt($ch, $option, $value)
{
HttpClientTest::$functions->curl_setopt($ch, $option, $value);
}

function curl_exec($ch) {
function curl_exec($ch)
{
return HttpClientTest::$functions->curl_exec($ch);
}

function curl_close($ch) {
function curl_close($ch)
{
HttpClientTest::$functions->curl_close($ch);
}

function curl_getinfo($ch, $option) {
function curl_getinfo($ch, $option)
{
HttpClientTest::$functions->curl_getinfo($ch, $option);
}

Expand Down Expand Up @@ -153,8 +160,8 @@ public function testWithCallback()
->will($this->returnValue(false));

$httpClient = new HttpClient('url');
$httpClient->withBeforeRequestCallback(function(HttpClient $client, $payload) {
$client->withHeaders(['Content-Length: '.strlen($payload)]);
$httpClient->withBeforeRequestCallback(function (HttpClient $client, $payload) {
$client->withHeaders(['Content-Length: ' . strlen($payload)]);
});

$this->expectException('\JsonRPC\Exception\ConnectionFailureException');
Expand Down Expand Up @@ -217,8 +224,8 @@ public function testWithCurl()
$httpClient = new HttpClient('url');
$httpClient
->withSslLocalCert('test.crt')
->withBeforeRequestCallback(function(HttpClient $client, $payload) {
$client->withHeaders(['Content-Length: '.strlen($payload)]);
->withBeforeRequestCallback(function (HttpClient $client, $payload) {
$client->withHeaders(['Content-Length: ' . strlen($payload)]);
});


Expand Down
2 changes: 1 addition & 1 deletion tests/MiddlewareHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use JsonRPC\MiddlewareInterface;
use PHPUnit\Framework\TestCase;

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

class FirstMiddleware implements MiddlewareInterface
{
Expand Down
Loading

0 comments on commit 0679356

Please sign in to comment.