1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Apply more PSR-12 style rules

This commit is contained in:
J. King 2022-05-31 23:55:04 -04:00
parent 4ed650fd87
commit 9ac615e4a4
17 changed files with 48 additions and 62 deletions

View file

@ -16,6 +16,8 @@ $paths = [
BASE."tests", BASE."tests",
]; ];
$rules = [ $rules = [
// PSR standard to apply
'@PSR12' => true,
// house rules where PSR series is silent // house rules where PSR series is silent
'align_multiline_comment' => ['comment_type' => "phpdocs_only"], 'align_multiline_comment' => ['comment_type' => "phpdocs_only"],
'array_syntax' => ['syntax' => "short"], 'array_syntax' => ['syntax' => "short"],
@ -51,27 +53,15 @@ $rules = [
'trailing_comma_in_multiline' => true, 'trailing_comma_in_multiline' => true,
'unary_operator_spaces' => true, 'unary_operator_spaces' => true,
'yoda_style' => false, '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 // house exceptions to PSR rules
'braces' => ['position_after_functions_and_oop_constructs' => "same"], 'braces' => ['position_after_functions_and_oop_constructs' => "same"],
'function_declaration' => ['closure_function_spacing' => "none"], '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(); $finder = \PhpCsFixer\Finder::create();

View file

@ -7,7 +7,6 @@ declare(strict_types=1);
namespace JKingWeb\Arsse\Db\PostgreSQL; namespace JKingWeb\Arsse\Db\PostgreSQL;
class PDOResult extends \JKingWeb\Arsse\Db\PDOResult { class PDOResult extends \JKingWeb\Arsse\Db\PDOResult {
// This method exists to transparent handle byte-array results // This method exists to transparent handle byte-array results
public function valid() { public function valid() {

View file

@ -10,7 +10,6 @@ namespace JKingWeb\Arsse\Misc;
* A collection of functions for manipulating URLs * A collection of functions for manipulating URLs
*/ */
class URL { class URL {
/** Returns whether a URL is absolute i.e. has a scheme */ /** Returns whether a URL is absolute i.e. has a scheme */
public static function absolute(string $url): bool { public static function absolute(string $url): bool {
return (bool) strlen((string) parse_url($url, \PHP_URL_SCHEME)); return (bool) strlen((string) parse_url($url, \PHP_URL_SCHEME));

View file

@ -107,7 +107,7 @@ class ValueInfo {
if ($strict && !$drop) { if ($strict && !$drop) {
throw new ExceptionType("strictFailure", $type); throw new ExceptionType("strictFailure", $type);
} }
return (!$drop) ? (int) $value->getTimestamp(): null; return (!$drop) ? (int) $value->getTimestamp() : null;
} elseif ($value instanceof \DateInterval) { } elseif ($value instanceof \DateInterval) {
if ($strict && !$drop) { if ($strict && !$drop) {
throw new ExceptionType("strictFailure", $type); throw new ExceptionType("strictFailure", $type);
@ -159,7 +159,7 @@ class ValueInfo {
if ($strict && !$drop) { if ($strict && !$drop) {
throw new ExceptionType("strictFailure", $type); throw new ExceptionType("strictFailure", $type);
} }
return (!$drop) ? (float) $value->getTimestamp(): null; return (!$drop) ? (float) $value->getTimestamp() : null;
} elseif ($value instanceof \DateInterval) { } elseif ($value instanceof \DateInterval) {
if ($drop) { if ($drop) {
return null; return null;
@ -203,13 +203,13 @@ class ValueInfo {
if ($value->days) { if ($value->days) {
$dateSpec = $value->days."D"; $dateSpec = $value->days."D";
} else { } else {
$dateSpec .= $value->y ? $value->y."Y": ""; $dateSpec .= $value->y ? $value->y."Y" : "";
$dateSpec .= $value->m ? $value->m."M": ""; $dateSpec .= $value->m ? $value->m."M" : "";
$dateSpec .= $value->d ? $value->d."D": ""; $dateSpec .= $value->d ? $value->d."D" : "";
} }
$timeSpec .= $value->h ? $value->h."H": ""; $timeSpec .= $value->h ? $value->h."H" : "";
$timeSpec .= $value->i ? $value->i."M": ""; $timeSpec .= $value->i ? $value->i."M" : "";
$timeSpec .= $value->s ? $value->s."S": ""; $timeSpec .= $value->s ? $value->s."S" : "";
$timeSpec = $timeSpec ? "T".$timeSpec : ""; $timeSpec = $timeSpec ? "T".$timeSpec : "";
if (!$dateSpec && !$timeSpec) { if (!$dateSpec && !$timeSpec) {
return "PT0S"; return "PT0S";

View file

@ -125,14 +125,14 @@ class REST {
$target = substr($url, strlen($api['strip'])); $target = substr($url, strlen($api['strip']));
} else { } else {
// if the match fails we are not able to handle the request // 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 the API name, stripped URL, and API class name
return [$id, $target, $api['class']]; return [$id, $target, $api['class']];
} }
} }
// or throw an exception otherwise // or throw an exception otherwise
throw new REST\Exception501(); throw new REST\Exception501;
} }
public function authenticateRequest(ServerRequestInterface $req): ServerRequestInterface { public function authenticateRequest(ServerRequestInterface $req): ServerRequestInterface {

View file

@ -21,13 +21,13 @@ class Service {
public function __construct() { public function __construct() {
$driver = Arsse::$conf->serviceDriver; $driver = Arsse::$conf->serviceDriver;
$this->drv = new $driver(); $this->drv = new $driver;
} }
public function watch(bool $loop = true): \DateTimeInterface { public function watch(bool $loop = true): \DateTimeInterface {
$this->loop = $loop; $this->loop = $loop;
$this->signalInit(); $this->signalInit();
$t = new \DateTime(); $t = new \DateTime;
do { do {
$this->checkIn(); $this->checkIn();
static::cleanupPre(); static::cleanupPre();
@ -80,7 +80,7 @@ class Service {
// get the checking interval // get the checking interval
$int = Arsse::$conf->serviceFrequency; $int = Arsse::$conf->serviceFrequency;
// subtract twice the checking interval from the current time to yield the earliest acceptable check-in time // 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);
$limit->sub($int); $limit->sub($int);
// return whether the check-in time is within the acceptable limit // return whether the check-in time is within the acceptable limit

View file

@ -31,7 +31,7 @@ class TestException extends \JKingWeb\Arsse\Test\AbstractTest {
*/ */
public function testBaseClassWithoutMessage(): void { public function testBaseClassWithoutMessage(): void {
$this->assertException("unknown"); $this->assertException("unknown");
throw new Exception(); throw new Exception;
} }
/** /**

View file

@ -150,10 +150,10 @@ class TestException extends \JKingWeb\Arsse\Test\AbstractTest {
public function providePicoFeedException() { public function providePicoFeedException() {
return [ return [
'Failed feed discovery' => [new \PicoFeed\Reader\SubscriptionNotFoundException(), "subscriptionNotFound"], 'Failed feed discovery' => [new \PicoFeed\Reader\SubscriptionNotFoundException, "subscriptionNotFound"],
'Unsupported format' => [new \PicoFeed\Reader\UnsupportedFeedFormatException(), "unsupportedFeedFormat"], 'Unsupported format' => [new \PicoFeed\Reader\UnsupportedFeedFormatException, "unsupportedFeedFormat"],
'Malformed XML' => [new \PicoFeed\Parser\MalformedXmlException(), "malformedXml"], 'Malformed XML' => [new \PicoFeed\Parser\MalformedXmlException, "malformedXml"],
'XML entity expansion' => [new \PicoFeed\Parser\XmlEntityException(), "xmlEntity"], 'XML entity expansion' => [new \PicoFeed\Parser\XmlEntityException, "xmlEntity"],
]; ];
} }

View file

@ -103,7 +103,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
public function testCleanIdArrayValues(): void { public function testCleanIdArrayValues(): void {
$methods = ["articles", "editions", "tags", "labels", "subscriptions"]; $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]; $out = [1, 2, 4];
$c = new Context; $c = new Context;
foreach ($methods as $method) { foreach ($methods as $method) {
@ -113,7 +113,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
public function testCleanFolderIdArrayValues(): void { public function testCleanFolderIdArrayValues(): void {
$methods = ["folders", "foldersShallow"]; $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]; $out = [1, 2, 4, 0];
$c = new Context; $c = new Context;
foreach ($methods as $method) { foreach ($methods as $method) {

View file

@ -10,7 +10,6 @@ use JKingWeb\Arsse\Misc\URL;
/** @covers \JKingWeb\Arsse\Misc\URL */ /** @covers \JKingWeb\Arsse\Misc\URL */
class TestURL extends \JKingWeb\Arsse\Test\AbstractTest { class TestURL extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideNormalizations */ /** @dataProvider provideNormalizations */
public function testNormalizeAUrl(string $url, string $exp, string $user = null, string $pass = null): void { public function testNormalizeAUrl(string $url, string $exp, string $user = null, string $pass = null): void {
$this->assertSame($exp, URL::normalize($url, $user, $pass)); $this->assertSame($exp, URL::normalize($url, $user, $pass));

View file

@ -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("propertiesGet")->willReturn(['num' => 42, 'admin' => false, 'root_folder_name' => null, 'tz' => "Asia/Gaza"]);
Arsse::$user->method("begin")->willReturn($this->transaction->get()); Arsse::$user->method("begin")->willReturn($this->transaction->get());
//initialize a handler //initialize a handler
$this->h = new V1(); $this->h = new V1;
} }
protected function v($value) { protected function v($value) {

View file

@ -328,7 +328,7 @@ class TestV1_2 extends \JKingWeb\Arsse\Test\AbstractTest {
$this->dbMock = $this->mock(Database::class); $this->dbMock = $this->mock(Database::class);
$this->dbMock->begin->returns($this->mock(Transaction::class)); $this->dbMock->begin->returns($this->mock(Transaction::class));
//initialize a handler //initialize a handler
$this->h = new V1_2(); $this->h = new V1_2;
} }
protected function v($value) { protected function v($value) {

View file

@ -22,7 +22,6 @@ use Laminas\Diactoros\Response\EmptyResponse;
/** @covers \JKingWeb\Arsse\REST */ /** @covers \JKingWeb\Arsse\REST */
class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideApiMatchData */ /** @dataProvider provideApiMatchData */
public function testMatchAUrlToAnApi($apiList, string $input, array $exp): void { public function testMatchAUrlToAnApi($apiList, string $input, array $exp): void {
$r = new REST($apiList); $r = new REST($apiList);
@ -61,7 +60,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideAuthenticableRequests */ /** @dataProvider provideAuthenticableRequests */
public function testAuthenticateRequests(array $serverParams, array $expAttr): void { public function testAuthenticateRequests(array $serverParams, array $expAttr): void {
$r = new REST(); $r = new REST;
// create a mock user manager // create a mock user manager
$this->userMock = $this->mock(User::class); $this->userMock = $this->mock(User::class);
$this->userMock->auth->returns(false); $this->userMock->auth->returns(false);
@ -95,7 +94,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
public function testSendAuthenticationChallenges(): void { public function testSendAuthenticationChallenges(): void {
self::setConf(); self::setConf();
$r = new REST(); $r = new REST;
$in = new EmptyResponse(401); $in = new EmptyResponse(401);
$exp = $in->withHeader("WWW-Authenticate", 'Basic realm="OOK", charset="UTF-8"'); $exp = $in->withHeader("WWW-Authenticate", 'Basic realm="OOK", charset="UTF-8"');
$act = $r->challenge($in, "OOK"); $act = $r->challenge($in, "OOK");
@ -107,7 +106,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideUnnormalizedOrigins */ /** @dataProvider provideUnnormalizedOrigins */
public function testNormalizeOrigins(string $origin, string $exp, array $ports = null): void { public function testNormalizeOrigins(string $origin, string $exp, array $ports = null): void {
$r = new REST(); $r = new REST;
$act = $r->corsNormalizeOrigin($origin, $ports); $act = $r->corsNormalizeOrigin($origin, $ports);
$this->assertSame($exp, $act); $this->assertSame($exp, $act);
} }
@ -188,7 +187,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideCorsHeaders */ /** @dataProvider provideCorsHeaders */
public function testAddCorsHeaders(string $reqMethod, array $reqHeaders, array $resHeaders, array $expHeaders): void { 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); $req = new Request("", $reqMethod, "php://memory", $reqHeaders);
$res = new EmptyResponse(204, $resHeaders); $res = new EmptyResponse(204, $resHeaders);
$exp = new EmptyResponse(204, $expHeaders); $exp = new EmptyResponse(204, $expHeaders);

View file

@ -147,7 +147,7 @@ LONG_STRING;
'expires' => "2112-12-21 21:12:00", 'expires' => "2112-12-21 21:12:00",
'user' => $this->userId, '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 { protected function req($data, string $method = "POST", string $target = "", string $strData = null, string $user = null): ResponseInterface {

View file

@ -25,7 +25,7 @@ class TestIcon extends \JKingWeb\Arsse\Test\AbstractTest {
Arsse::$user = $this->mock(User::class)->get(); Arsse::$user = $this->mock(User::class)->get();
// create a mock database interface // create a mock database interface
$this->dbMock = $this->mock(Database::class); $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 { protected function req(string $target, string $method = "GET", string $user = null): ResponseInterface {

View file

@ -20,7 +20,7 @@ class TestService extends \JKingWeb\Arsse\Test\AbstractTest {
self::setConf(); self::setConf();
$this->dbMock = $this->mock(Database::class); $this->dbMock = $this->mock(Database::class);
Arsse::$db = $this->dbMock->get(); Arsse::$db = $this->dbMock->get();
$this->srv = new Service(); $this->srv = new Service;
} }
public function testCheckIn(): void { public function testCheckIn(): void {

View file

@ -54,7 +54,7 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase {
Arsse::$$prop = null; Arsse::$$prop = null;
} }
if ($loadLang) { 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 = [ $defaults = [
'dbSQLite3File' => ":memory:", 'dbSQLite3File' => ":memory:",
'dbSQLite3Timeout' => 0, 'dbSQLite3Timeout' => 0,
'dbPostgreSQLHost' => $_ENV['ARSSE_TEST_PGSQL_HOST'] ?: "", 'dbPostgreSQLHost' => $_ENV['ARSSE_TEST_PGSQL_HOST'] ?: "",
'dbPostgreSQLPort' => $_ENV['ARSSE_TEST_PGSQL_PORT'] ?: 5432, 'dbPostgreSQLPort' => $_ENV['ARSSE_TEST_PGSQL_PORT'] ?: 5432,
'dbPostgreSQLUser' => $_ENV['ARSSE_TEST_PGSQL_USER'] ?: "arsse_test", 'dbPostgreSQLUser' => $_ENV['ARSSE_TEST_PGSQL_USER'] ?: "arsse_test",
'dbPostgreSQLPass' => $_ENV['ARSSE_TEST_PGSQL_PASS'] ?: "arsse_test", 'dbPostgreSQLPass' => $_ENV['ARSSE_TEST_PGSQL_PASS'] ?: "arsse_test",
'dbPostgreSQLDb' => $_ENV['ARSSE_TEST_PGSQL_DB'] ?: "arsse_test", 'dbPostgreSQLDb' => $_ENV['ARSSE_TEST_PGSQL_DB'] ?: "arsse_test",
'dbPostgreSQLSchema' => $_ENV['ARSSE_TEST_PGSQL_SCHEMA'] ?: "arsse_test", 'dbPostgreSQLSchema' => $_ENV['ARSSE_TEST_PGSQL_SCHEMA'] ?: "arsse_test",
'dbMySQLHost' => $_ENV['ARSSE_TEST_MYSQL_HOST'] ?: "localhost", 'dbMySQLHost' => $_ENV['ARSSE_TEST_MYSQL_HOST'] ?: "localhost",
'dbMySQLPort' => $_ENV['ARSSE_TEST_MYSQL_PORT'] ?: 3306, 'dbMySQLPort' => $_ENV['ARSSE_TEST_MYSQL_PORT'] ?: 3306,
'dbMySQLUser' => $_ENV['ARSSE_TEST_MYSQL_USER'] ?: "arsse_test", 'dbMySQLUser' => $_ENV['ARSSE_TEST_MYSQL_USER'] ?: "arsse_test",
'dbMySQLPass' => $_ENV['ARSSE_TEST_MYSQL_PASS'] ?: "arsse_test", 'dbMySQLPass' => $_ENV['ARSSE_TEST_MYSQL_PASS'] ?: "arsse_test",
'dbMySQLDb' => $_ENV['ARSSE_TEST_MYSQL_DB'] ?: "arsse_test", 'dbMySQLDb' => $_ENV['ARSSE_TEST_MYSQL_DB'] ?: "arsse_test",
]; ];
Arsse::$conf = (($force ? null : Arsse::$conf) ?? (new Conf))->import($defaults)->import($conf); Arsse::$conf = (($force ? null : Arsse::$conf) ?? (new Conf))->import($defaults)->import($conf);
} }