Skip to content

Commit

Permalink
Introduce explicit global function invocation
Browse files Browse the repository at this point in the history
The commit introduces explicit global function invocation to the codebase. PHP’s performance could be potentially improved by these changes because when a function is called, it will check the namespace first, if the namespace is not defined it will fallback to check the global function namespace. By doing this explicit call we save this check.

Function calls have been edited to explicitly call global functions across various files.

Additionally, this commit introduces the 'native_function_invocation' option in the .php-cs-fixer.php file. This will enforce the use of explicit calls to global functions anywhere in the codebase.

Lastly, refactoring codebase to integrate explicit calls to allow PHP to resolve global functions faster.
  • Loading branch information
AuroraYolo committed Aug 30, 2023
1 parent 89d6915 commit d0716d1
Show file tree
Hide file tree
Showing 33 changed files with 137 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
],
'sort_algorithm' => 'alpha',
],
'native_function_invocation' => [
'include' => ['@all'],
'strict' => false
],
'single_line_comment_style' => [
'comment_types' => [
],
Expand Down
4 changes: 4 additions & 0 deletions app/Command/MkCertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputOption;

use function shell_exec;
use function sprintf;
use function unlink;

/**
* Class MkCertCommand.
*
Expand Down
3 changes: 3 additions & 0 deletions app/Component/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Exception;

use function random_int;
use function strlen;

class Code
{
public static function generateSmsVerifyCode(int $length = 6): string
Expand Down
4 changes: 4 additions & 0 deletions app/Component/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace App\Component;

use function password_hash;
use function password_needs_rehash;
use function password_verify;

class Password
{
public static function hash(string $plaintext): string
Expand Down
2 changes: 2 additions & 0 deletions app/Constants/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use ReflectionClass;
use Throwable;

use function sprintf;

#[Constants]
enum ErrorCode: int implements ErrorCodeInterface
{
Expand Down
2 changes: 2 additions & 0 deletions app/Exception/BusinessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Hyperf\Server\Exception\ServerException;
use Throwable;

use function is_null;

class BusinessException extends ServerException
{
public function __construct(ErrorCodeInterface $code, string $message = null, Throwable $previous = null)
Expand Down
2 changes: 2 additions & 0 deletions app/Exception/Handler/AppExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Psr\Http\Message\ResponseInterface;
use Throwable;

use function sprintf;

class AppExceptionHandler extends ExceptionHandler
{
public function __construct(protected StdoutLoggerInterface $logger)
Expand Down
5 changes: 5 additions & 0 deletions app/Listener/DbQueryExecutedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

use function sprintf;
use function strlen;
use function strpos;
use function substr_replace;

#[Listener]
class DbQueryExecutedListener implements ListenerInterface
{
Expand Down
2 changes: 2 additions & 0 deletions app/Listener/UserLoggedInListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;

use function sprintf;

class UserLoggedInListener implements ListenerInterface
{
#[Inject]
Expand Down
1 change: 1 addition & 0 deletions app/Logic/SmsLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use RedisException;

use function CloudAdmin\Utils\redisClient;
use function sprintf;

class SmsLogic
{
Expand Down
10 changes: 10 additions & 0 deletions app/Middleware/Auth/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use App\Exception\AuthException;
use App\Kernel\Http\Response;
use App\Service\UserService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Phper666\JWTAuth\JWT;
use Phper666\JWTAuth\Util\JWTUtil;
Expand Down Expand Up @@ -48,9 +50,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$token = JWTUtil::handleToken($token);

if ($token !== false && $this->jwt->verifyToken($token)) {
$this->setUserContextWithToken($token);
return $handler->handle($request);
}

return $this->response->handleException(new AuthException(Status::UNAUTHORIZED, 'Token authentication does not pass'));
}

protected function setUserContextWithToken(string $token): void
{
if ($claims = $this->jwt->getClaimsByToken($token)) {
Context::set('user', UserService::get($claims['uid']));
}
}
}
8 changes: 8 additions & 0 deletions app/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public function login(string $username, string $password): User

return $userModel;
}

/**
* 获取用户信息.
*/
public static function get(int $uid): User
{
return User::find($uid);
}
}
14 changes: 7 additions & 7 deletions bin/hyperf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

function initialize(): void
{
ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on');
\ini_set('display_errors', 'on');
\ini_set('display_startup_errors', 'on');

error_reporting(E_ALL);
\error_reporting(E_ALL);

date_default_timezone_set('Asia/Shanghai');
\date_default_timezone_set('Asia/Shanghai');

defined('BASE_PATH') || define('BASE_PATH', dirname(__DIR__, 1));
\defined('BASE_PATH') || \define('BASE_PATH', \dirname(__DIR__, 1));

require BASE_PATH . '/vendor/autoload.php';
}

initialize();
\initialize();

(static function (): void {
Hyperf\Di\ClassLoader::init(handler: new Hyperf\Di\ScanHandler\ProcScanHandler());
Expand All @@ -44,7 +44,7 @@ function initialize(): void
if ($debuggerOptions['handler'] === Debugger::class) {
Debugger::runOnTTY();
} else {
[$serverOptions, $sslOptions] = array_values($debuggerOptions['options']);
[$serverOptions, $sslOptions] = \array_values($debuggerOptions['options']);

$serverConfig = new ServerConfig(
host: $serverOptions['host'],
Expand Down
9 changes: 9 additions & 0 deletions cloud-admin/Casbin/Adapters/Mysql/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Throwable;

use function array_filter;
use function count;
use function func_get_args;
use function implode;
use function is_null;
use function is_string;
use function range;
use function trim;

class DatabaseAdapter implements Adapter, BatchAdapter, UpdatableAdapter, FilteredAdapter
{
use AdapterHelper;
Expand Down
2 changes: 2 additions & 0 deletions cloud-admin/Casbin/EnforcerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function is_null;

class EnforcerFactory
{
/**
Expand Down
4 changes: 4 additions & 0 deletions cloud-admin/Log/AppendRequestIdWithMemoryUsageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

use function memory_get_usage;
use function round;
use function uniqid;

class AppendRequestIdWithMemoryUsageProcessor implements ProcessorInterface
{
public const REQUEST_ID = 'log.request.id';
Expand Down
3 changes: 3 additions & 0 deletions cloud-admin/Log/SwowSocketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Swow\Buffer;
use Swow\Socket;

use function is_array;
use function sprintf;

class SwowSocketHandler extends AbstractProcessingHandler
{
protected Socket $output;
Expand Down
5 changes: 5 additions & 0 deletions cloud-admin/RedLock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
use Swow\Channel;
use Throwable;

use function sleep;
use function sprintf;
use function time;
use function usleep;

class Lock implements LockInterface
{
public Channel $stopDog;
Expand Down
4 changes: 4 additions & 0 deletions cloud-admin/RedLock/RedLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Hyperf\Coroutine\Coroutine;
use SplFixedArray;

use function count;
use function sprintf;
use function time;

class RedLock
{
/**
Expand Down
3 changes: 3 additions & 0 deletions cloud-admin/RedisLock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use Throwable;

use function CloudAdmin\Utils\formatThrowable;
use function max;
use function sprintf;
use function usleep;

class Lock implements RedisLockInterface
{
Expand Down
4 changes: 4 additions & 0 deletions cloud-admin/RedisLock/WatchDog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
use Swow\Coroutine;
use Throwable;

use function microtime;
use function sprintf;
use function usleep;

class WatchDog implements WatchDogInterface
{
/**
Expand Down
9 changes: 9 additions & 0 deletions cloud-admin/SDB/Business/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Helper\TableSeparator;

use function array_merge;
use function array_slice;
use function array_unique;
use function count;
use function implode;
use function is_array;
use function is_null;
use function is_string;

/**
* @see \Hyperf\Devtool\Describe\RoutesCommand
*/
Expand Down
2 changes: 2 additions & 0 deletions cloud-admin/SDB/Debugger/SslConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use RuntimeException;

use function file_exists;

class SslConfig
{
final public function __construct(
Expand Down
15 changes: 15 additions & 0 deletions cloud-admin/SDB/WebSocketDebugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,24 @@
use Throwable;
use WeakMap;

use function array_filter;
use function array_shift;
use function bin2hex;
use function CloudAdmin\Utils\di;
use function CloudAdmin\Utils\logger;
use function count;
use function ctype_print;
use function explode;
use function implode;
use function in_array;
use function is_numeric;
use function is_string;
use function sleep;
use function sprintf;
use function strtolower;
use function Swow\Debug\var_dump_return;
use function trim;
use function usleep;

/**
* Class WebSocketDebugger.
Expand Down
3 changes: 3 additions & 0 deletions cloud-admin/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
use Swow\SocketException;
use Throwable;

use function in_array;
use function sleep;

class Server extends Psr7Server implements ServerInterface
{
public ?string $host = null;
Expand Down
5 changes: 5 additions & 0 deletions cloud-admin/Server/SslConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
use Hyperf\Contract\Arrayable;
use InvalidArgumentException;

use function in_array;
use function sprintf;
use function strtolower;
use function substr;

/**
* @method SslConfig setCertificate(string $cert)
* @method SslConfig setCertificateKey(string $key)
Expand Down
2 changes: 2 additions & 0 deletions cloud-admin/Utils/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CloudAdmin\Utils;

use function posix_getpid;

class Os
{
public static function getProcessId(): int
Expand Down
2 changes: 2 additions & 0 deletions cloud-admin/Utils/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Redis;
use Throwable;

use function function_exists;

if (! function_exists('logger')) {
/**
* @throws ContainerExceptionInterface
Expand Down
1 change: 1 addition & 0 deletions test/Cases/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Throwable;

use function CloudAdmin\Utils\di;
use function uniqid;

/**
* @internal
Expand Down
2 changes: 2 additions & 0 deletions test/CloudAdmin/RedisLock/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Throwable;

use function CloudAdmin\Utils\di;
use function sleep;
use function sprintf;

/**
* @internal
Expand Down
2 changes: 2 additions & 0 deletions test/HttpTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Mockery;
use PHPUnit\Framework\TestCase;

use function make;

/**
* Class HttpTestCase.
* @method get($uri, $data = [], $headers = [])
Expand Down
2 changes: 2 additions & 0 deletions test/Sys/SmsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use App\Service\SmsService;
use CloudAdmin\Test\HttpTestCase;

use function strlen;

/**
* @internal
* @coversNothing
Expand Down
Loading

0 comments on commit d0716d1

Please sign in to comment.