Skip to content

Commit af1c44b

Browse files
authored
Merge pull request #13 from codeigniter4/style
Apply coding standard
2 parents 095599f + 6309c22 commit af1c44b

13 files changed

Lines changed: 56 additions & 73 deletions

.php-cs-fixer.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@
1818
];
1919

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

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
"php": "^7.3 || ^8.0"
2121
},
2222
"require-dev": {
23+
"codeigniter/coding-standard": "^1.1",
2324
"codeigniter4/codeigniter4": "dev-develop",
2425
"fakerphp/faker": "^1.9",
2526
"mockery/mockery": "^1.0",
27+
"nexusphp/cs-config": "^3.1",
2628
"nexusphp/tachycardia": "^1.0",
2729
"php-coveralls/php-coveralls": "^2.4",
2830
"phpstan/phpstan": "^1.0",

src/Config/Services.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class Services extends BaseService
2222
{
2323
/**
2424
* Returns the Settings manager class.
25-
*
26-
* @param bool $getShared
27-
*
28-
* @return Settings
2925
*/
3026
public static function settings(bool $getShared = true): Settings
3127
{

src/Config/Settings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Settings
1919
* Database handler settings.
2020
*/
2121
public $database = [
22-
'class' => DatabaseHandler::class,
23-
'table' => 'settings',
22+
'class' => DatabaseHandler::class,
23+
'table' => 'settings',
2424
'writeable' => true,
2525
];
2626
}

src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
namespace Sparks\Settings\Database\Migrations;
44

55
use CodeIgniter\Database\Migration;
6-
use Config\Database;
76

87
class CreateSettingsTable extends Migration
98
{
109
public function up()
1110
{
1211
$this->forge->addField('id');
1312
$this->forge->addField([
14-
'class' => [
13+
'class' => [
1514
'type' => 'varchar',
1615
'constraint' => 255,
1716
],
18-
'key' => [
17+
'key' => [
1918
'type' => 'varchar',
2019
'constraint' => 255,
2120
],
22-
'value' => [
21+
'value' => [
2322
'type' => 'text',
2423
'null' => true,
2524
],
26-
'type' => [
25+
'type' => [
2726
'type' => 'varchar',
2827
'constraint' => 31,
2928
'default' => 'string',

src/Handlers/BaseHandler.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ abstract class BaseHandler
77
/**
88
* Returns a single value from the handler, if stored.
99
*
10-
* @param string $class
11-
* @param string $property
12-
*
1310
* @return mixed
1411
*/
1512
abstract public function get(string $class, string $property);
@@ -19,8 +16,7 @@ abstract public function get(string $class, string $property);
1916
* MUST override this method to provide that functionality.
2017
* Not all Handlers will support writing values.
2118
*
22-
* @param string $property
23-
* @param mixed $value
19+
* @param mixed $value
2420
*
2521
* @return mixed
2622
*/
@@ -35,12 +31,12 @@ public function set(string $class, string $property, $value = null)
3531
*
3632
* @param mixed $value
3733
*
38-
* @return string|mixed
34+
* @return mixed|string
3935
*/
4036
protected function prepareValue($value)
4137
{
4238
if (is_bool($value)) {
43-
return (int)$value;
39+
return (int) $value;
4440
}
4541

4642
if (is_array($value) || is_object($value)) {
@@ -57,7 +53,7 @@ protected function prepareValue($value)
5753
*
5854
* @param mixed $value
5955
*
60-
* @return boolean|mixed
56+
* @return bool|mixed
6157
*/
6258
protected function parseValue($value, string $type)
6359
{
@@ -76,10 +72,8 @@ protected function parseValue($value, string $type)
7672
*
7773
* Taken from Wordpress core functions.
7874
*
79-
* @param mixed $data
80-
* @param boolean $strict Whether to be strict about the end of the string.
81-
*
82-
* @return boolean
75+
* @param mixed $data
76+
* @param bool $strict Whether to be strict about the end of the string.
8377
*/
8478
protected function isSerialized($data, $strict = true): bool
8579
{
@@ -118,6 +112,7 @@ protected function isSerialized($data, $strict = true): bool
118112
}
119113
}
120114
$token = $data[0];
115+
121116
switch ($token) {
122117
case 's':
123118
if ($strict) {
@@ -128,15 +123,19 @@ protected function isSerialized($data, $strict = true): bool
128123
return false;
129124
}
130125
// Or else fall through.
126+
// no break
131127
case 'a':
132128
case 'O':
133129
return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
130+
134131
case 'b':
135132
case 'i':
136133
case 'd':
137134
$end = $strict ? '$' : '';
138-
return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
135+
136+
return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data);
139137
}
138+
140139
return false;
141140
}
142141
}

src/Handlers/DatabaseHandler.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DatabaseHandler extends BaseHandler
2323
* Have the settings been read and cached
2424
* from the database yet?
2525
*
26-
* @var boolean
26+
* @var bool
2727
*/
2828
private $hydrated = false;
2929

@@ -40,9 +40,6 @@ class DatabaseHandler extends BaseHandler
4040
* read and stored in $this->settings the first
4141
* time, and then used from there the rest of the request.
4242
*
43-
* @param string $class
44-
* @param string $property
45-
*
4643
* @return mixed|null
4744
*/
4845
public function get(string $class, string $property)
@@ -59,9 +56,7 @@ public function get(string $class, string $property)
5956
/**
6057
* Stores values into the database for later retrieval.
6158
*
62-
* @param string $class
63-
* @param string $property
64-
* @param mixed $value
59+
* @param mixed $value
6560
*
6661
* @return mixed|void
6762
*/
@@ -84,14 +79,14 @@ public function set(string $class, string $property, $value = null)
8479
]);
8580
} else {
8681
$result = db_connect()->table($this->table)
87-
->insert([
88-
'class' => $class,
89-
'key' => $property,
90-
'value' => $value,
91-
'type' => $type,
92-
'created_at' => $time,
93-
'updated_at' => $time,
94-
]);
82+
->insert([
83+
'class' => $class,
84+
'key' => $property,
85+
'value' => $value,
86+
'type' => $type,
87+
'created_at' => $time,
88+
'updated_at' => $time,
89+
]);
9590
}
9691

9792
// Update our cache
@@ -112,9 +107,6 @@ public function set(string $class, string $property, $value = null)
112107
/**
113108
* Deletes the record from persistent storage, if found,
114109
* and from the local cache.
115-
*
116-
* @param string $class
117-
* @param string $property
118110
*/
119111
public function forget(string $class, string $property)
120112
{

src/Helpers/setting_helper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
/**
55
* Provides a convenience interface to the Settings service.
66
*
7-
* @param string|null $key
8-
* @param mixed|null $value
7+
* @param mixed|null $value
98
*
109
* @return mixed
1110
*/
12-
function setting(string $key = null, $value = null)
11+
function setting(?string $key = null, $value = null)
1312
{
1413
$setting = service('settings');
1514

src/Settings.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* retrieve settings that were original set in config files
88
* in the core application or any third-party module.
99
*/
10-
1110
class Settings
1211
{
1312
/**
@@ -51,8 +50,6 @@ public function __construct()
5150
* Retrieve a value from either the database
5251
* or from a config file matching the name
5352
* file.arg.optionalArg
54-
*
55-
* @param string $key
5653
*/
5754
public function get(string $key)
5855
{
@@ -73,8 +70,7 @@ public function get(string $key)
7370
/**
7471
* Save a value to the writable handler for later retrieval.
7572
*
76-
* @param string $key
77-
* @param mixed $value
73+
* @param mixed $value
7874
*
7975
* @return void|null
8076
*/
@@ -91,8 +87,6 @@ public function set(string $key, $value = null)
9187
* Removes a setting from the persistent storage,
9288
* effectively returning the value to the default value
9389
* found in the config file, if any.
94-
*
95-
* @param string $key
9690
*/
9791
public function forget(string $key)
9892
{
@@ -120,8 +114,6 @@ private function getWriteHandler()
120114
/**
121115
* Analyzes the given key and breaks it into the class.field parts.
122116
*
123-
* @param string $key
124-
*
125117
* @return string[]
126118
*/
127119
private function parseDotSyntax(string $key): array
@@ -139,10 +131,7 @@ private function parseDotSyntax(string $key): array
139131
/**
140132
* Given a key in class.property syntax, will split the values
141133
* and determine the fully qualified class name, if possible.
142-
*
143-
* @param string $key
144-
* @return array
145-
*/
134+
*/
146135
private function prepareClassAndProperty(string $key): array
147136
{
148137
[$class, $property] = $this->parseDotSyntax($key);

tests/HelperTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Sparks\Settings\Settings;
88
use Tests\Support\TestCase;
99

10-
class HelperTest extends TestCase
10+
/**
11+
* @internal
12+
*/
13+
final class HelperTest extends TestCase
1114
{
1215
use DatabaseTestTrait;
1316

14-
public function setUp(): void
17+
protected function setUp(): void
1518
{
1619
parent::setUp();
1720

@@ -41,7 +44,7 @@ public function testReturnsValueDotArray()
4144
'updated_at' => Time::now()->toDateTimeString(),
4245
]);
4346

44-
$this->assertEquals('baz', setting('Foo.bar'));
47+
$this->assertSame('baz', setting('Foo.bar'));
4548
}
4649

4750
public function testSettingValueDotArray()

0 commit comments

Comments
 (0)