Skip to content

Commit 704e344

Browse files
committed
Add CS fixer; update code styles and naming
1 parent a0ef8c9 commit 704e344

23 files changed

+332
-205
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
.php_cs.cache
2+
build
13
composer.lock
4+
phpunit.xml
25
vendor
36
mysql-config.php
47
*.pid

.php_cs.dist

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRiskyAllowed(true)
5+
->setRules([
6+
"@PSR1" => true,
7+
"@PSR2" => true,
8+
"braces" => [
9+
"allow_single_line_closure" => true,
10+
"position_after_functions_and_oop_constructs" => "same",
11+
],
12+
"array_syntax" => ["syntax" => "short"],
13+
"cast_spaces" => true,
14+
"combine_consecutive_unsets" => true,
15+
"function_to_constant" => true,
16+
"no_multiline_whitespace_before_semicolons" => true,
17+
"no_unused_imports" => true,
18+
"no_useless_else" => true,
19+
"no_useless_return" => true,
20+
"no_whitespace_before_comma_in_array" => true,
21+
"no_whitespace_in_blank_line" => true,
22+
"non_printable_character" => true,
23+
"normalize_index_brace" => true,
24+
"ordered_imports" => true,
25+
"php_unit_construct" => true,
26+
"php_unit_dedicate_assert" => true,
27+
"php_unit_fqcn_annotation" => true,
28+
"phpdoc_summary" => true,
29+
"phpdoc_types" => true,
30+
"psr4" => true,
31+
"return_type_declaration" => ["space_before" => "none"],
32+
"short_scalar_cast" => true,
33+
"single_blank_line_before_namespace" => true,
34+
])
35+
->setFinder(
36+
PhpCsFixer\Finder::create()
37+
->in(__DIR__ . "/examples")
38+
->in(__DIR__ . "/lib")
39+
->in(__DIR__ . "/test")
40+
);

Makefile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
PHP_BIN := php
2+
COMPOSER_BIN := composer
3+
4+
COVERAGE = coverage
5+
SRCS = lib test
6+
7+
find_php_files = $(shell find $(1) -type f -name "*.php")
8+
src = $(foreach d,$(SRCS),$(call find_php_files,$(d)))
9+
10+
.PHONY: test
11+
test: setup phpunit code-style
12+
13+
.PHONY: clean
14+
clean: clean-coverage clean-vendor
15+
16+
.PHONY: clean-coverage
17+
clean-coverage:
18+
test ! -e coverage || rm -r coverage
19+
20+
.PHONY: clean-vendor
21+
clean-vendor:
22+
test ! -e vendor || rm -r vendor
23+
24+
.PHONY: setup
25+
setup: vendor/autoload.php
26+
27+
.PHONY: deps-update
28+
deps-update:
29+
$(COMPOSER_BIN) update
30+
31+
.PHONY: phpunit
32+
phpunit: setup
33+
$(PHP_BIN) vendor/bin/phpunit
34+
35+
.PHONY: code-style
36+
code-style: setup
37+
PHP_CS_FIXER_IGNORE_ENV=1 $(PHP_BIN) vendor/bin/php-cs-fixer --diff -v fix
38+
39+
composer.lock: composer.json
40+
$(COMPOSER_BIN) install
41+
touch $@
42+
43+
vendor/autoload.php: composer.lock
44+
$(COMPOSER_BIN) install
45+
touch $@

composer.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "amphp/mysql",
33
"description": "Asynchronous parallel Mysql client built on the Amp concurrency framework",
4-
"minimum-stability": "dev",
54
"require": {
65
"amphp/amp": "^2",
76
"amphp/socket": "^0.10"
@@ -11,11 +10,16 @@
1110
{
1211
"name": "Bob Weinand",
1312
"email": "[email protected]"
13+
},
14+
{
15+
"name": "Aaron Piotrowski",
16+
"email": "[email protected]"
1417
}
1518
],
1619
"require-dev": {
1720
"phpunit/phpunit": "^6",
18-
"amphp/phpunit-util": "^1"
21+
"amphp/phpunit-util": "^1",
22+
"friendsofphp/php-cs-fixer": "^2.3"
1923
},
2024
"autoload": {
2125
"psr-4": {

examples/1-connect.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
require 'support/bootstrap.php';
88

9-
Amp\Loop::run(function() {
9+
Amp\Loop::run(function () {
1010
/* If you want ssl, pass as second argument an array with ssl options (an empty options array is valid too); if null is passed, ssl is not enabled either */
1111
$db = yield \Amp\Mysql\connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
1212

examples/2-simple-query.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Amp\Mysql\ResultSet;
66

7-
Amp\Loop::run(function() {
7+
Amp\Loop::run(function () {
88
$db = Amp\Mysql\pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
99

1010
/** @var \Amp\Mysql\ResultSet $result */

examples/3-generic-with-yield.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'support/bootstrap.php';
44

5-
Amp\Loop::run(function() {
5+
Amp\Loop::run(function () {
66
$db = Amp\Mysql\pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
77

88
/* Create table and insert a few rows */

examples/4-multi-rows.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'support/bootstrap.php';
44
require 'support/generic-table.php';
55

6-
Amp\Loop::run(function() {
6+
Amp\Loop::run(function () {
77
$db = Amp\Mysql\pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
88

99
/* create same table than in 3-generic-with-yield.php */

examples/5-multi-stmts.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'support/bootstrap.php';
44
require 'support/generic-table.php';
55

6-
\Amp\Loop::run(function() {
6+
\Amp\Loop::run(function () {
77
$db = \Amp\Mysql\pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
88

99
/* create same table than in 3-generic-with-yield.php */

examples/support/bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
if ($answer === "no") {
2222
print "Can't run any examples without valid database credentials." . PHP_EOL;
2323
exit(1);
24-
} else if ($answer === "yes") {
24+
} elseif ($answer === "yes") {
2525
print "Database host: ";
2626
$host = var_export(trim(fgets(STDIN)), true);
2727

lib/CommandResult.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ public function affectedRows(): int {
2727
public function insertId(): int {
2828
return $this->insertId;
2929
}
30-
}
30+
}

lib/Connection.php

+19-45
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Amp\Deferred;
66
use Amp\Promise;
7-
use Amp\Socket\ClientTlsContext;
87

98
class Connection implements Link {
109
const REFRESH_GRANT = 0x01;
@@ -32,31 +31,6 @@ public function __construct(Internal\Processor $processor) {
3231
$this->processor = $processor;
3332
}
3433

35-
public static function parseConnectionString(string $connStr, ClientTlsContext $sslOptions = null): ConnectionConfig {
36-
$db = null;
37-
$useCompression = "false";
38-
39-
foreach (explode(";", $connStr) as $param) {
40-
list($key, $$key) = array_map("trim", explode("=", $param, 2) + [1 => null]);
41-
}
42-
if (!isset($host, $user, $pass)) {
43-
throw new \Error("Required parameters host, user and pass need to be passed in connection string");
44-
}
45-
46-
$config = new ConnectionConfig;
47-
$config->host = $host;
48-
$config->user = $user;
49-
$config->pass = $pass;
50-
$config->db = $db;
51-
$config->useCompression = $useCompression && $useCompression != "false";
52-
53-
$config->ssl = $sslOptions;
54-
55-
$config->resolveHost();
56-
57-
return $config;
58-
}
59-
6034
public function isAlive(): bool {
6135
return $this->processor->isAlive();
6236
}
@@ -93,18 +67,18 @@ public function setCharset(string $charset, string $collate = ""): Promise {
9367
/** @see 14.6.2 COM_QUIT */
9468
public function close() {
9569
$processor = $this->processor;
96-
$processor->startCommand(static function() use ($processor) {
70+
$processor->startCommand(static function () use ($processor) {
9771
$processor->sendPacket("\x01");
9872
$processor->initClosing();
99-
})->onResolve(static function() use ($processor) {
73+
})->onResolve(static function () use ($processor) {
10074
$processor->close();
10175
});
10276
}
10377

10478
/** @see 14.6.3 COM_INIT_DB */
10579
public function useDb(string $db): Promise {
10680
$processor = $this->processor;
107-
return $processor->startCommand(static function() use ($processor, $db) {
81+
return $processor->startCommand(static function () use ($processor, $db) {
10882
$processor->config->db = $db;
10983
$processor->sendPacket("\x02$db");
11084
});
@@ -114,7 +88,7 @@ public function useDb(string $db): Promise {
11488
public function query(string $query): Promise {
11589
$processor = $this->processor;
11690
return \Amp\call(static function () use ($processor, $query) {
117-
$result = yield $processor->startCommand(static function() use ($processor, $query) {
91+
$result = yield $processor->startCommand(static function () use ($processor, $query) {
11892
$processor->setQuery($query);
11993
$processor->sendPacket("\x03$query");
12094
});
@@ -161,7 +135,7 @@ public function transaction(int $isolation = Transaction::COMMITTED): Promise {
161135
/** @see 14.6.5 COM_FIELD_LIST */
162136
public function listFields(string $table, string $like = "%"): Promise {
163137
$processor = $this->processor;
164-
return $processor->startCommand(static function() use ($processor, $table, $like) {
138+
return $processor->startCommand(static function () use ($processor, $table, $like) {
165139
$processor->sendPacket("\x04$table\0$like");
166140
$processor->setFieldListing();
167141
});
@@ -171,7 +145,7 @@ public function listAllFields(string $table, string $like = "%"): Promise {
171145
$deferred = new Deferred;
172146

173147
$columns = [];
174-
$when = function($error, $array) use (&$columns, &$when, $deferred) {
148+
$onResolve = function ($error, $array) use (&$columns, &$onResolve, $deferred) {
175149
if ($error) {
176150
$deferred->fail($error);
177151
return;
@@ -181,25 +155,25 @@ public function listAllFields(string $table, string $like = "%"): Promise {
181155
return;
182156
}
183157
list($columns[], $promise) = $array;
184-
$promise->onResolve($when);
158+
$promise->onResolve($onResolve);
185159
};
186-
$this->listFields($table, $like)->onResolve($when);
160+
$this->listFields($table, $like)->onResolve($onResolve);
187161

188162
return $deferred->promise();
189163
}
190164

191165
/** @see 14.6.6 COM_CREATE_DB */
192166
public function createDatabase($db) {
193167
$processor = $this->processor;
194-
return $processor->startCommand(static function() use ($processor, $db) {
168+
return $processor->startCommand(static function () use ($processor, $db) {
195169
$processor->sendPacket("\x05$db");
196170
});
197171
}
198172

199173
/** @see 14.6.7 COM_DROP_DB */
200174
public function dropDatabase(string $db): Promise {
201175
$processor = $this->processor;
202-
return $processor->startCommand(static function() use ($processor, $db) {
176+
return $processor->startCommand(static function () use ($processor, $db) {
203177
$processor->sendPacket("\x06$db");
204178
});
205179
}
@@ -210,23 +184,23 @@ public function dropDatabase(string $db): Promise {
210184
*/
211185
public function refresh(int $subcommand): Promise {
212186
$processor = $this->processor;
213-
return $processor->startCommand(static function() use ($processor, $subcommand) {
187+
return $processor->startCommand(static function () use ($processor, $subcommand) {
214188
$processor->sendPacket("\x07" . chr($subcommand));
215189
});
216190
}
217191

218192
/** @see 14.6.9 COM_SHUTDOWN */
219193
public function shutdown(): Promise {
220194
$processor = $this->processor;
221-
return $processor->startCommand(static function() use ($processor) {
195+
return $processor->startCommand(static function () use ($processor) {
222196
$processor->sendPacket("\x08\x00"); /* SHUTDOWN_DEFAULT / SHUTDOWN_WAIT_ALL_BUFFERS, only one in use */
223197
});
224198
}
225199

226200
/** @see 14.6.10 COM_STATISTICS */
227201
public function statistics(): Promise {
228202
$processor = $this->processor;
229-
return $processor->startCommand(static function() use ($processor) {
203+
return $processor->startCommand(static function () use ($processor) {
230204
$processor->sendPacket("\x09");
231205
$processor->setStatisticsReading();
232206
});
@@ -235,7 +209,7 @@ public function statistics(): Promise {
235209
/** @see 14.6.11 COM_PROCESS_INFO */
236210
public function processInfo(): Promise {
237211
$processor = $this->processor;
238-
return $processor->startCommand(static function() use ($processor) {
212+
return $processor->startCommand(static function () use ($processor) {
239213
$processor->sendPacket("\x0a");
240214
$processor->setQuery("SHOW PROCESSLIST");
241215
});
@@ -244,23 +218,23 @@ public function processInfo(): Promise {
244218
/** @see 14.6.13 COM_PROCESS_KILL */
245219
public function killProcess($process): Promise {
246220
$processor = $this->processor;
247-
return $processor->startCommand(static function() use ($processor, $process) {
221+
return $processor->startCommand(static function () use ($processor, $process) {
248222
$processor->sendPacket("\x0c" . DataTypes::encode_int32($process));
249223
});
250224
}
251225

252226
/** @see 14.6.14 COM_DEBUG */
253227
public function debugStdout(): Promise {
254228
$processor = $this->processor;
255-
return $processor->startCommand(static function() use ($processor) {
229+
return $processor->startCommand(static function () use ($processor) {
256230
$processor->sendPacket("\x0d");
257231
});
258232
}
259233

260234
/** @see 14.6.15 COM_PING */
261235
public function ping(): Promise {
262236
$processor = $this->processor;
263-
return $processor->startCommand(static function() use ($processor) {
237+
return $processor->startCommand(static function () use ($processor) {
264238
$processor->sendPacket("\x0e");
265239
});
266240
}
@@ -292,15 +266,15 @@ public function changeUser($user, $pass, $db = null) {
292266
/** @see 14.6.19 COM_RESET_CONNECTION */
293267
public function resetConnection() {
294268
$processor = $this->processor;
295-
return $processor->startCommand(static function() use ($processor) {
269+
return $processor->startCommand(static function () use ($processor) {
296270
$processor->sendPacket("\x1f");
297271
});
298272
}
299273

300274
/** @see 14.7.4 COM_STMT_PREPARE */
301275
public function prepare(string $query): Promise {
302276
$processor = $this->processor;
303-
$promise = $processor->startCommand(static function() use ($processor, $query) {
277+
$promise = $processor->startCommand(static function () use ($processor, $query) {
304278
$processor->setPrepare($query);
305279
$regex = <<<'REGEX'
306280
(["'`])(?:\\(?:\\|\1)|(?!\1).)*+\1(*SKIP)(*F)|(\?)|:([a-zA-Z_]+)

0 commit comments

Comments
 (0)