Skip to content

Commit b17410c

Browse files
authored
Refactor: Remove/Replace unnecessary implementations in PHP (Laravel) (#144)
* Refactor: Remove/Replace unnecessary implementations * Revert function name and add static prefix to a callback in the Utils.php * Replace to modern array
1 parent caccc13 commit b17410c

3 files changed

Lines changed: 58 additions & 116 deletions

File tree

php/sqlcommenter-php/packages/sqlcommenter-laravel/src/Database/Connection.php

Lines changed: 28 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -26,119 +26,51 @@
2626
class Connection extends BaseConnection
2727
{
2828
/**
29-
* Run a select statement and return a single result.
30-
*
31-
* @param string $query
32-
* @param array $bindings
33-
* @param bool $useReadPdo
34-
* @return mixed
29+
* @inheritDoc
3530
*/
36-
public function selectOne($query, $bindings = [], $useReadPdo = true)
31+
protected function run($query, $bindings, \Closure $callback)
3732
{
38-
$query = $this->getSqlComments($query);
39-
$records = parent::select($query, $bindings, $useReadPdo);
40-
41-
if (count($records) > 0) {
42-
return $records;
43-
}
44-
return null;
45-
}
46-
47-
/**
48-
* Run a select statement against the database.
49-
*
50-
* @param string $query
51-
* @param array $bindings
52-
* @param bool $useReadPdo
53-
* @return array
54-
*/
55-
public function select($query, $bindings = [], $useReadPdo = true)
56-
{
57-
$query = $this->getSqlComments($query);
58-
$records = parent::select($query, $bindings, $useReadPdo);
59-
return $records;
33+
return parent::run(
34+
$this->appendSqlComments($query),
35+
$bindings,
36+
$callback
37+
);
6038
}
6139

62-
/**
63-
* Run an insert statement against the database.
64-
*
65-
* @param string $query
66-
* @param array $bindings
67-
* @return bool
68-
*/
69-
public function insert($query, $bindings = [])
40+
private function appendSqlComments(string $query): string
7041
{
71-
$query = $this->getSqlComments($query);
72-
$records = parent::insert($query, $bindings);
73-
74-
return $records;
75-
}
76-
77-
/**
78-
* Run an update statement against the database.
79-
*
80-
* @param string $query
81-
* @param array $bindings
82-
* @return int
83-
*/
84-
public function update($query, $bindings = [])
85-
{
86-
$query = $this->getSqlComments($query);
87-
88-
return $this->affectingStatement($query, $bindings);
89-
}
90-
91-
/**
92-
* Run a delete statement against the database.
93-
*
94-
* @param string $query
95-
* @param array $bindings
96-
* @return int
97-
*/
98-
public function delete($query, $bindings = [])
99-
{
100-
101-
$query = $this->getSqlComments($query);
102-
103-
return $this->affectingStatement($query, $bindings);
104-
}
105-
106-
private function getSqlComments($query)
107-
{
108-
$configurationKey = 'google_sqlcommenter.include.';
109-
$comment = [];
42+
static $configurationKey = 'google_sqlcommenter.include';
43+
$comments = [];
11044
$action = null;
11145

112-
if (!empty(app('request')->route())) {
113-
$action = app('request')->route()->getAction();
46+
if (!empty(request()->route())) {
47+
$action = request()->route()->getAction();
11448
}
115-
if (config($configurationKey . 'framework', true)) {
116-
$comment['framework'] = "laravel-" . app()->version();
49+
if (config("{$configurationKey}.framework", true)) {
50+
$comments['framework'] = "laravel-" . app()->version();
11751
}
118-
if (config($configurationKey . 'controller', true) and !empty($action['controller'])) {
119-
$comment['controller'] = explode("@", class_basename($action['controller']))[0];
52+
if (config("{$configurationKey}.controller", true) && !empty($action['controller'])) {
53+
$comments['controller'] = explode("@", class_basename($action['controller']))[0];
12054
}
121-
if (config($configurationKey . 'action', true) and !empty($action and $action['controller'] and str_contains($action['controller'], '@'))) {
122-
$comment['action'] = explode("@", class_basename($action['controller']))[1];
55+
if (config("{$configurationKey}.action", true) && !empty($action and $action['controller'] && str_contains($action['controller'], '@'))) {
56+
$comments['action'] = explode("@", class_basename($action['controller']))[1];
12357
}
124-
if (config($configurationKey . 'route', true)) {
125-
$comment['route'] = request()->getRequestUri();
58+
if (config("{$configurationKey}.route", true)) {
59+
$comments['route'] = request()->getRequestUri();
12660
}
127-
if (config($configurationKey . 'db_driver', true)) {
61+
if (config("{$configurationKey}.db_driver", true)) {
12862
$connection = config('database.default');
129-
$comment['db_driver'] = config("database.connections.{$connection}.driver");
63+
$comments['db_driver'] = config("database.connections.{$connection}.driver");
13064
}
131-
if (config($configurationKey . 'opentelemetry', true)) {
65+
if (config("{$configurationKey}.opentelemetry", true)) {
13266
$carrier = Opentelemetry::getOpentelemetryValues();
133-
$comment = array_merge($comment, $carrier);
67+
$comments = $comments + $carrier;
13468
}
13569

136-
$query=trim($query);
137-
138-
if ($query[-1] == ';'){
139-
return rtrim($query ,";"). Utils::formatComments(array_filter(($comment))). ';';
140-
}
141-
return $query . Utils::formatComments(array_filter(($comment)));
70+
$query = trim($query);
71+
$hasSemicolon = $query[-1] === ';';
72+
$query = rtrim($query, ';');
14273

74+
return $query . Utils::formatComments(array_filter($comments)) . ($hasSemicolon ? ';' : '');
14375
}
14476
}

php/sqlcommenter-php/packages/sqlcommenter-laravel/src/Utils.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,29 @@
1919

2020
class Utils
2121
{
22-
public static function formatComments($comment)
22+
public static function formatComments(array $comments): string
2323
{
24-
if (empty($comment)) {
24+
if (empty($comments)) {
2525
return "";
2626
}
27-
$lastElement = array_key_last($comment);
28-
$sql_comment = "/*";
29-
foreach ($comment as $key => $value) {
30-
if ($key == $lastElement) {
31-
$sql_comment .= Utils::customUrlEncode($key) . "=" . "'" . Utils::customUrlEncode($value) . "'*/";
32-
} else {
33-
$sql_comment .= Utils::customUrlEncode($key) . "=" . "'" . Utils::customUrlEncode($value) . "',";
34-
}
35-
}
36-
return $sql_comment;
27+
28+
return "/*" . implode(
29+
',',
30+
array_map(
31+
static fn (string $value, string $key) => Utils::customUrlEncode($key) . "='" . Utils::customUrlEncode($value) . "'", $comments,
32+
array_keys($comments)
33+
),
34+
) . "*/";
3735
}
3836

39-
private static function customUrlEncode($input)
37+
private static function customUrlEncode(string $input): string
4038
{
4139
$encodedString = urlencode($input);
4240

43-
# Since SQL uses '%' as a keyword, '%' is a by-product of url quoting
44-
# e.g. foo,bar --> foo%2Cbar
45-
# thus in our quoting, we need to escape it too to finally give
46-
# foo,bar --> foo%%2Cbar
41+
// Since SQL uses '%' as a keyword, '%' is a by-product of url quoting
42+
// e.g. foo,bar --> foo%2Cbar
43+
// thus in our quoting, we need to escape it too to finally give
44+
// foo,bar --> foo%%2Cbar
4745

4846
return str_replace("%", "%%", $encodedString);
4947
}

php/sqlcommenter-php/packages/sqlcommenter-laravel/tests/Unit/UtilsTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,26 @@ final class UtilsTest extends TestCase
2222
{
2323
public function testFormatCommentsWithKeys(): void
2424
{
25-
$this->assertEquals("/*key1='value1',key2='value2'*/", Utils::formatComments(array("key1" => "value1", "key2" => "value2")));
25+
$this->assertEquals("/*key1='value1',key2='value2'*/", Utils::formatComments(["key1" => "value1", "key2" => "value2"]));
2626
}
27+
2728
public function testFormatCommentsWithoutKeys(): void
2829
{
29-
$this->assertEquals("", Utils::formatComments(array()));
30+
$this->assertEquals("", Utils::formatComments([]));
3031
}
32+
3133
public function testFormatCommentsWithSpecialCharKeys(): void
3234
{
33-
$this->assertEquals("/*key1='value1%%40',key2='value2'*/", Utils::formatComments(array("key1" => "value1@", "key2" => "value2")));
35+
$this->assertEquals("/*key1='value1%%40',key2='value2'*/", Utils::formatComments(["key1" => "value1@", "key2" => "value2"]));
36+
}
37+
38+
public function testFormatCommentsWithPlaceholder(): void
39+
{
40+
$this->assertEquals("/*key1='value1%%3F',key2='value2'*/", Utils::formatComments(["key1" => "value1?", "key2" => "value2"]));
41+
}
42+
43+
public function testFormatCommentsWithNamedPlaceholder(): void
44+
{
45+
$this->assertEquals("/*key1='%%3Anamed',key2='value2'*/", Utils::formatComments(["key1" => ":named", "key2" => "value2"]));
3446
}
3547
}

0 commit comments

Comments
 (0)