Skip to content

Commit b068fb4

Browse files
committed
fix: Improve some coding
1 parent 3b34d67 commit b068fb4

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

src/adapter/DatabaseAdapter.php

+17-20
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
use tauthz\model\Rule;
66
use tauthz\cache\CacheHandlerContract;
77
use Casbin\Model\Model;
8-
use Casbin\Persist\Adapter;
9-
use Casbin\Persist\AdapterHelper;
10-
use Casbin\Persist\UpdatableAdapter;
11-
use Casbin\Persist\BatchAdapter;
12-
use Casbin\Persist\FilteredAdapter;
8+
use Casbin\Persist\{Adapter, AdapterHelper, UpdatableAdapter, BatchAdapter, FilteredAdapter};
139
use Casbin\Persist\Adapters\Filter;
1410
use Casbin\Exceptions\InvalidFilterTypeException;
1511
use tauthz\traits\Configurable;
@@ -27,21 +23,21 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
2723
/**
2824
* @var bool
2925
*/
30-
private $filtered = false;
26+
private bool $filtered = false;
3127

3228
/**
3329
* Rules model.
3430
*
3531
* @var Rule
3632
*/
37-
protected $model;
33+
protected Rule $model;
3834

3935
/**
4036
* Cache Handler.
4137
*
4238
* @var CacheHandlerContract
4339
*/
44-
protected $cacheHandler;
40+
protected CacheHandlerContract $cacheHandler;
4541

4642
/**
4743
* the DatabaseAdapter constructor.
@@ -84,12 +80,13 @@ public function filterRule(array $rule): array
8480
*
8581
* @return void
8682
*/
87-
public function savePolicyLine($ptype, array $rule)
83+
public function savePolicyLine(string $ptype, array $rule): void
8884
{
8985
$col['ptype'] = $ptype;
9086
foreach ($rule as $key => $value) {
91-
$col['v'.strval($key).''] = $value;
87+
$col['v' . strval($key) . ''] = $value;
9288
}
89+
9390
$this->cacheHandler->cachePolicies($this->model)->insert($col);
9491
}
9592

@@ -100,7 +97,8 @@ public function savePolicyLine($ptype, array $rule)
10097
*/
10198
public function loadPolicy(Model $model): void
10299
{
103-
$rows = $this->cacheHandler->cachePolicies($this->model)->field(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'])->select()->toArray();
100+
$rows = $this->cacheHandler->cachePolicies($this->model)->field(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'])
101+
->select()->toArray();
104102
foreach ($rows as $row) {
105103
$this->loadPolicyArray($this->filterRule($row), $model);
106104
}
@@ -160,6 +158,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
160158
$cols[$i++] = $temp;
161159
$temp = [];
162160
}
161+
163162
$this->cacheHandler->cachePolicies($this->model)->insertAll($cols);
164163
}
165164

@@ -177,7 +176,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
177176
$instance = $this->model->where('ptype', $ptype);
178177

179178
foreach ($rule as $key => $value) {
180-
$instance->where('v'.strval($key), $value);
179+
$instance->where('v' . strval($key), $value);
181180
}
182181

183182
foreach ($instance->select() as $model) {
@@ -234,7 +233,7 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
234233
++$count;
235234
}
236235
}
237-
236+
238237
return $removedRules;
239238
}
240239

@@ -271,7 +270,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
271270

272271
foreach ($newPolicy as $key => $value) {
273272
$column = 'v' . strval($key);
274-
$instance->$column = $value;
273+
$instance->{$column} = $value;
275274
}
276275

277276
$instance->save();
@@ -360,14 +359,12 @@ public function loadFilteredPolicy(Model $model, $filter): void
360359
}
361360
$rows = $instance->select()->hidden(['id'])->toArray();
362361
foreach ($rows as $row) {
363-
$row = array_filter($row, function ($value) {
364-
return !is_null($value) && $value !== '';
365-
});
366-
$line = implode(', ', array_filter($row, function ($val) {
367-
return '' != $val && !is_null($val);
368-
}));
362+
$row = array_filter($row, fn ($value) => !is_null($value) && $value !== '');
363+
$line = implode(', ', array_filter($row, fn ($val) => '' != $val && !is_null($val)));
364+
369365
$this->loadPolicyLine(trim($line), $model);
370366
}
367+
371368
$this->setFiltered(true);
372369
}
373370
}

src/command/Publish.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace tauthz\command;
44

5-
use think\console\Command;
6-
use think\console\Input;
7-
use think\console\Output;
5+
use think\console\{Command, Input, Output};
86

97
/**
108
* 发布配置文件、迁移文件指令

src/facade/Enforcer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
class Enforcer extends Facade
3434
{
3535
/**
36-
* 获取当前Facade对应类名(或者已经绑定的容器对象标识)
36+
* 获取当前 Facade 对应类名(或者已经绑定的容器对象标识)
37+
*
3738
* @access protected
3839
* @return string
3940
*/

src/model/Rule.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class Rule extends Model implements Arrayable
2929
];
3030
/**
3131
* 架构函数
32-
* @access public
33-
* @param array $data 数据
32+
*
33+
* @param array|object $data 数据
3434
*/
35-
public function __construct($data = [])
35+
public function __construct(array|object $data = [])
3636
{
3737
$this->connection = $this->config('database.connection') ?: '';
3838
$this->table = $this->config('database.rules_table');

0 commit comments

Comments
 (0)