Skip to content

Commit

Permalink
fix: Improve some coding
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Dec 24, 2024
1 parent b382bc2 commit 9b19f6b
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 34 deletions.
11 changes: 4 additions & 7 deletions src/Adapters/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Casbin\Persist\AdapterHelper;
use DateTime;
use Casbin\Exceptions\InvalidFilterTypeException;
use Illuminate\Support\Facades\DB;

/**
* DatabaseAdapter.
Expand All @@ -28,14 +27,14 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
/**
* @var bool
*/
private $filtered = false;
private bool $filtered = false;

/**
* Rules eloquent model.
*
* @var Rule
*/
protected $eloquent;
protected Rule $eloquent;

/**
* the DatabaseAdapter constructor.
Expand Down Expand Up @@ -331,10 +330,8 @@ public function loadFilteredPolicy(Model $model, $filter): void
}
$rows = $instance->get()->makeHidden(['created_at','updated_at', 'id'])->toArray();
foreach ($rows as $row) {
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
$line = implode(', ', array_filter($row, function ($val) {
return '' != $val && !is_null($val);
}));
$row = array_filter($row, static fn($value): bool => !is_null($value) && $value !== '');
$line = implode(', ', array_filter($row, static fn ($val): bool => '' != $val && !is_null($val)));
$this->loadPolicyLine(trim($line), $model);
}
$this->setFiltered(true);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/GroupAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GroupAdd extends Command
public function handle()
{
$params = explode(',', $this->argument('policy'));
array_walk($params, function (&$value) {
array_walk($params, static function (&$value): void {
$value = trim($value);
});
$ret = Enforcer::addGroupingPolicy(...$params);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PolicyAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PolicyAdd extends Command
public function handle()
{
$params = explode(',', $this->argument('policy'));
array_walk($params, function (&$value) {
array_walk($params, static function (&$value): void {
$value = trim($value);
});
$ret = Enforcer::addPolicy(...$params);
Expand Down
9 changes: 5 additions & 4 deletions src/EnforcerLocalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Foundation\Application;
use Lauthz\Facades\Enforcer;

class EnforcerLocalizer
{
/**
* The application instance.
*
* @var \Illuminate\Foundation\Application
* @var Application
*/
protected $app;
protected Application $app;

/**
* Create a new localizer instance.
*
* @param \Illuminate\Foundation\Application $app
* @param Application $app
*/
public function __construct($app)
public function __construct(Application $app)
{
$this->app = $app;
}
Expand Down
11 changes: 6 additions & 5 deletions src/EnforcerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Casbin\Log\Logger\DefaultLogger;
use Lauthz\Contracts\Factory;
use Lauthz\Models\Rule;
use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Lauthz\Loaders\ModelLoaderManager;
Expand All @@ -20,23 +21,23 @@ class EnforcerManager implements Factory
/**
* The application instance.
*
* @var \Illuminate\Foundation\Application
* @var \
*/
protected $app;
protected Application $app;

/**
* The array of created "guards".
*
* @var array
*/
protected $guards = [];
protected array $guards = [];

/**
* Create a new manager instance.
*
* @param \Illuminate\Foundation\Application $app
* @param Application $app
*/
public function __construct($app)
public function __construct(Application $app)
{
$this->app = $app;
}
Expand Down
12 changes: 3 additions & 9 deletions src/LauthzServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,11 @@ protected function bootObserver()
*/
public function register()
{
$this->app->singleton('enforcer', function ($app) {
return new EnforcerManager($app);
});
$this->app->singleton('enforcer', fn ($app) => new EnforcerManager($app));

$this->app->singleton(ModelLoaderManager::class, function ($app) {
return new ModelLoaderManager($app);
});
$this->app->singleton(ModelLoaderManager::class, fn ($app) => new ModelLoaderManager($app));

$this->app->singleton(EnforcerLocalizer::class, function ($app) {
return new EnforcerLocalizer($app);
});
$this->app->singleton(EnforcerLocalizer::class, fn ($app) => new EnforcerLocalizer($app));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FileLoader implements ModelLoader
*
* @var string
*/
private $filePath;
private string $filePath;

/**
* Constructor to initialize the file path.
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/ModelLoaderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ModelLoaderManager extends Manager
*
* @var array
*/
protected $config;
protected array $config;

/**
* Initialize configuration for the loader manager instance.
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/TextLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TextLoader implements ModelLoader
*
* @var string
*/
private $text;
private string $text;

/**
* Constructor to initialize the model text.
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/UrlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UrlLoader implements ModelLoader
*
* @var string
*/
private $url;
private string $url;

/**
* Constructor to initialize the url path.
Expand Down
4 changes: 1 addition & 3 deletions src/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public function __construct(array $attributes = [], $guard = '')
*/
public function getAllFromCache()
{
$get = function () {
return $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
};
$get = fn () => $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
if (!$this->config('cache.enabled', false)) {
return $get();
}
Expand Down

0 comments on commit 9b19f6b

Please sign in to comment.