Skip to content

Commit

Permalink
代码质量优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jan 2, 2025
1 parent 51f4b5f commit 8e5a35d
Show file tree
Hide file tree
Showing 38 changed files with 171 additions and 115 deletions.
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require-dev": {
"topthink/think-tracing": "^1.0",
"topthink/think-queue": "^3.0",
"phpunit/phpunit": "^9.5"
"phpstan/phpstan": "^2.0",
"pestphp/pest": "^3.7"
},
"autoload": {
"psr-4": {
Expand All @@ -34,7 +35,7 @@
},
"autoload-dev": {
"psr-4": {
"think\\tests\\swoole\\": "tests/"
"app\\": "tests/stub/app"
}
},
"extra": {
Expand All @@ -54,6 +55,13 @@
"platform": {
"ext-swoole": "5.0.0",
"ext-fileinfo": "1.0.4"
},
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"scripts": {
"analyze": "phpstan --memory-limit=1G",
"test": "pest --colors=always"
}
}
21 changes: 21 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
parameters:
level: 5
paths:
- src
scanFiles:
- vendor/topthink/framework/src/helper.php
scanDirectories:
- vendor/swoole/ide-helper/src/swoole_library/src
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
identifier: while.alwaysTrue
path: src\concerns\InteractsWithQueue.php
-
identifier: if.alwaysFalse
path: src\concerns\InteractsWithWebsocket.php
-
identifier: trait.unused
-
identifier: argument.type
path: src\config\swoole.php
25 changes: 9 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="ThinkPHP Test Suite">
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
1 change: 0 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* Class Http
* @package think\swoole
* @property $request
*/
class Http extends \think\Http
{
Expand Down
20 changes: 7 additions & 13 deletions src/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use InvalidArgumentException;
use ReflectionObject;
use RuntimeException;
use think\App;
use think\Config;
use think\Container;
use think\Event;
use think\exception\Handle;
use think\swoole\App as SwooleApp;
use think\swoole\concerns\ModifyProperty;
use think\swoole\contract\ResetterInterface;
use think\swoole\coroutine\Context;
Expand All @@ -27,14 +25,10 @@ class Sandbox
{
use ModifyProperty;

/**
* The app containers in different coroutine environment.
*
* @var SwooleApp[]
*/
/** @var App[] */
protected $snapshots = [];

/** @var SwooleApp */
/** @var App */
protected $app;

/** @var Config */
Expand All @@ -47,13 +41,13 @@ class Sandbox
protected $resetters = [];
protected $services = [];

public function __construct(Container $app)
public function __construct(App $app)
{
$this->setBaseApp($app);
$this->initialize();
}

public function setBaseApp(Container $app)
public function setBaseApp(App $app)
{
$this->app = $app;

Expand Down Expand Up @@ -113,7 +107,7 @@ public function clear()
public function getApplication($init = false)
{
$snapshot = $this->getSnapshot($init);
if ($snapshot instanceof Container) {
if ($snapshot instanceof App) {
return $snapshot;
}

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

public function setSnapshot(Container $snapshot)
public function setSnapshot(App $snapshot)
{
$this->snapshots[$this->getSnapshotId()] = $snapshot;

return $this;
}

public function setInstance(Container $app)
public function setInstance(App $app)
{
$app->instance('app', $app);
$app->instance(Container::class, $app);
Expand Down
4 changes: 2 additions & 2 deletions src/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace think\swoole;

use think\swoole\watcher\Driver;
use think\swoole\contract\WatcherInterface;

/**
* @mixin Driver
* @mixin WatcherInterface
*/
class Watcher extends \think\Manager
{
Expand Down
4 changes: 2 additions & 2 deletions src/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Websocket
/** @var Event */
protected $event;

/** @var Response */
/** @var Response|null */
protected $client;

protected $connected = true;
Expand Down Expand Up @@ -138,7 +138,7 @@ public function setClient($response)
/**
* Set sender fd.
*
* @param string
* @param string $fd
*
* @return $this
*/
Expand Down
1 change: 0 additions & 1 deletion src/concerns/InteractsWithPools.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function getPools()
protected function preparePools()
{
$createPools = function () {
/** @var Pool $pool */
$pools = $this->getPools();

foreach ($this->getConfig('pool', []) as $name => $config) {
Expand Down
2 changes: 1 addition & 1 deletion src/concerns/InteractsWithWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function onHandShake($req, $res)

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

$this->runWithBarrier(function () use ($app, $request, $handler) {
$this->runWithBarrier(function () use ($request, $handler) {
try {
$handler->onOpen($request);
} catch (Throwable $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/concerns/WithContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function getContainer()
/**
* 获取配置
* @param string $name
* @param null $default
* @param mixed $default
* @return mixed
*/
public function getConfig(string $name, $default = null)
Expand Down
8 changes: 8 additions & 0 deletions src/contract/WatcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace think\swoole\contract;

interface WatcherInterface
{
public function watch(callable $callback);
}
8 changes: 4 additions & 4 deletions src/contract/websocket/RoomInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ public function prepare(): RoomInterface;
/**
* Add multiple socket fds to a room.
*
* @param string fd
* @param string $fd
* @param array|string $roomNames
*/
public function add($fd, $roomNames);

/**
* Delete multiple socket fds from a room.
*
* @param string fd
* @param string $fd
* @param array|string $roomNames
*/
public function delete($fd, $roomNames);

/**
* Get all sockets by a room key.
*
* @param string room
* @param string $room
*
* @return array
*/
Expand All @@ -53,7 +53,7 @@ public function getClients(string $room);
/**
* Get all rooms by a fd.
*
* @param string fd
* @param string $fd
*
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static function getId()

/**
* 获取父级协程ID
* @param null $id
* @param int $id
* @return mixed
*/
public static function getPid($id = 0)
Expand Down
2 changes: 1 addition & 1 deletion src/pool/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function reset($connection, array $config)
/**
* Validate the connection
*
* @param \Swoole\Coroutine\Client $connection
* @param mixed $connection
* @return bool
*/
public function validate($connection): bool
Expand Down
11 changes: 6 additions & 5 deletions src/pool/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace think\swoole\pool;

use Closure;
use Exception;
use RuntimeException;
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\ConnectorInterface;
Expand Down Expand Up @@ -39,9 +38,11 @@ public function __construct($connector, $config, array $connectionConfig = [])
$connector = new Connector($connector);
}

$connector->setChecker(function ($connection) {
return !isset($this->disconnected[$connection]);
});
if ($connector instanceof Connector) {
$connector->setChecker(function ($connection) {
return !isset($this->disconnected[$connection]);
});
}

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

try {
return $connection->{$method}(...$arguments);
} catch (Exception|Throwable $e) {
} catch (Throwable $e) {
$this->disconnected[$connection] = true;
throw $e;
}
Expand Down
12 changes: 6 additions & 6 deletions src/pool/proxy/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function connect(array $config = [], $linkNum = 0)
*/
public function setDb(DbManager $db)
{
return $this->__call(__FUNCTION__, func_get_args());
$this->__call(__FUNCTION__, func_get_args());
}

/**
Expand All @@ -57,7 +57,7 @@ public function setDb(DbManager $db)
*/
public function setCache(CacheInterface $cache)
{
return $this->__call(__FUNCTION__, func_get_args());
$this->__call(__FUNCTION__, func_get_args());
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ public function insert(BaseQuery $query, bool $getLastInsID = false)
* 批量插入记录
* @access public
* @param BaseQuery $query 查询对象
* @param mixed $dataSet 数据集
* @param array $dataSet 数据集
* @return integer
* @throws \Exception
* @throws \Throwable
Expand Down Expand Up @@ -196,7 +196,7 @@ public function transaction(callable $callback)
*/
public function startTrans()
{
return $this->__call(__FUNCTION__, func_get_args());
$this->__call(__FUNCTION__, func_get_args());
}

/**
Expand All @@ -206,7 +206,7 @@ public function startTrans()
*/
public function commit()
{
return $this->__call(__FUNCTION__, func_get_args());
$this->__call(__FUNCTION__, func_get_args());
}

/**
Expand All @@ -216,7 +216,7 @@ public function commit()
*/
public function rollback()
{
return $this->__call(__FUNCTION__, func_get_args());
$this->__call(__FUNCTION__, func_get_args());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pool/proxy/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function clear(): bool
*/
public function clearTag($keys)
{
return $this->__call(__FUNCTION__, func_get_args());
$this->__call(__FUNCTION__, func_get_args());
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/response/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ public function setContentDisposition(string $disposition, string $filename = ''

public function setAutoLastModified()
{
$date = DateTime::createFromFormat('U', $this->file->getMTime());
return $this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
$mTime = $this->file->getMTime();
if ($mTime) {
$date = DateTime::createFromFormat('U', (string) $mTime);
$this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
}
return $this;
}

public function setAutoEtag()
Expand Down
Loading

0 comments on commit 8e5a35d

Please sign in to comment.