Skip to content

Commit

Permalink
style: fix code style issues reported by phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
a1383n committed Oct 13, 2023
1 parent 56cdd80 commit 05094ff
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.idea
composer.phar
/vendor/
.idea
composer.lock
/phpunit.xml
.phpunit.result.cache
.php-cs-fixer.cache
1 change: 0 additions & 1 deletion .php-cs-fixer.cache

This file was deleted.

40 changes: 39 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"LaravelEloquentSettings\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"authors": [
{
"name": "Amirmohammad Nafariyeh",
Expand All @@ -25,10 +33,40 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"require": {
"php": "^8.1",
"laravel/framework": "^9.0 || ^10.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.26",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.23|^2.18",
"phpstan/phpstan": "^1.10"
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"baseline": "vendor/bin/phpstan analyse --generate-baseline",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"test": "vendor/bin/pest",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/phpstan analyse"
]
}
}
11 changes: 11 additions & 0 deletions src/Contracts/EloquentSettingModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LaravelEloquentSettings\Contracts;

use LaravelEloquentSettings\Enums\SettingValueType;

/**
* Interface EloquentSettingModelInterface
*
Expand All @@ -22,4 +24,13 @@ public function getName(): string;
* @return mixed
*/
public function getValue(): mixed;

/**
* Set the value type
*
* @param SettingValueType $type
*
* @return self
*/
public function setType(SettingValueType $type): self;
}
4 changes: 3 additions & 1 deletion src/EloquentSettingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\Model;
use LaravelEloquentSettings\Contracts\SettingHandlerInterface;
use LaravelEloquentSettings\Contracts\SettingRepositoryInterface;
use LaravelEloquentSettings\Models\EloquentSetting;
use LaravelEloquentSettings\Traits\HasSettings;

/**
* Class EloquentSettingManager
Expand All @@ -33,7 +35,7 @@ public function getHandler(Model $model): SettingHandlerInterface
return $container->make(
SettingHandlerInterface::class,
[
'repository' => $container->make(SettingRepositoryInterface::class, ['morphManyRelation' => $model->settings()]),
'repository' => $container->make(SettingRepositoryInterface::class, ['morphManyRelation' => $model->{'settings'}()]),
]
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Models/EloquentSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use LaravelEloquentSettings\Contracts\EloquentSettingModelInterface;
use LaravelEloquentSettings\Enums\SettingValueType;

/**
* Class EloquentSetting
Expand All @@ -25,21 +26,35 @@ class EloquentSetting extends Model implements EloquentSettingModelInterface
'value' => 'string',
];

/**
* @param array<string, mixed> $attributes
*/
public function __construct(array $attributes = [])
{
// Set the table name from the configuration or use the default value
/**
* @phpstan-ignore-next-line
*/
$this->table = config('eloquent_settings.table_name', 'eloquent_settings');

parent::__construct($attributes);
}

public function getName(): string
{
/**
* @phpstan-ignore-next-line
*/
return $this->getAttribute('name');
}

public function getValue(): mixed
{
return $this->getAttribute('value');
}

public function setType(SettingValueType $type): EloquentSettingModelInterface
{
return $this->mergeCasts(['value' => $type->value]);
}
}
5 changes: 4 additions & 1 deletion src/Repositories/SettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SettingRepository implements SettingRepositoryInterface
/**
* SettingRepository constructor.
*
* @param MorphMany $morphManyRelation The morphMany relation for the settings.
* @param MorphMany<EloquentSetting> $morphManyRelation The morphMany relation for the settings.
*/
public function __construct(protected readonly MorphMany $morphManyRelation)
{
Expand Down Expand Up @@ -69,6 +69,9 @@ public function updateSettingByName(string $name, SettingValueType $type, mixed
*/
public function createSettingByName(string $name, SettingValueType $type, mixed $value): EloquentSettingModelInterface
{
/**
* @phpstan-ignore-next-line
*/
$model = (new EloquentSetting(['name' => $name]))
->mergeCasts(['value' => $type->value])
->setAttribute('value', $value)
Expand Down
4 changes: 2 additions & 2 deletions src/SettingDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class SettingDefinition
{
/**
* @var array
* @var array<string, SettingDefinitionItem>
*/
protected array $definitions = [];

Expand All @@ -30,7 +30,7 @@ public function define(string $name): SettingDefinitionItem
/**
* Get all defined setting definitions.
*
* @return array
* @return array<string, SettingDefinitionItem>
*/
public function getDefinitions(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/SettingDefinitionEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LaravelEloquentSettings;

use Illuminate\Contracts\Validation\ValidationRule;
use LaravelEloquentSettings\Enums\SettingValueType;

/**
Expand All @@ -21,7 +22,7 @@ class SettingDefinitionEntity
* @param mixed $default
* @param bool $insertOnDefault
* @param bool $nullable
* @param array|null $customValidationRules
* @param array<string|ValidationRule>|null $customValidationRules
*/
public function __construct(
public readonly string $name,
Expand Down
7 changes: 6 additions & 1 deletion src/SettingDefinitionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LaravelEloquentSettings;

use Illuminate\Contracts\Validation\ValidationRule;
use LaravelEloquentSettings\Enums\SettingValueType;

/**
Expand All @@ -18,6 +19,10 @@ class SettingDefinitionItem
protected mixed $default = null;
protected bool $insertOnDefault = true;
protected bool $nullable = false;

/**
* @var array<string|ValidationRule>|null
*/
protected ?array $customValidationRules = null;

/**
Expand Down Expand Up @@ -94,7 +99,7 @@ public function disableInsertOnDefault(): self
/**
* Set custom validation rules for the setting.
*
* @param array $rules
* @param array<string|ValidationRule> $rules
* @return $this
*/
public function validationRules(array $rules): self
Expand Down
2 changes: 1 addition & 1 deletion src/SettingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __invoke(SettingDefinitionEntity $entity): mixed
{
try {
return $this->handler->getSetting($entity)
->mergeCasts(['value' => $entity->type->value])
->setType($entity->type)
->getValue();
} catch (ModelNotFoundException) {
// If the setting does not exist, create it with the default value after return response
Expand Down

0 comments on commit 05094ff

Please sign in to comment.