Skip to content

Commit

Permalink
fix(any): fix all error for phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Apr 16, 2020
1 parent 4836d79 commit 997f484
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 168 deletions.
4 changes: 1 addition & 3 deletions application/admin/App/Controller/Support/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ trait Controller
{
/**
* 调用服务.
*
* @param object $service
*/
private function main(Request $request, $service): array
private function main(Request $request, object $service): array
{
return $service->handle($this->input($request));
}
Expand Down
2 changes: 1 addition & 1 deletion application/admin/App/Service/Login/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class Logout
{
public function handle()
public function handle(): void
{
Auth::logout();
}
Expand Down
95 changes: 0 additions & 95 deletions application/admin/Infra/Auth.php

This file was deleted.

76 changes: 64 additions & 12 deletions application/admin/Infra/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,82 @@

namespace Admin\Infra;

use Leevel\Cache\Proxy\Cache;

/**
* 权限存储.
* 校验权限.
*/
class Permission
{
/**
* 设置权限.
* 权限数据.
*
* @var array
*/
private array $permission = [];

/**
* 构造函数.
*
* @param string $permission
* @param \Admin\Infra\PermissionCache $permission
*/
public function set(string $id, array $permission): void
public function __construct(PermissionCache $permission, string $token)
{
Cache::set('permission_'.$id, $permission);
$this->permission = $permission->get($token);

if (!\is_array($this->permission['static'])) {
$this->permission['static'] = [];
}

if (!\is_array($this->permission['dynamic'])) {
$this->permission['dynamic'] = [];
}
}

/**
* 获取权限.
*
* @return string
* 校验权限.
*/
public function handle(string $resource, ?string $method = null): bool
{
// 超级管理员
if (\in_array('*', $this->permission['static'], true)) {
return true;
}

// 所有请求
if (\in_array($resource, $this->permission['static'], true)) {
return true;
}

// 带有请求类型
if ($method && \in_array($method.':'.$resource, $this->permission['static'], true)) {
return true;
}

// 动态权限
foreach ($this->permission['dynamic'] as $p) {
$p = $this->prepareRegexForWildcard($p);

// 无请求类型
if (preg_match($p, $resource, $res)) {
return true;
}

// 带有请求类型
if ($method && preg_match($p, $method.':'.$resource, $res)) {
return true;
}
}

return false;
}

/**
* 通配符正则.
*/
public function get(string $id): array
private function prepareRegexForWildcard(string $regex): string
{
return Cache::get('permission_'.$id) ?: ['static' => [], 'dynamic' => []];
$regex = preg_quote($regex, '/');
$regex = '/^'.str_replace('\*', '(\S*)', $regex).'$/';

return $regex;
}
}
39 changes: 39 additions & 0 deletions application/admin/Infra/PermissionCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the your app package.
*
* The PHP Application For Code Poem For You.
* (c) 2018-2099 http://yourdomian.com All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Admin\Infra;

use Leevel\Cache\Proxy\Cache;

/**
* 权限缓存.
*/
class PermissionCache
{
/**
* 设置权限.
*/
public function set(string $id, array $permission): void
{
Cache::set('permission_'.$id, $permission);
}

/**
* 获取权限.
*/
public function get(string $id): array
{
return Cache::get('permission_'.$id) ?: ['static' => [], 'dynamic' => []];
}
}
4 changes: 2 additions & 2 deletions application/app/App/Controller/Coroutine/WaitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ class BaseWaitGroup
/**
* 通道.
*
* @var \Swoole\Coroutine\Channel[]
* @var \Swoole\Coroutine\Channel
*/
private $chan;
private Channel $chan;

public function __construct()
{
Expand Down
6 changes: 3 additions & 3 deletions application/app/App/Controller/Petstore/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Api
*
* @param mixed $petId
*/
public function petLeevelForApi($petId)
public function petLeevelForApi($petId): string
{
return sprintf('Hi you,i am petLeevelForApi and it petId is %s', $petId);
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public function petLeevelForApi($petId)
* }
* )
*/
public function petLeevelV2ForApi()
public function petLeevelV2ForApi(): void
{
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public function petLeevelV2ForApi()
* leevelIgnore=true
* )
*/
public function petLeevelIgnoreForApi()
public function petLeevelIgnoreForApi(): void
{
}
}
22 changes: 11 additions & 11 deletions application/app/App/Controller/Petstore/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Pet
*
* @param mixed $petId
*/
public function withBind($petId)
public function withBind($petId): string
{
return sprintf('Hi you,i am withBind and it petId is %s', $petId);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public function withBind($petId)
* leevelMiddlewares="api"
* )
*/
public function petLeevel()
public function petLeevel(): void
{
}

Expand Down Expand Up @@ -117,7 +117,7 @@ public function petLeevel()
* leevelIgnore=true
* )
*/
public function petLeevelIgnore()
public function petLeevelIgnore(): void
{
}

Expand All @@ -137,7 +137,7 @@ public function petLeevelIgnore()
* requestBody={"$ref": "#/components/requestBodies/Pet"}
* )
*/
public function addPet()
public function addPet(): void
{
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public function addPet()
* requestBody={"$ref": "#/components/requestBodies/Pet"}
* )
*/
public function updatePet()
public function updatePet(): void
{
}

Expand Down Expand Up @@ -223,7 +223,7 @@ public function updatePet()
* }
* )
*/
public function findPetsByStatus()
public function findPetsByStatus(): void
{
}

Expand Down Expand Up @@ -280,7 +280,7 @@ public function findPetsByStatus()
* }
* )
*/
public function findByTags()
public function findByTags(): void
{
}

Expand Down Expand Up @@ -332,7 +332,7 @@ public function findByTags()
*
* @param int $id
*/
public function getPetById($id)
public function getPetById($id): void
{
}

Expand Down Expand Up @@ -380,7 +380,7 @@ public function getPetById($id)
* )
* )
*/
public function updatePetWithForm()
public function updatePetWithForm(): void
{
}

Expand Down Expand Up @@ -421,7 +421,7 @@ public function updatePetWithForm()
* },
* )
*/
public function deletePet()
public function deletePet(): void
{
}

Expand Down Expand Up @@ -467,7 +467,7 @@ public function deletePet()
* )
* )
*/
public function uploadFile()
public function uploadFile(): void
{
}
}
Loading

0 comments on commit 997f484

Please sign in to comment.