Skip to content

Commit

Permalink
Merge pull request #294 from marcosmarcolin/refactor-phpcs-part-1
Browse files Browse the repository at this point in the history
refactor: code improvements with php_codesniffer without affecting logic, part 1
  • Loading branch information
walkor committed Jul 18, 2023
2 parents e29a597 + cfd0e14 commit 902ab18
Show file tree
Hide file tree
Showing 27 changed files with 1,302 additions and 1,415 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/docs/ export-ignore
/examples/ export-ignore
/tests/ export-ignore
/phpcs.xml export-ignore
37 changes: 26 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
{
"name" : "workerman/phpsocket.io",
"type" : "library",
"keywords": ["socket.io"],
"homepage": "http://www.workerman.net",
"license" : "MIT",
"require": {
"workerman/workerman" : "^4.0.0",
"workerman/channel" : ">=1.0.0"
},
"autoload": {
"psr-4": {"PHPSocketIO\\": "./src"}
"name": "workerman/phpsocket.io",
"description": "A server side alternative implementation of socket.io in PHP based on Workerman",
"type": "library",
"keywords": [
"socket.io",
"phpsocket.io",
"workerman",
"sockets",
"async",
"stream",
"server",
"non-blocking"
],
"homepage": "https://www.workerman.net",
"license": "MIT",
"require": {
"workerman/workerman": "^4.0.0",
"workerman/channel": ">=1.0.0"
},
"autoload": {
"psr-4": {
"PHPSocketIO\\": "./src"
}
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.7"
}
}
33 changes: 33 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<ruleset name="PHPSocket.io Coding Standard">
<description>PHPSocket.io Coding Standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="colors"/>

<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/examples/*</exclude-pattern>

<!-- inherit rules from: -->
<rule ref="PSR2"/>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="200"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.Formatting.SpaceAfterNot"/>
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>
</ruleset>
105 changes: 49 additions & 56 deletions src/ChannelAdapter.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
<?php

namespace PHPSocketIO;

use Exception;

class ChannelAdapter extends DefaultAdapter
{
protected $_channelId = null;

public static $ip = '127.0.0.1';

public static $port = 2206;


/**
* @throws Exception
*/
public function __construct($nsp)
{
parent::__construct($nsp);
$this->_channelId = (function_exists('random_int') ? random_int(1, 10000000): rand(1, 10000000)) . "-" . (function_exists('posix_getpid') ? posix_getpid(): 1);
$this->_channelId = (function_exists('random_int') ? random_int(1, 10000000) : rand(1, 10000000)) . "-" . (function_exists('posix_getpid') ? posix_getpid() : 1);
\Channel\Client::connect(self::$ip, self::$port);
\Channel\Client::$onMessage = array($this, 'onChannelMessage');
\Channel\Client::$onMessage = [$this, 'onChannelMessage'];
\Channel\Client::subscribe("socket.io#/#");
Debug::debug('ChannelAdapter __construct');
}

public function __destruct()
{
Debug::debug('ChannelAdapter __destruct');
}
public function add($id ,$room)

public function add($id, $room)
{
$this->sids[$id][$room] = true;
$this->rooms[$room][$id] = true;
$channel = "socket.io#/#$room#";
\Channel\Client::subscribe($channel);
}

public function del($id, $room)
{
unset($this->sids[$id][$room]);
unset($this->rooms[$room][$id]);
if(empty($this->rooms[$room]))
{
if (empty($this->rooms[$room])) {
unset($this->rooms[$room]);
$channel = "socket.io#/#$room#";
\Channel\Client::unsubscribe($channel);
}
}

public function delAll($id)
{
$rooms = isset($this->sids[$id]) ? array_keys($this->sids[$id]) : array();
if($rooms)
{
foreach($rooms as $room)
{
if(isset($this->rooms[$room][$id]))
{
$rooms = isset($this->sids[$id]) ? array_keys($this->sids[$id]) : [];
if ($rooms) {
foreach ($rooms as $room) {
if (isset($this->rooms[$room][$id])) {
unset($this->rooms[$room][$id]);
$channel = "socket.io#/#$room#";
\Channel\Client::unsubscribe($channel);
}
if(isset($this->rooms[$room]) && empty($this->rooms[$room]))
{
if (isset($this->rooms[$room]) && empty($this->rooms[$room])) {
unset($this->rooms[$room]);
}
}
Expand All @@ -67,58 +69,49 @@ public function delAll($id)

public function onChannelMessage($channel, $msg)
{
if($this->_channelId === array_shift($msg))
{
if ($this->_channelId === array_shift($msg)) {
//echo "ignore same channel_id \n";
return;
}

$packet = $msg[0];

$opts = $msg[1];

if(!$packet)
{

if (! $packet) {
echo "invalid channel:$channel packet \n";
return;
}

if(empty($packet['nsp']))
{

if (empty($packet['nsp'])) {
$packet['nsp'] = '/';
}

if($packet['nsp'] != $this->nsp->name)
{
echo "ignore different namespace {$packet['nsp']} != {$this->nsp->name}\n";
return;

if ($packet['nsp'] != $this->nsp->name) {
echo "ignore different namespace {$packet['nsp']} != {$this->nsp->name}\n";
return;
}

$this->broadcast($packet, $opts, true);
}

public function broadcast($packet, $opts, $remote = false)
{
parent::broadcast($packet, $opts);
if (!$remote)
{
if (! $remote) {
$packet['nsp'] = '/';

if(!empty($opts['rooms']))
{
foreach($opts['rooms'] as $room)
{
$chn = "socket.io#/#$room#";
$msg = array($this->_channelId, $packet, $opts);
\Channel\Client::publish($chn, $msg);
}
}
else
{
$chn = "socket.io#/#";
$msg = array($this->_channelId, $packet, $opts);
\Channel\Client::publish($chn, $msg);

if (! empty($opts['rooms'])) {
foreach ($opts['rooms'] as $room) {
$chn = "socket.io#/#$room#";
$msg = [$this->_channelId, $packet, $opts];
\Channel\Client::publish($chn, $msg);
}
} else {
$chn = "socket.io#/#";
$msg = [$this->_channelId, $packet, $opts];
\Channel\Client::publish($chn, $msg);
}
}
}
}
}

0 comments on commit 902ab18

Please sign in to comment.