Skip to content

Commit 74c4132

Browse files
committed
feature: solve phpstan 9
1 parent 469f7aa commit 74c4132

9 files changed

Lines changed: 29 additions & 20 deletions

File tree

src/Amis.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function renderApp(array $schema = [])
4242
'view_path' => '../vendor/webman-tech/amis-admin/src', // 相对 app 目录
4343
'assets' => $this->getAssets(),
4444
];
45-
$appData = ConfigHelper::get('app', []);
45+
$appData = (array)ConfigHelper::get('app', []);
4646
if (isset($appData['amisJSON']) && is_callable($appData['amisJSON'])) {
4747
$appData['amisJSON'] = call_user_func($appData['amisJSON']);
4848
}
@@ -77,7 +77,7 @@ public function renderPage(string $title, array $schema = [])
7777
'view_path' => '../vendor/webman-tech/amis-admin/src', // 相对 app 目录
7878
'assets' => $this->getAssets(),
7979
];
80-
$pageData = ConfigHelper::get('page', []);
80+
$pageData = (array)ConfigHelper::get('page', []);
8181
if (isset($pageData['amisJSON']) && is_callable($pageData['amisJSON'])) {
8282
$pageData['amisJSON'] = call_user_func($pageData['amisJSON']);
8383
}
@@ -107,15 +107,18 @@ public function renderPage(string $title, array $schema = [])
107107
public function getRequestPath(Request $request): string
108108
{
109109
if ($requestPathGetter = ConfigHelper::get('request_path_getter')) {
110-
return $requestPathGetter($request);
110+
if (!is_callable($requestPathGetter)) {
111+
throw new \InvalidArgumentException('request_path_getter 必须是个 callable');
112+
}
113+
return (string)$requestPathGetter($request);
111114
}
112115

113116
return $request->path();
114117
}
115118

116119
private function getAssets(): array
117120
{
118-
$assets = ConfigHelper::get('assets', []);
121+
$assets = (array)ConfigHelper::get('assets', []);
119122

120123
$assets['js'] = $assets['js'] ?? [];
121124
if (is_callable($assets['js'])) {
@@ -133,12 +136,12 @@ private function getAssets(): array
133136

134137
$assets['lang'] = $assets['lang'] ?? 'zh';
135138
if (is_callable($assets['lang'])) {
136-
$assets['lang'] = call_user_func($assets['lang']);
139+
$assets['lang'] = (string)call_user_func($assets['lang']);
137140
}
138141

139142
$assets['locale'] = $assets['locale'] ?? 'zh-CN';
140143
if (is_callable($assets['locale'])) {
141-
$assets['locale'] = call_user_func($assets['locale']);
144+
$assets['locale'] = (string)call_user_func($assets['locale']);
142145
}
143146

144147
return $assets;

src/Amis/Component.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __construct()
3737
public static function make(array $schema = null)
3838
{
3939
/** @var static $component */
40+
/** @phpstan-ignore-next-line */
4041
$component = clone Container::get(static::class);
4142
if ($schema) {
4243
$component->schema($schema);
@@ -133,13 +134,13 @@ protected function merge(array ...$arrays): array
133134
protected function mergeGlobalConfigWithType(string $type, array $schema): array
134135
{
135136
$componentTypeName = 'type' . Str::studly($type);
136-
$componentConfig = ConfigHelper::get('components.' . $componentTypeName, [], true);
137+
$componentConfig = (array)ConfigHelper::get('components.' . $componentTypeName, [], true);
137138
if ($componentConfig === [] && Str::contains($type, 'static-')) {
138139
// 支持兼容 typeStaticImage 到 typeImage 的配置
139140
$componentTypeName = str_replace('typeStatic', 'type', $componentTypeName);
140-
$componentConfig = ConfigHelper::get('components.' . $componentTypeName, [], true);
141+
$componentConfig = (array)ConfigHelper::get('components.' . $componentTypeName, [], true);
141142
}
142-
if ($globalSchema = $componentConfig['schema'] ?? []) {
143+
if (($globalSchema = $componentConfig['schema'] ?? []) && is_array($globalSchema)) {
143144
if (isset($globalSchema['type'])) {
144145
$schema['type'] = $globalSchema['type']; // 允许做全局的 type 修改,这样可以做自定义组件
145146
}

src/Amis/Crud.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function withColumns(array $columns)
5656
foreach ($columns as $index => &$column) {
5757
if ($column instanceof GridColumnActions) {
5858
// 去除操作栏为空 buttons 的
59-
if (count($column->get('buttons', [])) <= 0) {
59+
if (count((array)$column->get('buttons', [])) <= 0) {
6060
unset($columns[$index]);
6161
continue;
6262
}

src/Controller/AmisSourceController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function index(Request $request): Response
6868
$order[$orderBy] = $request->get('orderDir', 'asc');
6969
}
7070
return amis_response($this->repository()->pagination(
71-
$request->get('page'),
72-
$request->get('perPage'),
73-
array_filter($request->get(), fn($item) => $item !== ''), // 仅过滤空字符串的,保留为 0 的情况
71+
(int)$request->get('page'),
72+
(int)$request->get('perPage'),
73+
array_filter((array)$request->get(), fn($item) => $item !== ''), // 仅过滤空字符串的,保留为 0 的情况
7474
$order
7575
));
7676
}

src/Controller/Traits/AmisSourceController/UpdateTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function update(Request $request, $id): Response
2222
if (!$this->authUpdate($id)) {
2323
throw new ActionDisableException();
2424
}
25-
$this->repository()->update($request->post(), $id);
25+
$this->repository()->update((array)$request->post(), $id);
2626
return amis_response(['result' => 'ok']);
2727
}
2828

src/Helper/DTO/PresetItemDTO.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function buildRule($value)
215215
if (is_string($value)) {
216216
$value = array_values(array_filter(explode('|', $value)));
217217
}
218-
if ($this->scene === AbsRepository::SCENE_UPDATE) {
218+
if ($this->scene === AbsRepository::SCENE_UPDATE && is_array($value)) {
219219
// 为了保证 update 场景下,可能需要部分字段更新(quickEdit),此时给字段默认添加 sometimes 规则
220220
if (!in_array(self::SCENE_UPDATE_APPEND_RULE, $value, true)) {
221221
array_unshift($value, self::SCENE_UPDATE_APPEND_RULE);
@@ -247,6 +247,7 @@ protected function getSelectOptionsInfo(): ?array
247247
if (!is_array($defineSelectOptions)) {
248248
throw new \InvalidArgumentException('selectOptions must be an array or Closure return array');
249249
}
250+
/** @phpstan-ignore-next-line */
250251
if (isset($defineSelectOptions[0]['value'])) {
251252
// 二维数组的形式
252253
$data['options'] = $defineSelectOptions;
@@ -257,7 +258,7 @@ protected function getSelectOptionsInfo(): ?array
257258
foreach ($defineSelectOptions as $value => $label) {
258259
$data['options'][] = [
259260
'value' => (string)$value, // 强制为 string,保证行为一致
260-
'label' => strip_tags($label), // 去除 html
261+
'label' => strip_tags((string)$label), // 去除 html
261262
];
262263
}
263264
}
@@ -273,7 +274,7 @@ protected function getSelectOptionsInfo(): ?array
273274
protected function callExt(string $name, $value, bool $useScene = false)
274275
{
275276
$ext = $this->getDefineValueByName($name) ?? null;
276-
if ($ext) {
277+
if ($ext && is_callable($ext)) {
277278
if ($useScene) {
278279
$result = $ext($value, $this->scene);
279280
} else {

src/Repository/AbsRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected function validator(): ValidatorInterface
170170
if ($this->validator) {
171171
return $this->validator;
172172
}
173-
if ($validator = ConfigHelper::get('validator')) {
173+
if (($validator = ConfigHelper::get('validator')) && is_callable($validator)) {
174174
$this->validator = call_user_func($validator);
175175
if (!$this->validator instanceof ValidatorInterface) {
176176
throw new \InvalidArgumentException('validator must be instance of ' . ValidatorInterface::class);

src/Repository/EloquentRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ protected function buildSearch(EloquentBuilder $query, array $search): EloquentB
113113
$searchableAttributes = $this->searchableAttributes();
114114
foreach ($search as $attribute => $value) {
115115
if (array_key_exists($attribute, $searchableAttributes) && $value !== '' && $value !== null) {
116-
$query = call_user_func($searchableAttributes[$attribute], $query, $value, $attribute);
116+
$newQuery = call_user_func($searchableAttributes[$attribute], $query, $value, $attribute);
117+
if ($newQuery instanceof EloquentBuilder) {
118+
return $newQuery;
119+
}
117120
}
118121
}
119122
return $query;

src/helper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22

3-
use WebmanTech\AmisAdmin\Amis;
43
use support\Container;
54
use Webman\Http\Response;
5+
use WebmanTech\AmisAdmin\Amis;
66

77
if (!function_exists('amis')) {
88
/**
99
* @return Amis
1010
*/
1111
function amis(): Amis
1212
{
13+
/** @phpstan-ignore-next-line */
1314
return Container::get(Amis::class);
1415
}
1516
}

0 commit comments

Comments
 (0)