Skip to content

Commit

Permalink
Apply coding standard
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Nov 6, 2021
1 parent fc0812e commit a8f63e6
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 96 deletions.
10 changes: 1 addition & 9 deletions .php-cs-fixer.dist.php
Expand Up @@ -11,24 +11,16 @@
->append([__FILE__]);

// Remove overrides for incremental changes
$overrides = [
'array_indentation' => false,
'braces' => false,
'indentation_type' => false,
];
$overrides = [];

$options = [
'finder' => $finder,
'cacheFile' => 'build/.php-cs-fixer.cache',
];

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

/* Reenable For libraries after incremental changes are applied
return Factory::create(new CodeIgniter4(), $overrides, $options)->forLibrary(
'Tatter Settings',
'Tatter Software',
'',
2021
);
*/
12 changes: 4 additions & 8 deletions src/Commands/SettingsAdd.php
Expand Up @@ -19,15 +19,11 @@

class SettingsAdd extends BaseCommand
{
protected $group = 'Settings';

protected $name = 'settings:add';

protected $group = 'Settings';
protected $name = 'settings:add';
protected $description = 'Adds a setting template to the database.';

protected $usage = 'settings:add [name] [datatype] [summary] [content] [protected]';

protected $arguments = [
protected $usage = 'settings:add [name] [datatype] [summary] [content] [protected]';
protected $arguments = [
'name' => 'The name of the setting (e.g. "timezone")',
'summary' => "A brief summary of this setting's purpose",
'content' => 'The default value for the setting',
Expand Down
6 changes: 2 additions & 4 deletions src/Commands/SettingsList.php
Expand Up @@ -17,10 +17,8 @@

class SettingsList extends BaseCommand
{
protected $group = 'Settings';

protected $name = 'settings:list';

protected $group = 'Settings';
protected $name = 'settings:list';
protected $description = 'Lists setting templates from the database.';

public function run(array $params)
Expand Down
3 changes: 0 additions & 3 deletions src/Config/Services.php
Expand Up @@ -16,9 +16,6 @@

class Services extends BaseService
{
/**
* @param bool $getShared
*/
public static function settings(bool $getShared = true)
{
if ($getShared) {
Expand Down
2 changes: 0 additions & 2 deletions src/Config/Settings.php
Expand Up @@ -19,8 +19,6 @@ class Settings extends BaseConfig
* Matches config value requests to its
* corresponding Setting template.
*
* @param string $name
*
* @return mixed Null for non-existant templates
*/
public function __get(string $name)
Expand Down
13 changes: 3 additions & 10 deletions src/Entities/Setting.php
Expand Up @@ -15,16 +15,13 @@

class Setting extends Entity
{
protected $table = 'settings';

protected $table = 'settings';
protected $primaryKey = 'id';

protected $dates = [
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];

protected $casts = [
'id' => '?int',
'protected' => 'bool',
Expand All @@ -33,10 +30,8 @@ class Setting extends Entity
/**
* Forces the content to cast
* to its predefined datatype.
*
* @param array|null $data
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setContentCast($data['datatype'] ?? 'string');

Expand All @@ -47,8 +42,6 @@ public function __construct(array $data = null)
* Sets the cast datatype for
* the content field.
*
* @param string $datatype
*
* @return $this
*/
public function setContentCast(string $datatype = 'string')
Expand Down
34 changes: 8 additions & 26 deletions src/Models/SettingModel.php
Expand Up @@ -18,38 +18,28 @@

class SettingModel extends Model
{
protected $table = 'settings';

protected $primaryKey = 'id';

protected $returnType = Setting::class;

protected $useTimestamps = true;

protected $table = 'settings';
protected $primaryKey = 'id';
protected $returnType = Setting::class;
protected $useTimestamps = true;
protected $useSoftDeletes = true;

protected $skipValidation = false;

protected $allowedFields = [
protected $allowedFields = [
'name',
'datatype',
'summary',
'content',
'protected',
];

protected $validationRules = [
'name' => 'required|max_length[63]',
'datatype' => 'required|max_length[31]',
'summary' => 'permit_empty|max_length[255]',
'content' => 'permit_empty|max_length[255]',
'protected' => 'in_list[0,1]',
];

protected $afterInsert = ['clearTemplates'];

protected $afterUpdate = ['clearTemplates'];

protected $afterDelete = ['clearTemplates'];

/**
Expand Down Expand Up @@ -95,7 +85,7 @@ public function getTemplates(): array
$ttl = config('Cache')->ttl ?? 5 * MINUTE;

// If caching is disabled or no cache is matched...
if (is_null($ttl) || null === $templates = cache('settings-templates')) {
if (null === $ttl || null === $templates = cache('settings-templates')) {
// ... then have to load from the database instead
$templates = $this->builder()
->select(['id', 'name', 'datatype', 'content', 'protected'])
Expand All @@ -108,6 +98,7 @@ public function getTemplates(): array

// Convert the arrays to Setting entities and index by name
self::$templates = [];

foreach ($templates as $template) {
self::$templates[$template['name']] = (new Setting())->setContentCast($template['datatype'])->setAttributes($template);
}
Expand All @@ -118,8 +109,6 @@ public function getTemplates(): array
/**
* Retrieves all of a user's overrides.
*
* @param int $userId
*
* @return array<int,mixed>
*/
public function getOverrides(int $userId): array
Expand All @@ -130,6 +119,7 @@ public function getOverrides(int $userId): array

// Load from the database
self::$overrides[$userId] = [];

foreach ($this->builder('settings_users')->where('user_id', $userId)->get()->getResultArray() as $override) {
self::$overrides[$userId][$override['setting_id']] = $override['content'];
}
Expand All @@ -140,11 +130,7 @@ public function getOverrides(int $userId): array
/**
* Sets a user override for a Setting.
*
* @param int $settingId
* @param int $userId
* @param mixed $content
*
* @return void
*/
public function setOverride(int $settingId, int $userId, $content): void
{
Expand Down Expand Up @@ -172,10 +158,6 @@ public function setOverride(int $settingId, int $userId, $content): void

/**
* Faked data for Fabricator.
*
* @param Generator $faker
*
* @return Setting
*/
public function fake(Generator &$faker): Setting
{
Expand Down
29 changes: 7 additions & 22 deletions src/Settings.php
Expand Up @@ -21,21 +21,19 @@
*/
class Settings
{
/**
* @var SettingModel
*/
protected $model;
/**
* @var SettingModel
*/
protected $model;

public function __construct(?SettingModel $model = null)
{
$this->model = $model ?? model(SettingModel::class); // @phpstan-ignore-line
$this->model = $model ?? model(SettingModel::class); // @phpstan-ignore-line
}

/**
* Magic getter.
*
* @param string $name
*
* @return mixed
*/
public function __get(string $name)
Expand All @@ -48,10 +46,7 @@ public function __get(string $name)
/**
* Magic setter for changing a setting.
*
* @param string $name
* @param mixed|null $content
*
* @return void
*/
public function __set(string $name, $content): void
{
Expand All @@ -64,8 +59,6 @@ public function __set(string $name, $content): void
* and it is fairly restrictive we use
* its validation.
*
* @param string $name
*
* @throws InvalidArgumentException
*
* @return string The validated name
Expand All @@ -81,10 +74,6 @@ public static function validate(string $name): string

/**
* Gets a Setting template, if it exists.
*
* @param string $name
*
* @return Setting|null
*/
public function getTemplate(string $name): ?Setting
{
Expand All @@ -98,8 +87,6 @@ public function getTemplate(string $name): ?Setting
/**
* Retrieves Setting content by its name.
*
* @param string $name
*
* @return mixed|null
*/
public function get(string $name)
Expand Down Expand Up @@ -135,8 +122,7 @@ public function get(string $name)
/**
* Updates a Setting value.
*
* @param string $name
* @param mixed $content Null to remove
* @param mixed $content Null to remove
*
* @return $this
*/
Expand Down Expand Up @@ -174,8 +160,7 @@ public function set(string $name, $content): self
* Always called when retrieving a value to improve chances
* at a hit next time.
*
* @param string $name
* @param mixed $content
* @param mixed $content
*
* @return mixed
*/
Expand Down
4 changes: 1 addition & 3 deletions tests/LibraryTest.php
Expand Up @@ -12,7 +12,6 @@
use CodeIgniter\Test\DatabaseTestTrait;
use Myth\Auth\Test\AuthTestTrait;
use Tatter\Settings\Entities\Setting;
use Tatter\Settings\Models\SettingModel;
use Tatter\Settings\Settings;
use Tests\Support\SettingsTestCase;

Expand All @@ -25,8 +24,7 @@ final class LibraryTest extends SettingsTestCase
use DatabaseTestTrait;

protected $migrateOnce = true;

protected $seedOnce = true;
protected $seedOnce = true;

protected function tearDown(): void
{
Expand Down
1 change: 0 additions & 1 deletion tests/ModelTest.php
Expand Up @@ -9,7 +9,6 @@
* the LICENSE file that was distributed with this source code.
*/

use Tatter\Settings\Models\SettingModel;
use Tests\Support\SettingsTestCase;

/**
Expand Down
16 changes: 8 additions & 8 deletions tests/_support/SettingsTestCase.php
Expand Up @@ -35,10 +35,10 @@ abstract class SettingsTestCase extends CIUnitTestCase
*/
protected $seed = SettingSeeder::class;

/**
* @var SettingModel
*/
protected $model;
/**
* @var SettingModel
*/
protected $model;

/**
* Initializes required helpers.
Expand All @@ -54,10 +54,10 @@ protected function setUp(): void
{
parent::setUp();

/** @var SettingModel $model */
$model = model(SettingModel::class);
$model->clearTemplates();
/** @var SettingModel $model */
$model = model(SettingModel::class);
$model->clearTemplates();

$this->model = $model;
$this->model = $model;
}
}

0 comments on commit a8f63e6

Please sign in to comment.