Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Static Code Analysis #72

Merged
merged 3 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Static Code Analysis

on:
push:
pull_request:

jobs:
tests:
name: PHP${{ matrix.php-version }} Static Code Analysis on Linux
runs-on: "${{ matrix.os }}"
strategy:
matrix:
os: [ ubuntu-latest ]
php-version: [ '7.2', '7.3', '7.4', '8.0', '8.1' ]
max-parallel: 5
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: swoole
coverage: none

- name: Setup Dependencies
env:
COMPOSER_ROOT_VERSION: 1.4-dev
run:
composer install -o

- name: Run Analysis
run: |
composer analyse
1 change: 1 addition & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.20-blue.svg)](https://github.com/swoole/swoole-src)

[![GitHub Actions](https://github.com/simps/mqtt/workflows/PHPUnit%20for%20MQTT/badge.svg)](https://github.com/simps/mqtt/actions)
[![Static Code Analysis](https://github.com/simps/mqtt/actions/workflows/phpstan.yml/badge.svg)](https://github.com/simps/mqtt/actions/workflows/phpstan.yml)
[![Gitee Star](https://gitee.com/phpmqtt/mqtt/badge/star.svg?theme=dark)](https://gitee.com/phpmqtt/mqtt/stargazers)
[![Simps 交流群](https://img.shields.io/badge/QQ%E7%BE%A4-983679945-orange)](https://shang.qq.com/wpa/qunwpa?idkey=587be6cdd66b437f2306b1a6d7263fd8096b70ef1db716a259485219f28d41a3)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Support for `QoS 0`, `QoS 1`, `QoS 2`.
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.20-blue.svg)](https://github.com/swoole/swoole-src)

[![GitHub Actions](https://github.com/simps/mqtt/workflows/PHPUnit%20for%20MQTT/badge.svg)](https://github.com/simps/mqtt/actions)
[![Static Code Analysis](https://github.com/simps/mqtt/actions/workflows/phpstan.yml/badge.svg)](https://github.com/simps/mqtt/actions/workflows/phpstan.yml)
[![Gitee Star](https://gitee.com/phpmqtt/mqtt/badge/star.svg?theme=dark)](https://gitee.com/phpmqtt/mqtt/stargazers)
[![Simps 交流群](https://img.shields.io/badge/QQ%E7%BE%A4-983679945-orange)](https://shang.qq.com/wpa/qunwpa?idkey=587be6cdd66b437f2306b1a6d7263fd8096b70ef1db716a259485219f28d41a3)

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"friendsofphp/php-cs-fixer": "^3.0",
"phpunit/phpunit": "^8.5",
"swoole/ide-helper": ">=4.4.20",
"simps/mqtt-cli": "*"
"simps/mqtt-cli": "*",
"phpstan/phpstan": "^1.0"
},
"suggest": {
"ext-swoole": "The ext-swoole >= v4.4.20 or v4.5.3 needs to be loaded when using the MQTT Client."
Expand All @@ -49,7 +50,8 @@
"scripts": {
"cs-check": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix --dry-run",
"cs-fix": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix",
"test": "./tests/co-phpunit --colors=always"
"test": "./tests/co-phpunit --colors=always",
"analyse": "phpstan analyse --memory-limit 4096M -c phpstan.neon"
},
"extra": {
"branch-alias": {
Expand Down
25 changes: 25 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
parameters:
level: 6
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
bootstrapFiles:
- examples/bootstrap.php
paths:
- src
- tests
excludePaths:
- src/Tools/Common.php
ignoreErrors:
- '#Call to an undefined method \S+::setTopic\(\).#'
- '#Method Simps\\MQTT\\Message\\[a-zA-Z]+::getContents\(\) has no return type specified.#'
-
message: '#Method \S+ has no return type specified.#'
paths:
- tests/*
- src/Client.php
-
message: '#Property \S+ has no type specified.#'
paths:
- tests/*
- src/Message/*
- src/Property/PacketMap.php
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ class Client
/** @var Coroutine\Client|\Swoole\Client */
private $client;

/** @var int */
private $messageId = 0;

/** @var array */
private $connectData = [];

/** @var string */
private $host;

/** @var int */
private $port;

/** @var ClientConfig */
private $config;

/** @var int */
private $clientType;

public const COROUTINE_CLIENT_TYPE = 1;
Expand Down
14 changes: 13 additions & 1 deletion src/Config/ClientConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,42 @@

class ClientConfig extends AbstractConfig
{
/** @var string */
protected $clientId = '';

/** @var array */
protected $swooleConfig = [
'open_mqtt_protocol' => true,
];

/** @var string */
protected $userName = '';

/** @var string */
protected $password = '';

/** @var int */
protected $keepAlive = 0;

/** @var string */
protected $protocolName = ProtocolInterface::MQTT_PROTOCOL_NAME;

/** @var int */
protected $protocolLevel = ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1_1;

/** @var array */
protected $properties = [];

/** @var int */
protected $delay = 3000;

/** @var int */
protected $maxAttempts = -1;

/** @var int */
protected $sockType = SWOOLE_SOCK_TCP;

/** @var int */
protected $verbose = MQTT_VERBOSE_NONE;

public function getClientId(): string
Expand Down Expand Up @@ -188,7 +200,7 @@ public function getVerbose(): int
return $this->verbose;
}

public function setVerbose(int $verbose)
public function setVerbose(int $verbose): self
{
$this->verbose = $verbose;

Expand Down
2 changes: 2 additions & 0 deletions src/Hex/ReasonCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ abstract class ReasonCode
public const WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED = 0xA2;

/**
* @var array
* @see https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901079
*/
protected static $reasonPhrases = [
Expand Down Expand Up @@ -154,6 +155,7 @@ abstract class ReasonCode
self::PACKET_IDENTIFIER_NOT_FOUND => 'Packet Identifier not found',
];

/** @var array */
protected static $qosReasonPhrases = [
self::GRANTED_QOS_0 => 'Granted QoS 0',
self::GRANTED_QOS_1 => 'Granted QoS 1',
Expand Down
4 changes: 2 additions & 2 deletions src/Message/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public function setProperties(array $properties): self
return $this;
}

public function isMQTT5()
public function isMQTT5(): bool
{
return $this->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0;
}

abstract public function getContents(bool $getArray = false);

public function toArray()
public function toArray(): array
{
return $this->getContents(true);
}
Expand Down
1 change: 1 addition & 0 deletions src/Packet/UnPackV5.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function connect(string $remaining): array
$properties = UnPackProperty::connect($propertiesTotalLength, $remaining);
}
$clientId = UnPackTool::string($remaining);
$willProperties = [];
if ($willFlag) {
$willPropertiesTotalLength = UnPackTool::byte($remaining);
if ($willPropertiesTotalLength) {
Expand Down
1 change: 1 addition & 0 deletions src/Protocol/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Types

public const AUTH = 15; // Authentication exchange

/** @var array */
protected static $types = [
self::CONNECT => 'connect',
self::CONNACK => 'connack',
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
abstract class Common
{
public static function printf(string $data)
public static function printf(string $data): void
{
echo "\033[36m";
for ($i = 0; $i < strlen($data); $i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/Tools/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function hexStream(): string
return bin2hex($this->getEncode());
}

private function toHexDump($contents, $hasAscii = false)
private function toHexDump(string $contents, bool $hasAscii = false): string
{
$address = $column = 0;
$result = $hexDump = $asciiDump = '';
Expand Down Expand Up @@ -98,7 +98,7 @@ private function toHexDump($contents, $hasAscii = false)
return $result;
}

private function toAscii($contents)
private function toAscii(string $contents): string
{
$address = $column = 0;
$result = $asciiDump = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/PackTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function packHeader(int $type, int $bodyLength, int $dup = 0, int
return chr($type) . static::packRemainingLength($bodyLength);
}

private static function packRemainingLength(int $bodyLength): string
protected static function packRemainingLength(int $bodyLength): string
{
$string = '';
do {
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/UnPackTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function varInt(string &$remaining, ?int &$len): int
return $result;
}

private static function getRemainingLength(string $data, ?int &$headBytes): int
protected static function getRemainingLength(string $data, ?int &$headBytes): int
{
$headBytes = $multiplier = 1;
$value = 0;
Expand Down