Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-12-26 19:24:24 +00:00 committed by StyleCI Bot
parent 84abb27f18
commit 0a8c04998e
11 changed files with 71 additions and 44 deletions

View File

@ -164,7 +164,7 @@ class Connection extends Tools
}
$packet = stream_get_contents($this->sock, $length);
if (strlen($packet) != $length) {
throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've read ".($length).", read ".strlen($packet).")!");
throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've read ".($length).', read '.strlen($packet).')!');
}
return $packet;

View File

@ -33,7 +33,7 @@ class MTProto extends PrimeModule
public $config = ['expires' => -1];
public $ipv6 = false;
public $should_serialize = true;
public function __construct($settings = [])
{
// Parse settings
@ -67,7 +67,9 @@ class MTProto extends PrimeModule
$this->get_updates_difference();
}
}
public function parse_settings($settings) {
public function parse_settings($settings)
{
// Detect ipv6
$google = '';
try {
@ -194,8 +196,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
],
'updates' => [
'updates_array_limit' => 1000, // How big should be the array containing the updates processed with the default example_update_handler callback
'callback' => [$this, 'get_updates_update_handler'] // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter
]
'callback' => [$this, 'get_updates_update_handler'], // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter
],
];
foreach ($default_settings as $key => $param) {
if (!isset($settings[$key])) {
@ -217,6 +219,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
}
$this->settings = $settings;
}
public function setup_logger()
{
if (!\danog\MadelineProto\Logger::$constructed) {

View File

@ -25,7 +25,7 @@ trait CallHandler
foreach (range(1, $this->settings['max_tries']['query']) as $count) {
try {
\danog\MadelineProto\Logger::log('Calling method (try number '.$count.' for '.$method.')...');
$args = $this->tl->get_named_method_args($method, $args);
$int_message_id = $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method), $message_id);
$this->datacenter->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args];

View File

@ -65,7 +65,6 @@ trait PeerHandler
$this->should_serialize = true;
}
public function get_info($id, $recursive = true)
{
if (is_array($id)) {
@ -100,18 +99,23 @@ trait PeerHandler
$id = '-100'.$id['channel_id'];
break;
default:
throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($id, true));
throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($id, true));
break;
}
}
if (preg_match('/^channel#/', $id)) $id = str_replace('channel#', '-100', $id);
if (preg_match('/^chat#/', $id)) $id = str_replace('chat#', '-', $id);
if (preg_match('/^user#/', $id)) $id = str_replace('user#', '', $id);
if (preg_match('/^channel#/', $id)) {
$id = str_replace('channel#', '-100', $id);
}
if (preg_match('/^chat#/', $id)) {
$id = str_replace('chat#', '-', $id);
}
if (preg_match('/^user#/', $id)) {
$id = str_replace('user#', '', $id);
}
if (is_numeric($id)) {
$id = (int)$id;
$id = (int) $id;
if (isset($this->chats[$id])) {
return $this->gen_all($this->chats[$id]);
}
@ -134,7 +138,8 @@ trait PeerHandler
throw new \danog\MadelineProto\Exception("Couldn't find peer by provided username ".$id);
}
public function gen_all($constructor) {
public function gen_all($constructor)
{
switch ($constructor['_']) {
case 'user':
$inputPeer = $constructor['self'] ? ['_' => 'inputPeerSelf'] : ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
@ -155,13 +160,13 @@ trait PeerHandler
$inputType = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
$Peer = ['_' => 'peerChannel', 'channel_id' => $constructor['id']];
$id = $constructor['id'];
$bot_api_id = (int)('-100'.$constructor['id']);
$bot_api_id = (int) ('-100'.$constructor['id']);
break;
default:
throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($constructor, true));
throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($constructor, true));
break;
}
return ['constructor' => $constructor, 'inputPeer' => $inputPeer, 'inputType' => $inputType, 'Peer' => $Peer, 'id' => $id, 'botApiId' => $bot_api_id];
}

View File

@ -22,7 +22,8 @@ trait UpdateHandler
public $updates = [];
public function get_updates_update_handler($update) {
public function get_updates_update_handler($update)
{
if (count($this->updates) > $this->settings['updates']['updates_array_limit']) {
array_shift($this->updates);
}
@ -30,7 +31,8 @@ trait UpdateHandler
\danog\MadelineProto\Logger::log('Stored ', $update);
}
public function get_updates($offset, $limit = null, $timeout = 0) {
public function get_updates($offset, $limit = null, $timeout = 0)
{
sleep($timeout);
$this->get_updates_difference();
$result = array_slice($this->updates, $offset, $limit, true);
@ -39,15 +41,19 @@ trait UpdateHandler
$updates[] = ['update_id' => $key, 'update' => $value];
unset($this->updates[$key]);
}
return $updates;
}
public function &get_channel_state($channel, $pts = 0) {
public function &get_channel_state($channel, $pts = 0)
{
if (!isset($this->channels_state[$channel])) {
$this->channels_state[$channel] = ['pts' => $pts, 'pop_pts' => [], 'pending_seq_updates' =>[]];
}
return $this->channels_state[$channel];
}
public function update_channel_state($channel, $data)
{
$this->get_channel_state($channel);
@ -59,7 +65,7 @@ trait UpdateHandler
{
$this->get_channel_state($channel);
$difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['inputType'], 'filter' => ['_' => 'channelMessagesFilterEmpty'],'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]);
$difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['inputType'], 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]);
switch ($difference['_']) {
case 'updates.channelDifferenceEmpty':
$this->update_channel_state($difference);
@ -81,6 +87,7 @@ trait UpdateHandler
break;
}
}
public function update_state($data)
{
if (empty($this->updates_state)) {
@ -92,7 +99,6 @@ trait UpdateHandler
$this->updates_state['date'] = (!isset($data['date']) || $data['date'] < $this->updates_state['date']) ? $this->updates_state['date'] : $data['date'];
}
public function get_updates_difference()
{
if (empty($this->updates_state)) {
@ -149,7 +155,7 @@ trait UpdateHandler
$this->add_chats($updates['chats']);
$this->handle_multiple_update($updates['updates'], ['date' => $updates['date'], 'seq' => $updates['seq'], 'seq_start' => $updates['seq_start']]);
break;
case 'updates':
$this->add_users($updates['users']);
$this->add_chats($updates['chats']);
@ -177,6 +183,7 @@ trait UpdateHandler
if (!isset($this->channels_state[$channel_id])) {
return false;
}
return $this->get_channel_difference($channel_id);
break;
}
@ -195,14 +202,14 @@ trait UpdateHandler
!isset($this->get_info($message['to_id'])['bot_api_info'])) {
\danog\MadelineProto\Logger::log('Not enough data for message update');
if ($channel_id !== false && isset($this->chats[$channel_id])) {
$this->get_channel_difference($channel_id);
} else {
$this->get_updates_difference();
}
return false;
}
break;
default:
@ -216,7 +223,7 @@ trait UpdateHandler
$new_pts = $cur_state['pts'] + (isset($update['pts_count']) ? $update['pts_count'] : 0);
if ($new_pts < $update['pts']) {
\danog\MadelineProto\Logger::log('Pts hole', $cur_state, $update, $this->get_info($channel_id));
$this->cur_state['pop_pts'][] = $update;
if ($channel_id && isset($this->chats[$channel_id])) {
@ -289,7 +296,8 @@ trait UpdateHandler
}
}
public function save_update($update) {
public function save_update($update)
{
$this->settings['updates']['callback']($update);
}
}

View File

@ -13,19 +13,23 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto;
/**
* Manages serialization of the MadelineProto instance
* Manages serialization of the MadelineProto instance.
*/
class Serialization {
public static function serialize($filename, $instance, $force = false) {
class Serialization
{
public static function serialize($filename, $instance, $force = false)
{
if ($instance->API->should_serialize || !(file_exists($filename) && !empty(file_get_contents($filename))) || $force) {
$instance->API->should_serialize = false;
return file_put_contents($filename, serialize($instance));
}
return false;
}
public static function deserialize($filename) {
public static function deserialize($filename)
{
return file_exists($filename) ? unserialize(file_get_contents($filename)) : false;
}
}
}

View File

@ -364,6 +364,7 @@ class TL extends \danog\MadelineProto\Tools
if (isset($x['flags'])) { // I don't think we need this anymore
unset($x['flags']);
}
return $x;
}

View File

@ -49,10 +49,9 @@ trait Login
);
$this->API->datacenter->authorized = true;
$this->API->get_updates_difference();
$this->API->should_serialize = true;
$this->API->updates = [];
\danog\MadelineProto\Logger::log('Logged in successfully!');

View File

@ -17,15 +17,18 @@ namespace danog\MadelineProto\Wrappers;
*/
trait PeerHandler
{
public function get_info($id, $recursive = true) {
public function get_info($id, $recursive = true)
{
return $this->API->get_info($id, $recursive);
}
public function gen_all($constructor) {
public function gen_all($constructor)
{
return $this->API->gen_all($constructor);
}
public function resolve_username($username) {
public function resolve_username($username)
{
return $this->API->resolve_username($username);
}
}

View File

@ -15,11 +15,15 @@ namespace danog\MadelineProto\Wrappers;
/**
* Manages changing API instance settings.
*/
trait SettingsManager {
public function get_settings() {
trait SettingsManager
{
public function get_settings()
{
return $this->API->settings;
}
public function update_settings($settings) {
public function update_settings($settings)
{
$this->API->parse_settings($settings);
}
}
}

View File

@ -14,7 +14,7 @@ If not, see <http://www.gnu.org/licenses/>.
require_once 'vendor/autoload.php';
$settings = [];
if (file_exists('web_data.php')) {
require_once('web_data.php');
require_once 'web_data.php';
}
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline');