Skip to content

Commit ec8dc54

Browse files
committed
refactor: makes improvements to the code
1 parent a16aa9c commit ec8dc54

6 files changed

Lines changed: 40 additions & 34 deletions

File tree

src/Aspect/SqlCommenterAspect.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515

1616
namespace ReinanHS\SqlCommenterHyperf\Aspect;
1717

18-
use Hyperf\Context\Context;
18+
use Hyperf\Context\RequestContext;
1919
use Hyperf\Contract\ConfigInterface;
2020
use Hyperf\Database\Connection;
2121
use Hyperf\Di\Aop\AbstractAspect;
2222
use Hyperf\Di\Aop\ProceedingJoinPoint;
2323
use Hyperf\HttpServer\Router\Dispatched;
24-
use Psr\Http\Message\ServerRequestInterface;
2524
use ReinanHS\SqlCommenterHyperf\Opentelemetry;
2625
use ReinanHS\SqlCommenterHyperf\SwitchManager;
2726
use ReinanHS\SqlCommenterHyperf\Utils;
@@ -38,10 +37,11 @@ public function __construct(private readonly ConfigInterface $config, private re
3837

3938
public function process(ProceedingJoinPoint $proceedingJoinPoint)
4039
{
41-
if (! $this->config->get('sqlcommenter.enable', true)) {
40+
if (! $this->config->get('sqlcommenter.enable', true) || ! isset($proceedingJoinPoint->arguments['keys']['query'])) {
4241
return $proceedingJoinPoint->process();
4342
}
4443

44+
/** @var string $query */
4545
$query = $proceedingJoinPoint->arguments['keys']['query'];
4646

4747
/** @var Connection $dbInstance */
@@ -61,33 +61,31 @@ private function appendSqlComments(string $query, string $dbDriver): string
6161
}
6262

6363
if ($this->switchManager->isEnable('application')) {
64-
$comments['application'] = $this->config->get('app_name');
64+
$comments['application'] = (string) $this->config->get('app_name');
6565
}
6666

6767
if ($this->switchManager->isEnable('db_driver')) {
6868
$comments['db_driver'] = $dbDriver;
6969
}
7070

71-
$request = Context::get(ServerRequestInterface::class);
72-
if ($request) {
73-
if ($this->switchManager->isEnable('route')) {
74-
$comments['route'] = $request->getUri()->getPath();
75-
}
71+
$request = RequestContext::get();
72+
if ($this->switchManager->isEnable('route')) {
73+
$comments['route'] = $request->getUri()->getPath();
74+
}
7675

77-
if ($this->switchManager->isEnable('controller') || $this->switchManager->isEnable('action')) {
78-
/** @var null|Dispatched $dispatched */
79-
$dispatched = $request->getAttribute(Dispatched::class);
76+
if ($this->switchManager->isEnable('controller') || $this->switchManager->isEnable('action')) {
77+
/** @var null|Dispatched $dispatched */
78+
$dispatched = $request->getAttribute(Dispatched::class);
8079

81-
if ($dispatched && $dispatched->isFound()) {
82-
$parts = Utils::extractCallback($dispatched->handler?->callback);
80+
if ($dispatched && $dispatched->isFound()) {
81+
$parts = Utils::extractCallback($dispatched->handler?->callback);
8382

84-
if ($this->switchManager->isEnable('controller')) {
85-
$comments['controller'] = $parts[0];
86-
}
83+
if ($this->switchManager->isEnable('controller')) {
84+
$comments['controller'] = (string) $parts[0];
85+
}
8786

88-
if ($this->switchManager->isEnable('action')) {
89-
$comments['action'] = $parts[1];
90-
}
87+
if ($this->switchManager->isEnable('action')) {
88+
$comments['action'] = (string) $parts[1];
9189
}
9290
}
9391
}

src/Factory/SwitchManagerFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace ReinanHS\SqlCommenterHyperf\Factory;
1717

18+
use Hyperf\Config\Config;
1819
use Hyperf\Contract\ConfigInterface;
1920
use Psr\Container\ContainerInterface;
2021
use ReinanHS\SqlCommenterHyperf\SwitchManager;
@@ -23,10 +24,15 @@ class SwitchManagerFactory
2324
{
2425
public function __invoke(ContainerInterface $container): SwitchManager
2526
{
27+
/** @var Config $config */
2628
$config = $container->get(ConfigInterface::class);
29+
2730
$manager = new SwitchManager();
28-
$manager->apply($config->get('sqlcommenter.include', []));
2931

32+
/** @var array $sqlcommenterConfig */
33+
$sqlcommenterConfig = $config->get('sqlcommenter.include', []);
34+
35+
$manager->apply($sqlcommenterConfig);
3036
return $manager;
3137
}
3238
}

src/Opentelemetry.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ class Opentelemetry
2929
*/
3030
public static function getOpentelemetryValues(): array
3131
{
32-
$appendContext = [];
33-
3432
$root = TracerContext::getRoot();
3533
if ($root instanceof Span) {
34+
$appendContext = [];
35+
3636
TracerContext::getTracer()->inject(
3737
spanContext: $root->getContext(),
3838
format: TEXT_MAP,
3939
carrier: $appendContext
4040
);
4141

42-
if ($appendContext) {
42+
if ($appendContext && is_array($appendContext)) {
4343
$traceparent = self::convertB3ToW3C($appendContext);
4444

4545
return ['traceparent' => $traceparent];
4646
}
4747
}
4848

49-
return $appendContext;
49+
return [];
5050
}
5151

5252
/**
@@ -57,8 +57,8 @@ public static function getOpentelemetryValues(): array
5757
*/
5858
private static function convertB3ToW3C(array $b3Context): string
5959
{
60-
$traceId = str_pad($b3Context['x-b3-traceid'], 32, '0', STR_PAD_LEFT);
61-
$spanId = str_pad($b3Context['x-b3-spanid'], 16, '0', STR_PAD_LEFT);
60+
$traceId = str_pad((string) $b3Context['x-b3-traceid'], 32, '0', STR_PAD_LEFT);
61+
$spanId = str_pad((string) $b3Context['x-b3-spanid'], 16, '0', STR_PAD_LEFT);
6262
$sampled = $b3Context['x-b3-sampled'] === '1' ? '01' : '00';
6363

6464
// W3C Traceparent format

src/SwitchManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public function isEnable(string $identifier): bool
4444
return false;
4545
}
4646

47-
return $this->config[$identifier];
47+
return (bool) $this->config[$identifier];
4848
}
4949
}

src/Utils.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public static function formatComments(array $comments): string
2929
return '/*' . implode(
3030
',',
3131
array_map(
32-
static fn (string $value, string $key) => Utils::customUrlEncode($key) . "='" . Utils::customUrlEncode($value) . "'",
32+
function (string $value, string $key): string {
33+
return Utils::customUrlEncode($key) . "='" . Utils::customUrlEncode($value) . "'";
34+
},
3335
$comments,
3436
array_keys($comments)
3537
),
@@ -54,9 +56,10 @@ public static function extractCallback(mixed $callback): array
5456

5557
return [$controllerName, $method];
5658
case 'array':
59+
/** @var string $method */
5760
$method = $callback[1] ?? '__invoke';
5861

59-
$controllerNameParts = explode('\\', $callback[0]);
62+
$controllerNameParts = explode('\\', (string) $callback[0]);
6063
$controllerName = end($controllerNameParts);
6164
$controllerName = str_replace('.php', '', $controllerName);
6265

tests/Aspect/SqlCommenterAspectTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace ReinanHS\Test\Aspect;
1717

1818
use FastRoute\Dispatcher;
19-
use Hyperf\Context\Context;
19+
use Hyperf\Context\RequestContext;
2020
use Hyperf\Contract\ConfigInterface;
2121
use Hyperf\Database\Connection;
2222
use Hyperf\Di\Aop\ProceedingJoinPoint;
@@ -25,7 +25,6 @@
2525
use Hyperf\HttpServer\Router\Handler;
2626
use Mockery;
2727
use PHPUnit\Framework\TestCase;
28-
use Psr\Http\Message\ServerRequestInterface;
2928
use Psr\Http\Message\UriInterface;
3029
use ReinanHS\SqlCommenterHyperf\Aspect\SqlCommenterAspect;
3130
use ReinanHS\SqlCommenterHyperf\Opentelemetry;
@@ -77,9 +76,9 @@ public function testProcess(): void
7776
->method('getUri')
7877
->willReturn($mockedUri);
7978

80-
$mockedContext = Mockery::mock('alias:' . Context::class);
79+
$mockedContext = Mockery::mock('alias:' . RequestContext::class);
8180
$mockedContext->shouldReceive('get')
82-
->with(ServerRequestInterface::class)
81+
->with()
8382
->andReturn($mockedRequest);
8483

8584
$mockedOpentelemetry = Mockery::mock('alias:' . Opentelemetry::class);

0 commit comments

Comments
 (0)