From 6309c2214a011baaafa6a48d1f515d14808da2b2 Mon Sep 17 00:00:00 2001 From: MGatner Date: Thu, 11 Nov 2021 18:48:23 +0000 Subject: [PATCH] Apply coding standard --- .php-cs-fixer.dist.php | 1 - composer.json | 2 ++ src/Config/Services.php | 4 --- src/Config/Settings.php | 4 +-- .../2021-07-04-041948_CreateSettingsTable.php | 9 +++--- src/Handlers/BaseHandler.php | 25 ++++++++--------- src/Handlers/DatabaseHandler.php | 28 +++++++------------ src/Helpers/setting_helper.php | 5 ++-- src/Settings.php | 15 ++-------- tests/HelperTest.php | 9 ++++-- tests/SettingsTest.php | 18 ++++++------ tests/_support/Config/Test.php | 5 +++- tests/_support/TestCase.php | 4 +-- 13 files changed, 56 insertions(+), 73 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 3586995..dfa45af 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -18,4 +18,3 @@ ]; return Factory::create(new CodeIgniter4(), $overrides, $options)->forProjects(); - diff --git a/composer.json b/composer.json index 7154ab5..41c5197 100644 --- a/composer.json +++ b/composer.json @@ -20,9 +20,11 @@ "php": "^7.3 || ^8.0" }, "require-dev": { + "codeigniter/coding-standard": "^1.1", "codeigniter4/codeigniter4": "dev-develop", "fakerphp/faker": "^1.9", "mockery/mockery": "^1.0", + "nexusphp/cs-config": "^3.1", "nexusphp/tachycardia": "^1.0", "php-coveralls/php-coveralls": "^2.4", "phpstan/phpstan": "^1.0", diff --git a/src/Config/Services.php b/src/Config/Services.php index e19d19c..a49f92b 100644 --- a/src/Config/Services.php +++ b/src/Config/Services.php @@ -22,10 +22,6 @@ class Services extends BaseService { /** * Returns the Settings manager class. - * - * @param bool $getShared - * - * @return Settings */ public static function settings(bool $getShared = true): Settings { diff --git a/src/Config/Settings.php b/src/Config/Settings.php index fdd2519..467e753 100644 --- a/src/Config/Settings.php +++ b/src/Config/Settings.php @@ -19,8 +19,8 @@ class Settings * Database handler settings. */ public $database = [ - 'class' => DatabaseHandler::class, - 'table' => 'settings', + 'class' => DatabaseHandler::class, + 'table' => 'settings', 'writeable' => true, ]; } diff --git a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php index c281eca..708c822 100644 --- a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php +++ b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php @@ -3,7 +3,6 @@ namespace Sparks\Settings\Database\Migrations; use CodeIgniter\Database\Migration; -use Config\Database; class CreateSettingsTable extends Migration { @@ -11,19 +10,19 @@ public function up() { $this->forge->addField('id'); $this->forge->addField([ - 'class' => [ + 'class' => [ 'type' => 'varchar', 'constraint' => 255, ], - 'key' => [ + 'key' => [ 'type' => 'varchar', 'constraint' => 255, ], - 'value' => [ + 'value' => [ 'type' => 'text', 'null' => true, ], - 'type' => [ + 'type' => [ 'type' => 'varchar', 'constraint' => 31, 'default' => 'string', diff --git a/src/Handlers/BaseHandler.php b/src/Handlers/BaseHandler.php index 7f6b8c7..a02d250 100644 --- a/src/Handlers/BaseHandler.php +++ b/src/Handlers/BaseHandler.php @@ -7,9 +7,6 @@ abstract class BaseHandler /** * Returns a single value from the handler, if stored. * - * @param string $class - * @param string $property - * * @return mixed */ abstract public function get(string $class, string $property); @@ -19,8 +16,7 @@ abstract public function get(string $class, string $property); * MUST override this method to provide that functionality. * Not all Handlers will support writing values. * - * @param string $property - * @param mixed $value + * @param mixed $value * * @return mixed */ @@ -35,12 +31,12 @@ public function set(string $class, string $property, $value = null) * * @param mixed $value * - * @return string|mixed + * @return mixed|string */ protected function prepareValue($value) { if (is_bool($value)) { - return (int)$value; + return (int) $value; } if (is_array($value) || is_object($value)) { @@ -57,7 +53,7 @@ protected function prepareValue($value) * * @param mixed $value * - * @return boolean|mixed + * @return bool|mixed */ protected function parseValue($value, string $type) { @@ -76,10 +72,8 @@ protected function parseValue($value, string $type) * * Taken from Wordpress core functions. * - * @param mixed $data - * @param boolean $strict Whether to be strict about the end of the string. - * - * @return boolean + * @param mixed $data + * @param bool $strict Whether to be strict about the end of the string. */ protected function isSerialized($data, $strict = true): bool { @@ -118,6 +112,7 @@ protected function isSerialized($data, $strict = true): bool } } $token = $data[0]; + switch ($token) { case 's': if ($strict) { @@ -128,15 +123,19 @@ protected function isSerialized($data, $strict = true): bool return false; } // Or else fall through. + // no break case 'a': case 'O': return (bool) preg_match("/^{$token}:[0-9]+:/s", $data); + case 'b': case 'i': case 'd': $end = $strict ? '$' : ''; - return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data); + + return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data); } + return false; } } diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 43ffc36..6707436 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -23,7 +23,7 @@ class DatabaseHandler extends BaseHandler * Have the settings been read and cached * from the database yet? * - * @var boolean + * @var bool */ private $hydrated = false; @@ -40,9 +40,6 @@ class DatabaseHandler extends BaseHandler * read and stored in $this->settings the first * time, and then used from there the rest of the request. * - * @param string $class - * @param string $property - * * @return mixed|null */ public function get(string $class, string $property) @@ -59,9 +56,7 @@ public function get(string $class, string $property) /** * Stores values into the database for later retrieval. * - * @param string $class - * @param string $property - * @param mixed $value + * @param mixed $value * * @return mixed|void */ @@ -84,14 +79,14 @@ public function set(string $class, string $property, $value = null) ]); } else { $result = db_connect()->table($this->table) - ->insert([ - 'class' => $class, - 'key' => $property, - 'value' => $value, - 'type' => $type, - 'created_at' => $time, - 'updated_at' => $time, - ]); + ->insert([ + 'class' => $class, + 'key' => $property, + 'value' => $value, + 'type' => $type, + 'created_at' => $time, + 'updated_at' => $time, + ]); } // Update our cache @@ -112,9 +107,6 @@ public function set(string $class, string $property, $value = null) /** * Deletes the record from persistent storage, if found, * and from the local cache. - * - * @param string $class - * @param string $property */ public function forget(string $class, string $property) { diff --git a/src/Helpers/setting_helper.php b/src/Helpers/setting_helper.php index 8e8c3fd..315fd26 100644 --- a/src/Helpers/setting_helper.php +++ b/src/Helpers/setting_helper.php @@ -4,12 +4,11 @@ /** * Provides a convenience interface to the Settings service. * - * @param string|null $key - * @param mixed|null $value + * @param mixed|null $value * * @return mixed */ - function setting(string $key = null, $value = null) + function setting(?string $key = null, $value = null) { $setting = service('settings'); diff --git a/src/Settings.php b/src/Settings.php index 81b5ba4..908eff0 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -7,7 +7,6 @@ * retrieve settings that were original set in config files * in the core application or any third-party module. */ - class Settings { /** @@ -51,8 +50,6 @@ public function __construct() * Retrieve a value from either the database * or from a config file matching the name * file.arg.optionalArg - * - * @param string $key */ public function get(string $key) { @@ -73,8 +70,7 @@ public function get(string $key) /** * Save a value to the writable handler for later retrieval. * - * @param string $key - * @param mixed $value + * @param mixed $value * * @return void|null */ @@ -91,8 +87,6 @@ public function set(string $key, $value = null) * Removes a setting from the persistent storage, * effectively returning the value to the default value * found in the config file, if any. - * - * @param string $key */ public function forget(string $key) { @@ -120,8 +114,6 @@ private function getWriteHandler() /** * Analyzes the given key and breaks it into the class.field parts. * - * @param string $key - * * @return string[] */ private function parseDotSyntax(string $key): array @@ -139,10 +131,7 @@ private function parseDotSyntax(string $key): array /** * Given a key in class.property syntax, will split the values * and determine the fully qualified class name, if possible. - * - * @param string $key - * @return array - */ + */ private function prepareClassAndProperty(string $key): array { [$class, $property] = $this->parseDotSyntax($key); diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 22077ac..00b04b0 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -7,11 +7,14 @@ use Sparks\Settings\Settings; use Tests\Support\TestCase; -class HelperTest extends TestCase +/** + * @internal + */ +final class HelperTest extends TestCase { use DatabaseTestTrait; - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -41,7 +44,7 @@ public function testReturnsValueDotArray() 'updated_at' => Time::now()->toDateTimeString(), ]); - $this->assertEquals('baz', setting('Foo.bar')); + $this->assertSame('baz', setting('Foo.bar')); } public function testSettingValueDotArray() diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 9cc506d..eaecbc2 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -2,15 +2,17 @@ namespace Tests; -use Sparks\Settings\Settings; use CodeIgniter\I18n\Time; use CodeIgniter\Test\DatabaseTestTrait; +use Sparks\Settings\Settings; use Tests\Support\TestCase; /** * NOTE: $this->table is set in the TestCase itself + * + * @internal */ -class SettingsTest extends TestCase +final class SettingsTest extends TestCase { use DatabaseTestTrait; @@ -18,14 +20,14 @@ public function testSettingsGetsFromConfig() { $settings = new Settings(); - $this->assertEquals(config('Test')->siteName, $settings->get('Test.siteName')); + $this->assertSame(config('Test')->siteName, $settings->get('Test.siteName')); } public function testSettingsDatabaseNotFound() { $settings = new Settings(); - $this->assertEquals(config('Test')->siteName, $settings->get('Test.siteName')); + $this->assertSame(config('Test')->siteName, $settings->get('Test.siteName')); } public function testSetInsertsNewRows() @@ -92,13 +94,13 @@ public function testSetInsertsArray() 'type' => 'array', ]); - $this->assertEquals($data, $settings->get('Test.siteName')); + $this->assertSame($data, $settings->get('Test.siteName')); } public function testSetInsertsObject() { $settings = new Settings(); - $data = (object)['foo' => 'bar']; + $data = (object) ['foo' => 'bar']; $results = $settings->set('Test.siteName', $data); @@ -110,7 +112,7 @@ public function testSetInsertsObject() 'type' => 'object', ]); - $this->assertEquals($data, $settings->get('Test.siteName')); + $this->assertSame((array) $data, (array) $settings->get('Test.siteName')); } public function testSetUpdatesExistingRows() @@ -148,7 +150,7 @@ public function testWorksWithoutConfigClass() 'value' => 'Bar', ]); - $this->assertEquals('Bar', $settings->get('Nada.siteName')); + $this->assertSame('Bar', $settings->get('Nada.siteName')); } public function testForgetSuccess() diff --git a/tests/_support/Config/Test.php b/tests/_support/Config/Test.php index 4addb99..f6c7d5f 100644 --- a/tests/_support/Config/Test.php +++ b/tests/_support/Config/Test.php @@ -4,7 +4,10 @@ use CodeIgniter\Config\BaseConfig; -class Test extends BaseConfig +/** + * @internal + */ +final class Test extends BaseConfig { public $siteName = 'Settings Test'; } diff --git a/tests/_support/TestCase.php b/tests/_support/TestCase.php index 1f25e32..a0ee942 100644 --- a/tests/_support/TestCase.php +++ b/tests/_support/TestCase.php @@ -17,14 +17,14 @@ abstract class TestCase extends CIUnitTestCase */ protected $table; - public function setUp(): void + protected function setUp(): void { parent::setUp(); $this->table = config('Settings')->database['table']; } - public function tearDown(): void + protected function tearDown(): void { parent::tearDown();