Skip to content

Commit 8e5a35d

Browse files
committed
代码质量优化
1 parent 51f4b5f commit 8e5a35d

38 files changed

+171
-115
lines changed

composer.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require-dev": {
2323
"topthink/think-tracing": "^1.0",
2424
"topthink/think-queue": "^3.0",
25-
"phpunit/phpunit": "^9.5"
25+
"phpstan/phpstan": "^2.0",
26+
"pestphp/pest": "^3.7"
2627
},
2728
"autoload": {
2829
"psr-4": {
@@ -34,7 +35,7 @@
3435
},
3536
"autoload-dev": {
3637
"psr-4": {
37-
"think\\tests\\swoole\\": "tests/"
38+
"app\\": "tests/stub/app"
3839
}
3940
},
4041
"extra": {
@@ -54,6 +55,13 @@
5455
"platform": {
5556
"ext-swoole": "5.0.0",
5657
"ext-fileinfo": "1.0.4"
58+
},
59+
"allow-plugins": {
60+
"pestphp/pest-plugin": true
5761
}
62+
},
63+
"scripts": {
64+
"analyze": "phpstan --memory-limit=1G",
65+
"test": "pest --colors=always"
5866
}
5967
}

phpstan.neon

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src
5+
scanFiles:
6+
- vendor/topthink/framework/src/helper.php
7+
scanDirectories:
8+
- vendor/swoole/ide-helper/src/swoole_library/src
9+
treatPhpDocTypesAsCertain: false
10+
ignoreErrors:
11+
-
12+
identifier: while.alwaysTrue
13+
path: src\concerns\InteractsWithQueue.php
14+
-
15+
identifier: if.alwaysFalse
16+
path: src\concerns\InteractsWithWebsocket.php
17+
-
18+
identifier: trait.unused
19+
-
20+
identifier: argument.type
21+
path: src\config\swoole.php

phpunit.xml

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
beStrictAboutTestsThatDoNotTestAnything="false"
5-
bootstrap="tests/bootstrap.php"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
65
colors="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnError="false"
12-
stopOnFailure="false"
13-
verbose="true"
146
>
157
<testsuites>
16-
<testsuite name="ThinkPHP Test Suite">
8+
<testsuite name="Test Suite">
179
<directory suffix="Test.php">./tests</directory>
1810
</testsuite>
1911
</testsuites>
20-
<filter>
21-
<whitelist processUncoveredFilesFromWhitelist="true">
12+
<coverage/>
13+
<source>
14+
<include>
2215
<directory suffix=".php">./src</directory>
23-
</whitelist>
24-
</filter>
16+
</include>
17+
</source>
2518
</phpunit>

src/Http.php

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/**
66
* Class Http
77
* @package think\swoole
8-
* @property $request
98
*/
109
class Http extends \think\Http
1110
{

src/Sandbox.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
use InvalidArgumentException;
77
use ReflectionObject;
88
use RuntimeException;
9-
use think\App;
109
use think\Config;
1110
use think\Container;
1211
use think\Event;
1312
use think\exception\Handle;
14-
use think\swoole\App as SwooleApp;
1513
use think\swoole\concerns\ModifyProperty;
1614
use think\swoole\contract\ResetterInterface;
1715
use think\swoole\coroutine\Context;
@@ -27,14 +25,10 @@ class Sandbox
2725
{
2826
use ModifyProperty;
2927

30-
/**
31-
* The app containers in different coroutine environment.
32-
*
33-
* @var SwooleApp[]
34-
*/
28+
/** @var App[] */
3529
protected $snapshots = [];
3630

37-
/** @var SwooleApp */
31+
/** @var App */
3832
protected $app;
3933

4034
/** @var Config */
@@ -47,13 +41,13 @@ class Sandbox
4741
protected $resetters = [];
4842
protected $services = [];
4943

50-
public function __construct(Container $app)
44+
public function __construct(App $app)
5145
{
5246
$this->setBaseApp($app);
5347
$this->initialize();
5448
}
5549

56-
public function setBaseApp(Container $app)
50+
public function setBaseApp(App $app)
5751
{
5852
$this->app = $app;
5953

@@ -113,7 +107,7 @@ public function clear()
113107
public function getApplication($init = false)
114108
{
115109
$snapshot = $this->getSnapshot($init);
116-
if ($snapshot instanceof Container) {
110+
if ($snapshot instanceof App) {
117111
return $snapshot;
118112
}
119113

@@ -140,14 +134,14 @@ public function getSnapshot($init = false)
140134
return $this->snapshots[$this->getSnapshotId($init)] ?? null;
141135
}
142136

143-
public function setSnapshot(Container $snapshot)
137+
public function setSnapshot(App $snapshot)
144138
{
145139
$this->snapshots[$this->getSnapshotId()] = $snapshot;
146140

147141
return $this;
148142
}
149143

150-
public function setInstance(Container $app)
144+
public function setInstance(App $app)
151145
{
152146
$app->instance('app', $app);
153147
$app->instance(Container::class, $app);

src/Watcher.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace think\swoole;
44

5-
use think\swoole\watcher\Driver;
5+
use think\swoole\contract\WatcherInterface;
66

77
/**
8-
* @mixin Driver
8+
* @mixin WatcherInterface
99
*/
1010
class Watcher extends \think\Manager
1111
{

src/Websocket.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Websocket
3333
/** @var Event */
3434
protected $event;
3535

36-
/** @var Response */
36+
/** @var Response|null */
3737
protected $client;
3838

3939
protected $connected = true;
@@ -138,7 +138,7 @@ public function setClient($response)
138138
/**
139139
* Set sender fd.
140140
*
141-
* @param string
141+
* @param string $fd
142142
*
143143
* @return $this
144144
*/

src/concerns/InteractsWithPools.php

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public function getPools()
2626
protected function preparePools()
2727
{
2828
$createPools = function () {
29-
/** @var Pool $pool */
3029
$pools = $this->getPools();
3130

3231
foreach ($this->getConfig('pool', []) as $name => $config) {

src/concerns/InteractsWithWebsocket.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function onHandShake($req, $res)
101101

102102
$handler = $app->make(HandlerInterface::class);
103103

104-
$this->runWithBarrier(function () use ($app, $request, $handler) {
104+
$this->runWithBarrier(function () use ($request, $handler) {
105105
try {
106106
$handler->onOpen($request);
107107
} catch (Throwable $e) {

src/concerns/WithContainer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function getContainer()
3131
/**
3232
* 获取配置
3333
* @param string $name
34-
* @param null $default
34+
* @param mixed $default
3535
* @return mixed
3636
*/
3737
public function getConfig(string $name, $default = null)

src/contract/WatcherInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace think\swoole\contract;
4+
5+
interface WatcherInterface
6+
{
7+
public function watch(callable $callback);
8+
}

src/contract/websocket/RoomInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ public function prepare(): RoomInterface;
2828
/**
2929
* Add multiple socket fds to a room.
3030
*
31-
* @param string fd
31+
* @param string $fd
3232
* @param array|string $roomNames
3333
*/
3434
public function add($fd, $roomNames);
3535

3636
/**
3737
* Delete multiple socket fds from a room.
3838
*
39-
* @param string fd
39+
* @param string $fd
4040
* @param array|string $roomNames
4141
*/
4242
public function delete($fd, $roomNames);
4343

4444
/**
4545
* Get all sockets by a room key.
4646
*
47-
* @param string room
47+
* @param string $room
4848
*
4949
* @return array
5050
*/
@@ -53,7 +53,7 @@ public function getClients(string $room);
5353
/**
5454
* Get all rooms by a fd.
5555
*
56-
* @param string fd
56+
* @param string $fd
5757
*
5858
* @return array
5959
*/

src/coroutine/Context.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static function getId()
125125

126126
/**
127127
* 获取父级协程ID
128-
* @param null $id
128+
* @param int $id
129129
* @return mixed
130130
*/
131131
public static function getPid($id = 0)

src/pool/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function reset($connection, array $config)
6161
/**
6262
* Validate the connection
6363
*
64-
* @param \Swoole\Coroutine\Client $connection
64+
* @param mixed $connection
6565
* @return bool
6666
*/
6767
public function validate($connection): bool

src/pool/Proxy.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace think\swoole\pool;
44

55
use Closure;
6-
use Exception;
76
use RuntimeException;
87
use Smf\ConnectionPool\ConnectionPool;
98
use Smf\ConnectionPool\Connectors\ConnectorInterface;
@@ -39,9 +38,11 @@ public function __construct($connector, $config, array $connectionConfig = [])
3938
$connector = new Connector($connector);
4039
}
4140

42-
$connector->setChecker(function ($connection) {
43-
return !isset($this->disconnected[$connection]);
44-
});
41+
if ($connector instanceof Connector) {
42+
$connector->setChecker(function ($connection) {
43+
return !isset($this->disconnected[$connection]);
44+
});
45+
}
4546

4647
$this->pool = new ConnectionPool(
4748
Pool::pullPoolConfig($config),
@@ -92,7 +93,7 @@ public function __call($method, $arguments)
9293

9394
try {
9495
return $connection->{$method}(...$arguments);
95-
} catch (Exception|Throwable $e) {
96+
} catch (Throwable $e) {
9697
$this->disconnected[$connection] = true;
9798
throw $e;
9899
}

src/pool/proxy/Connection.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function connect(array $config = [], $linkNum = 0)
4646
*/
4747
public function setDb(DbManager $db)
4848
{
49-
return $this->__call(__FUNCTION__, func_get_args());
49+
$this->__call(__FUNCTION__, func_get_args());
5050
}
5151

5252
/**
@@ -57,7 +57,7 @@ public function setDb(DbManager $db)
5757
*/
5858
public function setCache(CacheInterface $cache)
5959
{
60-
return $this->__call(__FUNCTION__, func_get_args());
60+
$this->__call(__FUNCTION__, func_get_args());
6161
}
6262

6363
/**
@@ -118,7 +118,7 @@ public function insert(BaseQuery $query, bool $getLastInsID = false)
118118
* 批量插入记录
119119
* @access public
120120
* @param BaseQuery $query 查询对象
121-
* @param mixed $dataSet 数据集
121+
* @param array $dataSet 数据集
122122
* @return integer
123123
* @throws \Exception
124124
* @throws \Throwable
@@ -196,7 +196,7 @@ public function transaction(callable $callback)
196196
*/
197197
public function startTrans()
198198
{
199-
return $this->__call(__FUNCTION__, func_get_args());
199+
$this->__call(__FUNCTION__, func_get_args());
200200
}
201201

202202
/**
@@ -206,7 +206,7 @@ public function startTrans()
206206
*/
207207
public function commit()
208208
{
209-
return $this->__call(__FUNCTION__, func_get_args());
209+
$this->__call(__FUNCTION__, func_get_args());
210210
}
211211

212212
/**
@@ -216,7 +216,7 @@ public function commit()
216216
*/
217217
public function rollback()
218218
{
219-
return $this->__call(__FUNCTION__, func_get_args());
219+
$this->__call(__FUNCTION__, func_get_args());
220220
}
221221

222222
/**

src/pool/proxy/Store.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function clear(): bool
6969
*/
7070
public function clearTag($keys)
7171
{
72-
return $this->__call(__FUNCTION__, func_get_args());
72+
$this->__call(__FUNCTION__, func_get_args());
7373
}
7474

7575
/**

src/response/File.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ public function setContentDisposition(string $disposition, string $filename = ''
8888

8989
public function setAutoLastModified()
9090
{
91-
$date = DateTime::createFromFormat('U', $this->file->getMTime());
92-
return $this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
91+
$mTime = $this->file->getMTime();
92+
if ($mTime) {
93+
$date = DateTime::createFromFormat('U', (string) $mTime);
94+
$this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
95+
}
96+
return $this;
9397
}
9498

9599
public function setAutoEtag()

0 commit comments

Comments
 (0)