Skip to content

Commit

Permalink
Merge pull request #13 from codeigniter4/style
Browse files Browse the repository at this point in the history
Apply coding standard
  • Loading branch information
lonnieezell authored Nov 11, 2021
2 parents 095599f + 6309c22 commit af1c44b
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 73 deletions.
1 change: 0 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@
];

return Factory::create(new CodeIgniter4(), $overrides, $options)->forProjects();

2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/Config/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Settings
* Database handler settings.
*/
public $database = [
'class' => DatabaseHandler::class,
'table' => 'settings',
'class' => DatabaseHandler::class,
'table' => 'settings',
'writeable' => true,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
namespace Sparks\Settings\Database\Migrations;

use CodeIgniter\Database\Migration;
use Config\Database;

class CreateSettingsTable extends Migration
{
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',
Expand Down
25 changes: 12 additions & 13 deletions src/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*/
Expand All @@ -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)) {
Expand All @@ -57,7 +53,7 @@ protected function prepareValue($value)
*
* @param mixed $value
*
* @return boolean|mixed
* @return bool|mixed
*/
protected function parseValue($value, string $type)
{
Expand All @@ -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
{
Expand Down Expand Up @@ -118,6 +112,7 @@ protected function isSerialized($data, $strict = true): bool
}
}
$token = $data[0];

switch ($token) {
case 's':
if ($strict) {
Expand All @@ -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;
}
}
28 changes: 10 additions & 18 deletions src/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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)
{
Expand Down
5 changes: 2 additions & 3 deletions src/Helpers/setting_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
15 changes: 2 additions & 13 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* retrieve settings that were original set in config files
* in the core application or any third-party module.
*/

class Settings
{
/**
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
*/
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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()
Expand Down
18 changes: 10 additions & 8 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

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;

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()
Expand Down Expand Up @@ -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);

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit af1c44b

Please sign in to comment.