Skip to content

Commit f18c242

Browse files
committed
Add Coding Standards and Static Analysis
1 parent e433236 commit f18c242

19 files changed

+309
-183
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
22
/composer.lock
3+
/.phpcs-cache

Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
PHP ?= php7.4
2+
COMPOSER ?= $(PHP) $(shell which composer)
3+
4+
test: phpstan phpcs
5+
6+
fix: phpcbf
7+
8+
vendor/bin/phpstan vendor/bin/phpcs vendor/bin/phpcbf: vendor/autoload.php
9+
10+
vendor/autoload.php: composer.lock
11+
$(COMPOSER) install
12+
13+
phpstan: vendor/bin/phpstan
14+
$(PHP) vendor/bin/phpstan analyse -l max lib -a include/dbus-php.php
15+
16+
phpcs phpcbf: vendor/bin/$@
17+
$(PHP) vendor/bin/$@

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"autoload": {
1111
"psr-4": {
12-
"":"lib"
12+
"Paxal\\DBus\\":"lib"
1313
}
1414
},
1515
"license": "MIT",
@@ -20,6 +20,8 @@
2020
}
2121
],
2222
"require-dev": {
23-
"friendsofphp/php-cs-fixer": "^2.15"
23+
"doctrine/coding-standard": "^6.0",
24+
"phpstan/phpstan": "^0.11.12",
25+
"phpstan/phpstan-strict-rules": "^0.11.1"
2426
}
2527
}

examples/bluez-music-notify.php

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
<?php
22

3-
require_once __DIR__.'/../vendor/autoload.php';
4-
$ffi = require_once __DIR__.'/../load.php';
3+
declare(strict_types=1);
4+
5+
use Paxal\DBus\DBus;
6+
use Paxal\DBus\DBusMessageIterDecoder;
7+
use Paxal\DBus\Message;
8+
9+
require_once __DIR__ . '/../vendor/autoload.php';
10+
$ffi = require_once __DIR__ . '/../load.php';
511

612
$err = $ffi->new('struct DBusError');
713
$ffi->dbus_error_init(FFI::addr($err));
814
$args = $ffi->new('struct DBusMessageIter');
915
$conn = $ffi->dbus_bus_get(DBus::BUS_SYSTEM, FFI::addr($err));
1016

11-
$ffi->dbus_bus_add_match($conn,
12-
"arg0=org.bluez.MediaPlayer1",
13-
FFI::addr($err));
17+
$ffi->dbus_bus_add_match(
18+
$conn,
19+
'arg0=org.bluez.MediaPlayer1',
20+
FFI::addr($err)
21+
);
1422
$ffi->dbus_connection_flush($conn);
1523
if ($ffi->dbus_error_is_set(FFI::addr($err))) {
1624
fprintf(stderr, "Match Error (%s)\n", $err->message);
@@ -26,20 +34,20 @@
2634
$end = microtime(true);
2735

2836
// loop again if we haven't read a message
29-
if (NULL === $msg) {
37+
if ($msg === null) {
3038
usleep(100000);
3139
continue;
3240
}
3341

34-
if (!$ffi->dbus_message_iter_init($msg, FFI::addr($args))) {
42+
if (! $ffi->dbus_message_iter_init($msg, FFI::addr($args))) {
3543
error_log("Message has no arguments!\n");
3644
continue;
3745
}
3846

39-
$message = new Message();
47+
$message = new Message();
4048
$message->interface = $ffi->dbus_message_get_interface($msg);
41-
$message->path = $ffi->dbus_message_get_path($msg);
42-
$message->member = $ffi->dbus_message_get_member($msg);
49+
$message->path = $ffi->dbus_message_get_path($msg);
50+
$message->member = $ffi->dbus_message_get_member($msg);
4351
$message->arguments = $decoder->decode($args);
4452

4553
$ffi->dbus_message_unref($msg);
@@ -53,7 +61,7 @@
5361
}
5462

5563
$arguments = $message->arguments;
56-
if (!is_array($arguments)) {
64+
if (! is_array($arguments)) {
5765
continue;
5866
}
5967

@@ -63,17 +71,21 @@
6371
}
6472

6573
$track = $arguments[1]['Track'] ?? null;
66-
if (null === $track) {
74+
if ($track === null) {
6775
continue;
6876
}
6977

7078
$item = $track['Item'] ?? null;
71-
if (null === $item) {
79+
if ($item === null) {
7280
continue;
7381
}
7482

75-
if ($item !== $lastItem) {
76-
$lastItem = $item;
77-
exec('notify-send '.join(' ', array_map('escapeshellarg', ['-t', '5000', '-a', 'Music', $track['Title'], $track['Artist'].' - '.$track['Album']])));
83+
if ($item === $lastItem) {
84+
continue;
7885
}
86+
87+
$lastItem = $item;
88+
$arguments = ['-t', '5000', '-a', 'Music', $track['Title'], $track['Artist'] . ' - ' . $track['Album']];
89+
$escapedArguments = array_map('escapeshellarg', $arguments);
90+
exec('notify-send ' . implode(' ', $escapedArguments));
7991
}

examples/bluez-volume-sync.php

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
use Paxal\DBus\DBus;
6+
use Paxal\DBus\DBusMessageIterDecoder;
7+
use Paxal\DBus\Message;
8+
39
const BLUEZ_TO_PA_FACTOR = 65535/127;
410

5-
require_once __DIR__.'/../vendor/autoload.php';
6-
$ffi = require_once __DIR__.'/../load.php';
11+
require_once __DIR__ . '/../vendor/autoload.php';
12+
$ffi = require_once __DIR__ . '/../load.php';
713

814
$err = $ffi->new('struct DBusError');
915
$ffi->dbus_error_init(FFI::addr($err));
1016
$args = $ffi->new('struct DBusMessageIter');
1117
$conn = $ffi->dbus_bus_get(DBus::BUS_SYSTEM, FFI::addr($err));
1218

13-
$ffi->dbus_bus_add_match($conn,
14-
"arg0=org.bluez.MediaTransport1",
15-
FFI::addr($err));
19+
$ffi->dbus_bus_add_match(
20+
$conn,
21+
'arg0=org.bluez.MediaTransport1',
22+
FFI::addr($err)
23+
);
1624
$ffi->dbus_connection_flush($conn);
1725
if ($ffi->dbus_error_is_set(FFI::addr($err))) {
1826
fprintf(STDERR, "Match Error (%s)\n", $err->message);
@@ -28,20 +36,20 @@
2836
$end = microtime(true);
2937

3038
// loop again if we haven't read a message
31-
if (NULL === $msg) {
39+
if ($msg === null) {
3240
usleep(100000);
3341
continue;
3442
}
3543

36-
if (!$ffi->dbus_message_iter_init($msg, FFI::addr($args))) {
44+
if (! $ffi->dbus_message_iter_init($msg, FFI::addr($args))) {
3745
error_log("Message has no arguments!\n");
3846
continue;
3947
}
4048

41-
$message = new Message();
49+
$message = new Message();
4250
$message->interface = $ffi->dbus_message_get_interface($msg);
43-
$message->path = $ffi->dbus_message_get_path($msg);
44-
$message->member = $ffi->dbus_message_get_member($msg);
51+
$message->path = $ffi->dbus_message_get_path($msg);
52+
$message->member = $ffi->dbus_message_get_member($msg);
4553
$message->arguments = $decoder->decode($args);
4654

4755
$ffi->dbus_message_unref($msg);
@@ -55,7 +63,7 @@
5563
}
5664

5765
$arguments = $message->arguments;
58-
if (!is_array($arguments)) {
66+
if (! is_array($arguments)) {
5967
continue;
6068
}
6169

@@ -65,12 +73,12 @@
6573
}
6674

6775
$volume = $arguments[1]['Volume'] ?? null;
68-
if (null === $volume) {
76+
if ($volume === null) {
6977
continue;
7078
}
7179

72-
$addr = preg_replace('@^.*/dev_(.*?)(/.*)$@', '$1', $message->path);
80+
$addr = preg_replace('@^.*/dev_(.*?)(/.*)$@', '$1', $message->path);
7381
$paVolume = (int) (BLUEZ_TO_PA_FACTOR * (int) $volume);
74-
echo "Set volume to ".round($paVolume / 65535 * 100).'%'.PHP_EOL;
75-
exec("pactl set-source-volume bluez_source.{$addr}.a2dp_source {$paVolume}");
82+
echo 'Set volume to ' . round($paVolume / 65535 * 100) . '%' . PHP_EOL;
83+
exec(sprintf('pactl set-source-volume bluez_source.%s.a2dp_source %s', $addr, $paVolume));
7684
}

include/dbus-php.php

+18-31
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,31 @@
11
<?php
22

3-
namespace {
3+
declare(strict_types=1);
44

5-
use FFI\CData;
5+
namespace {
66

77
/**
88
* @method FFI\CData new(string $type)
9-
* @method void dbus_error_init(FFI\CData &$err)
10-
* @method FFI\CData dbus_bus_get(int $bus, FFI\CData &$err)
11-
* @method bool dbus_error_is_set(FFI\CData &$err)
12-
* @method void dbus_error_free(FFI\CData &$err)
13-
* @method void dbus_bus_add_match(FFI\CData $conn, string $match, FFI\CData &$err)
9+
* @method void dbus_error_init(FFI\CData $err)
10+
* @method (FFI\CData|null) dbus_bus_get(int $bus, FFI\CData $err)
11+
* @method bool dbus_error_is_set(FFI\CData $err)
12+
* @method void dbus_error_free(FFI\CData $err)
13+
* @method void dbus_bus_add_match(FFI\CData $conn, string $match, FFI\CData $err)
1414
* @method void dbus_connection_flush(FFI\CData $conn)
1515
* @method void dbus_connection_read_write(FFI\CData $conn, int $timeout)
16-
* @method null|FFI\CData dbus_connection_pop_message(FFI\CData $conn)&
17-
* @method bool dbus_message_iter_init(FFI\CData &$msg, &FFI\CData $args)
18-
* @method dbus_message_unref(FFI\CData &$msg)
19-
* @method null|string dbus_message_get_interface(FFI\CData &$msg)
20-
* @method null|string dbus_message_get_path(FFI\CData &$msg)
21-
* @method null|string dbus_message_get_member(FFI\CData &$msg)
22-
*/
23-
class PaxalDbusPhp extends FFI
24-
{
25-
}
26-
27-
/**
28-
* @method FFI\CData new(string $type)
29-
* @method static FFI\CData addr(FFI\CData $val)
16+
* @method FFI\CData|null dbus_connection_pop_message(FFI\CData $conn)
17+
* @method bool dbus_message_iter_init(FFI\CData $msg, FFI\CData $args)
18+
* @method dbus_message_unref(FFI\CData $msg)
19+
* @method string|null dbus_message_get_interface(FFI\CData $msg)
20+
* @method string|null dbus_message_get_path(FFI\CData $msg)
21+
* @method string|null dbus_message_get_member(FFI\CData $msg)
22+
* @method int dbus_message_iter_get_arg_type(FFI\CData $args)
23+
* @method bool dbus_message_iter_next(FFI\CData $args)
24+
* @method void dbus_message_iter_recurse(FFI\CData $args, FFI\CData $sub)
25+
* @method void dbus_message_iter_get_basic(FFI\CData $args, FFI\CData $value)
3026
*/
31-
class FFI
27+
final class PaxalDbusPhp/* extends FFI */
3228
{
3329
}
34-
}
3530

36-
namespace FFI {
37-
/**
38-
* @property $cdata
39-
*/
40-
class CData
41-
{
42-
}
4331
}
44-

include/ffi.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace {
6+
/**
7+
* @method FFI\CData new(string $type)
8+
* @method static FFI\CData addr(FFI\CData $val)
9+
*/
10+
class FFI
11+
{
12+
}
13+
}
14+
15+
namespace FFI {
16+
/**
17+
* @property $cdata
18+
*/
19+
class CData
20+
{
21+
}
22+
}

0 commit comments

Comments
 (0)