From 9ac615e4a4634d296e51d5020a8f92db527d22ea Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 31 May 2022 23:55:04 -0400 Subject: [PATCH] Apply more PSR-12 style rules --- .php-cs-fixer.dist.php | 26 +++++++-------------- lib/Db/PostgreSQL/PDOResult.php | 1 - lib/Misc/URL.php | 1 - lib/Misc/ValueInfo.php | 16 ++++++------- lib/REST.php | 4 ++-- lib/Service.php | 6 ++--- tests/cases/Exception/TestException.php | 2 +- tests/cases/Feed/TestException.php | 8 +++---- tests/cases/Misc/TestContext.php | 4 ++-- tests/cases/Misc/TestURL.php | 1 - tests/cases/REST/Miniflux/TestV1.php | 2 +- tests/cases/REST/NextcloudNews/TestV1_2.php | 2 +- tests/cases/REST/TestREST.php | 9 ++++--- tests/cases/REST/TinyTinyRSS/TestAPI.php | 2 +- tests/cases/REST/TinyTinyRSS/TestIcon.php | 2 +- tests/cases/Service/TestService.php | 2 +- tests/lib/AbstractTest.php | 22 ++++++++--------- 17 files changed, 48 insertions(+), 62 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 925d4d19..d1706cb3 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -16,6 +16,8 @@ $paths = [ BASE."tests", ]; $rules = [ + // PSR standard to apply + '@PSR12' => true, // house rules where PSR series is silent 'align_multiline_comment' => ['comment_type' => "phpdocs_only"], 'array_syntax' => ['syntax' => "short"], @@ -51,27 +53,15 @@ $rules = [ 'trailing_comma_in_multiline' => true, 'unary_operator_spaces' => true, 'yoda_style' => false, - // PSR standard to apply - '@PSR2' => true, - // PSR-12 rules; php-cs-fixer does not yet support PSR-12 natively - 'compact_nullable_typehint' => true, - 'declare_equal_normalize' => ['space' => "none"], - 'function_typehint_space' => true, - 'lowercase_cast' => true, - 'lowercase_static_reference' => true, - 'no_alternative_syntax' => true, - 'no_empty_statement' => true, - 'no_leading_import_slash' => true, - 'no_leading_namespace_whitespace' => true, - 'no_whitespace_in_blank_line' => true, - 'return_type_declaration' => ['space_before' => "none"], - 'single_trait_insert_per_statement' => true, - 'short_scalar_cast' => true, - 'visibility_required' => ['elements' => ["const", "property", "method"]], // house exceptions to PSR rules 'braces' => ['position_after_functions_and_oop_constructs' => "same"], 'function_declaration' => ['closure_function_spacing' => "none"], - 'new_with_braces' => false, // no option to specify absence of braces + 'new_with_braces' => [ + 'anonymous_class' => false, + 'named_class' => false, + ], + 'single_blank_line_before_namespace' => false, + 'blank_line_after_opening_tag' => false, ]; $finder = \PhpCsFixer\Finder::create(); diff --git a/lib/Db/PostgreSQL/PDOResult.php b/lib/Db/PostgreSQL/PDOResult.php index 4920776f..77cf88f0 100644 --- a/lib/Db/PostgreSQL/PDOResult.php +++ b/lib/Db/PostgreSQL/PDOResult.php @@ -7,7 +7,6 @@ declare(strict_types=1); namespace JKingWeb\Arsse\Db\PostgreSQL; class PDOResult extends \JKingWeb\Arsse\Db\PDOResult { - // This method exists to transparent handle byte-array results public function valid() { diff --git a/lib/Misc/URL.php b/lib/Misc/URL.php index a1cad01b..6b22b63e 100644 --- a/lib/Misc/URL.php +++ b/lib/Misc/URL.php @@ -10,7 +10,6 @@ namespace JKingWeb\Arsse\Misc; * A collection of functions for manipulating URLs */ class URL { - /** Returns whether a URL is absolute i.e. has a scheme */ public static function absolute(string $url): bool { return (bool) strlen((string) parse_url($url, \PHP_URL_SCHEME)); diff --git a/lib/Misc/ValueInfo.php b/lib/Misc/ValueInfo.php index d03949cd..b3cdb56e 100644 --- a/lib/Misc/ValueInfo.php +++ b/lib/Misc/ValueInfo.php @@ -107,7 +107,7 @@ class ValueInfo { if ($strict && !$drop) { throw new ExceptionType("strictFailure", $type); } - return (!$drop) ? (int) $value->getTimestamp(): null; + return (!$drop) ? (int) $value->getTimestamp() : null; } elseif ($value instanceof \DateInterval) { if ($strict && !$drop) { throw new ExceptionType("strictFailure", $type); @@ -159,7 +159,7 @@ class ValueInfo { if ($strict && !$drop) { throw new ExceptionType("strictFailure", $type); } - return (!$drop) ? (float) $value->getTimestamp(): null; + return (!$drop) ? (float) $value->getTimestamp() : null; } elseif ($value instanceof \DateInterval) { if ($drop) { return null; @@ -203,13 +203,13 @@ class ValueInfo { if ($value->days) { $dateSpec = $value->days."D"; } else { - $dateSpec .= $value->y ? $value->y."Y": ""; - $dateSpec .= $value->m ? $value->m."M": ""; - $dateSpec .= $value->d ? $value->d."D": ""; + $dateSpec .= $value->y ? $value->y."Y" : ""; + $dateSpec .= $value->m ? $value->m."M" : ""; + $dateSpec .= $value->d ? $value->d."D" : ""; } - $timeSpec .= $value->h ? $value->h."H": ""; - $timeSpec .= $value->i ? $value->i."M": ""; - $timeSpec .= $value->s ? $value->s."S": ""; + $timeSpec .= $value->h ? $value->h."H" : ""; + $timeSpec .= $value->i ? $value->i."M" : ""; + $timeSpec .= $value->s ? $value->s."S" : ""; $timeSpec = $timeSpec ? "T".$timeSpec : ""; if (!$dateSpec && !$timeSpec) { return "PT0S"; diff --git a/lib/REST.php b/lib/REST.php index f7818e56..adc56ac5 100644 --- a/lib/REST.php +++ b/lib/REST.php @@ -125,14 +125,14 @@ class REST { $target = substr($url, strlen($api['strip'])); } else { // if the match fails we are not able to handle the request - throw new REST\Exception501(); + throw new REST\Exception501; } // return the API name, stripped URL, and API class name return [$id, $target, $api['class']]; } } // or throw an exception otherwise - throw new REST\Exception501(); + throw new REST\Exception501; } public function authenticateRequest(ServerRequestInterface $req): ServerRequestInterface { diff --git a/lib/Service.php b/lib/Service.php index 7eb31779..79a53467 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -21,13 +21,13 @@ class Service { public function __construct() { $driver = Arsse::$conf->serviceDriver; - $this->drv = new $driver(); + $this->drv = new $driver; } public function watch(bool $loop = true): \DateTimeInterface { $this->loop = $loop; $this->signalInit(); - $t = new \DateTime(); + $t = new \DateTime; do { $this->checkIn(); static::cleanupPre(); @@ -80,7 +80,7 @@ class Service { // get the checking interval $int = Arsse::$conf->serviceFrequency; // subtract twice the checking interval from the current time to yield the earliest acceptable check-in time - $limit = new \DateTime(); + $limit = new \DateTime; $limit->sub($int); $limit->sub($int); // return whether the check-in time is within the acceptable limit diff --git a/tests/cases/Exception/TestException.php b/tests/cases/Exception/TestException.php index c85bff8d..3ab7d3b6 100644 --- a/tests/cases/Exception/TestException.php +++ b/tests/cases/Exception/TestException.php @@ -31,7 +31,7 @@ class TestException extends \JKingWeb\Arsse\Test\AbstractTest { */ public function testBaseClassWithoutMessage(): void { $this->assertException("unknown"); - throw new Exception(); + throw new Exception; } /** diff --git a/tests/cases/Feed/TestException.php b/tests/cases/Feed/TestException.php index b28d0d1d..b39a32f0 100644 --- a/tests/cases/Feed/TestException.php +++ b/tests/cases/Feed/TestException.php @@ -150,10 +150,10 @@ class TestException extends \JKingWeb\Arsse\Test\AbstractTest { public function providePicoFeedException() { return [ - 'Failed feed discovery' => [new \PicoFeed\Reader\SubscriptionNotFoundException(), "subscriptionNotFound"], - 'Unsupported format' => [new \PicoFeed\Reader\UnsupportedFeedFormatException(), "unsupportedFeedFormat"], - 'Malformed XML' => [new \PicoFeed\Parser\MalformedXmlException(), "malformedXml"], - 'XML entity expansion' => [new \PicoFeed\Parser\XmlEntityException(), "xmlEntity"], + 'Failed feed discovery' => [new \PicoFeed\Reader\SubscriptionNotFoundException, "subscriptionNotFound"], + 'Unsupported format' => [new \PicoFeed\Reader\UnsupportedFeedFormatException, "unsupportedFeedFormat"], + 'Malformed XML' => [new \PicoFeed\Parser\MalformedXmlException, "malformedXml"], + 'XML entity expansion' => [new \PicoFeed\Parser\XmlEntityException, "xmlEntity"], ]; } diff --git a/tests/cases/Misc/TestContext.php b/tests/cases/Misc/TestContext.php index f038594c..97882311 100644 --- a/tests/cases/Misc/TestContext.php +++ b/tests/cases/Misc/TestContext.php @@ -103,7 +103,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest { public function testCleanIdArrayValues(): void { $methods = ["articles", "editions", "tags", "labels", "subscriptions"]; - $in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime(), -1.0]; + $in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime, -1.0]; $out = [1, 2, 4]; $c = new Context; foreach ($methods as $method) { @@ -113,7 +113,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest { public function testCleanFolderIdArrayValues(): void { $methods = ["folders", "foldersShallow"]; - $in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime(), -1.0]; + $in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime, -1.0]; $out = [1, 2, 4, 0]; $c = new Context; foreach ($methods as $method) { diff --git a/tests/cases/Misc/TestURL.php b/tests/cases/Misc/TestURL.php index eddc67ed..a5de76ac 100644 --- a/tests/cases/Misc/TestURL.php +++ b/tests/cases/Misc/TestURL.php @@ -10,7 +10,6 @@ use JKingWeb\Arsse\Misc\URL; /** @covers \JKingWeb\Arsse\Misc\URL */ class TestURL extends \JKingWeb\Arsse\Test\AbstractTest { - /** @dataProvider provideNormalizations */ public function testNormalizeAUrl(string $url, string $exp, string $user = null, string $pass = null): void { $this->assertSame($exp, URL::normalize($url, $user, $pass)); diff --git a/tests/cases/REST/Miniflux/TestV1.php b/tests/cases/REST/Miniflux/TestV1.php index 9623fd3d..3bae78e8 100644 --- a/tests/cases/REST/Miniflux/TestV1.php +++ b/tests/cases/REST/Miniflux/TestV1.php @@ -93,7 +93,7 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { Arsse::$user->method("propertiesGet")->willReturn(['num' => 42, 'admin' => false, 'root_folder_name' => null, 'tz' => "Asia/Gaza"]); Arsse::$user->method("begin")->willReturn($this->transaction->get()); //initialize a handler - $this->h = new V1(); + $this->h = new V1; } protected function v($value) { diff --git a/tests/cases/REST/NextcloudNews/TestV1_2.php b/tests/cases/REST/NextcloudNews/TestV1_2.php index f58f87d6..34720e31 100644 --- a/tests/cases/REST/NextcloudNews/TestV1_2.php +++ b/tests/cases/REST/NextcloudNews/TestV1_2.php @@ -328,7 +328,7 @@ class TestV1_2 extends \JKingWeb\Arsse\Test\AbstractTest { $this->dbMock = $this->mock(Database::class); $this->dbMock->begin->returns($this->mock(Transaction::class)); //initialize a handler - $this->h = new V1_2(); + $this->h = new V1_2; } protected function v($value) { diff --git a/tests/cases/REST/TestREST.php b/tests/cases/REST/TestREST.php index 0ba6eada..347ab7fe 100644 --- a/tests/cases/REST/TestREST.php +++ b/tests/cases/REST/TestREST.php @@ -22,7 +22,6 @@ use Laminas\Diactoros\Response\EmptyResponse; /** @covers \JKingWeb\Arsse\REST */ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { - /** @dataProvider provideApiMatchData */ public function testMatchAUrlToAnApi($apiList, string $input, array $exp): void { $r = new REST($apiList); @@ -61,7 +60,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideAuthenticableRequests */ public function testAuthenticateRequests(array $serverParams, array $expAttr): void { - $r = new REST(); + $r = new REST; // create a mock user manager $this->userMock = $this->mock(User::class); $this->userMock->auth->returns(false); @@ -95,7 +94,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { public function testSendAuthenticationChallenges(): void { self::setConf(); - $r = new REST(); + $r = new REST; $in = new EmptyResponse(401); $exp = $in->withHeader("WWW-Authenticate", 'Basic realm="OOK", charset="UTF-8"'); $act = $r->challenge($in, "OOK"); @@ -107,7 +106,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideUnnormalizedOrigins */ public function testNormalizeOrigins(string $origin, string $exp, array $ports = null): void { - $r = new REST(); + $r = new REST; $act = $r->corsNormalizeOrigin($origin, $ports); $this->assertSame($exp, $act); } @@ -188,7 +187,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { /** @dataProvider provideCorsHeaders */ public function testAddCorsHeaders(string $reqMethod, array $reqHeaders, array $resHeaders, array $expHeaders): void { - $r = new REST(); + $r = new REST; $req = new Request("", $reqMethod, "php://memory", $reqHeaders); $res = new EmptyResponse(204, $resHeaders); $exp = new EmptyResponse(204, $expHeaders); diff --git a/tests/cases/REST/TinyTinyRSS/TestAPI.php b/tests/cases/REST/TinyTinyRSS/TestAPI.php index 77497a61..9a10d6a2 100644 --- a/tests/cases/REST/TinyTinyRSS/TestAPI.php +++ b/tests/cases/REST/TinyTinyRSS/TestAPI.php @@ -147,7 +147,7 @@ LONG_STRING; 'expires' => "2112-12-21 21:12:00", 'user' => $this->userId, ]); - $this->h = new API(); + $this->h = new API; } protected function req($data, string $method = "POST", string $target = "", string $strData = null, string $user = null): ResponseInterface { diff --git a/tests/cases/REST/TinyTinyRSS/TestIcon.php b/tests/cases/REST/TinyTinyRSS/TestIcon.php index f541f504..f9d4b277 100644 --- a/tests/cases/REST/TinyTinyRSS/TestIcon.php +++ b/tests/cases/REST/TinyTinyRSS/TestIcon.php @@ -25,7 +25,7 @@ class TestIcon extends \JKingWeb\Arsse\Test\AbstractTest { Arsse::$user = $this->mock(User::class)->get(); // create a mock database interface $this->dbMock = $this->mock(Database::class); - $this->h = new Icon(); + $this->h = new Icon; } protected function req(string $target, string $method = "GET", string $user = null): ResponseInterface { diff --git a/tests/cases/Service/TestService.php b/tests/cases/Service/TestService.php index 277df8f1..8ac4e4a1 100644 --- a/tests/cases/Service/TestService.php +++ b/tests/cases/Service/TestService.php @@ -20,7 +20,7 @@ class TestService extends \JKingWeb\Arsse\Test\AbstractTest { self::setConf(); $this->dbMock = $this->mock(Database::class); Arsse::$db = $this->dbMock->get(); - $this->srv = new Service(); + $this->srv = new Service; } public function testCheckIn(): void { diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index 409a6e95..627e888a 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -54,7 +54,7 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { Arsse::$$prop = null; } if ($loadLang) { - Arsse::$lang = new \JKingWeb\Arsse\Lang(); + Arsse::$lang = new \JKingWeb\Arsse\Lang; } } @@ -62,17 +62,17 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { $defaults = [ 'dbSQLite3File' => ":memory:", 'dbSQLite3Timeout' => 0, - 'dbPostgreSQLHost' => $_ENV['ARSSE_TEST_PGSQL_HOST'] ?: "", - 'dbPostgreSQLPort' => $_ENV['ARSSE_TEST_PGSQL_PORT'] ?: 5432, - 'dbPostgreSQLUser' => $_ENV['ARSSE_TEST_PGSQL_USER'] ?: "arsse_test", - 'dbPostgreSQLPass' => $_ENV['ARSSE_TEST_PGSQL_PASS'] ?: "arsse_test", - 'dbPostgreSQLDb' => $_ENV['ARSSE_TEST_PGSQL_DB'] ?: "arsse_test", + 'dbPostgreSQLHost' => $_ENV['ARSSE_TEST_PGSQL_HOST'] ?: "", + 'dbPostgreSQLPort' => $_ENV['ARSSE_TEST_PGSQL_PORT'] ?: 5432, + 'dbPostgreSQLUser' => $_ENV['ARSSE_TEST_PGSQL_USER'] ?: "arsse_test", + 'dbPostgreSQLPass' => $_ENV['ARSSE_TEST_PGSQL_PASS'] ?: "arsse_test", + 'dbPostgreSQLDb' => $_ENV['ARSSE_TEST_PGSQL_DB'] ?: "arsse_test", 'dbPostgreSQLSchema' => $_ENV['ARSSE_TEST_PGSQL_SCHEMA'] ?: "arsse_test", - 'dbMySQLHost' => $_ENV['ARSSE_TEST_MYSQL_HOST'] ?: "localhost", - 'dbMySQLPort' => $_ENV['ARSSE_TEST_MYSQL_PORT'] ?: 3306, - 'dbMySQLUser' => $_ENV['ARSSE_TEST_MYSQL_USER'] ?: "arsse_test", - 'dbMySQLPass' => $_ENV['ARSSE_TEST_MYSQL_PASS'] ?: "arsse_test", - 'dbMySQLDb' => $_ENV['ARSSE_TEST_MYSQL_DB'] ?: "arsse_test", + 'dbMySQLHost' => $_ENV['ARSSE_TEST_MYSQL_HOST'] ?: "localhost", + 'dbMySQLPort' => $_ENV['ARSSE_TEST_MYSQL_PORT'] ?: 3306, + 'dbMySQLUser' => $_ENV['ARSSE_TEST_MYSQL_USER'] ?: "arsse_test", + 'dbMySQLPass' => $_ENV['ARSSE_TEST_MYSQL_PASS'] ?: "arsse_test", + 'dbMySQLDb' => $_ENV['ARSSE_TEST_MYSQL_DB'] ?: "arsse_test", ]; Arsse::$conf = (($force ? null : Arsse::$conf) ?? (new Conf))->import($defaults)->import($conf); }