Skip to content

Commit aae0d91

Browse files
committed
Merge branch 'further_code_cleanup' into develop
2 parents 1b0724f + 86c996d commit aae0d91

27 files changed

+1314
-676
lines changed

CONTRIBUTING.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,41 @@ If you develop a new feature please create a new branch.
2222

2323
Version
2424
-------
25+
2526
Version number: 0.#version.#hotfix
27+
28+
Further code convention adopted
29+
-------------------------------
30+
31+
- Each method and class is documented with a docblock
32+
33+
Example for a function or methods:
34+
```
35+
/**
36+
* Get formatted date
37+
*
38+
* @param string $location
39+
*
40+
* @return string
41+
*/
42+
```
43+
44+
- Each file is provided with the following header:
45+
```
46+
/**
47+
* This file is part of the TelegramBot package.
48+
*
49+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
50+
*
51+
* For the full copyright and license information, please view the LICENSE
52+
* file that was distributed with this source code.
53+
*/
54+
```
55+
- No empty line after class declaration
56+
- No empty line after `<?php`
57+
- Last element of a multi-line array must be followed by a comma
58+
- One empty line after the docblock header
59+
- Array elements and `use` statements should be written in a logical order, preferrably alphabetically
60+
- Use [] instead of array()
61+
- Use single quotes (`'`) for string
62+
- Leave space in front and behind the concatenation operator `.`

src/Admin/ChatsCommand.php

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
11
<?php
2-
/*
2+
/**
33
* This file is part of the TelegramBot package.
44
*
55
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
66
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
9-
*/
9+
*/
10+
1011
namespace Longman\TelegramBot\Commands;
1112

12-
use Longman\TelegramBot\Request;
13-
use Longman\TelegramBot\DB;
1413
use Longman\TelegramBot\Command;
15-
use Longman\TelegramBot\Entities\Update;
14+
use Longman\TelegramBot\DB;
1615
use Longman\TelegramBot\Entities\Chat;
17-
use Longman\TelegramBot\Exception\TelegramException;
16+
use Longman\TelegramBot\Request;
1817

18+
/**
19+
* Admin "/chats" command
20+
*/
1921
class ChatsCommand extends Command
2022
{
23+
/**#@+
24+
* {@inheritdoc}
25+
*/
2126
protected $name = 'chats';
2227
protected $description = 'List all chats stored by the bot';
23-
protected $usage = '/chats ';
24-
protected $version = '1.0.0';
25-
protected $enabled = true;
28+
protected $usage = '/chats';
29+
protected $version = '1.0.1';
2630
protected $public = true;
27-
//need Mysql
28-
protected $need_mysql = true;
31+
protected $need_mysql = false;
32+
/**#@-*/
2933

34+
/**
35+
* Execution if MySQL is required but not available
36+
*
37+
* @return boolean
38+
*/
3039
public function executeNoDB()
3140
{
32-
//Database not setted or without connection
3341
//Preparing message
3442
$message = $this->getMessage();
3543
$chat_id = $message->getChat()->getId();
36-
$data = [];
37-
$data['chat_id'] = $chat_id;
38-
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
39-
$result = Request::sendMessage($data);
40-
return $result->isOk();
44+
45+
$data = [
46+
'chat_id' => $chat_id,
47+
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
48+
];
49+
50+
return Request::sendMessage($data)->isOk();
4151
}
4252

53+
/**
54+
* Execute command
55+
*
56+
* @return boolean
57+
*/
4358
public function execute()
4459
{
45-
$update = $this->getUpdate();
4660
$message = $this->getMessage();
4761

4862
$chat_id = $message->getChat()->getId();
49-
$message_id = $message->getMessageId();
50-
$text = $message->getText(true);
5163

5264
$results = DB::selectChats(
5365
true, //Send to groups (group chat)
@@ -60,39 +72,39 @@ public function execute()
6072
$user_chats = 0;
6173
$group_chats = 0;
6274
$super_group_chats = 0;
63-
$text = "List of bot chats:\n";
75+
$text = 'List of bot chats:' . "\n";
6476

6577
foreach ($results as $result) {
66-
//initialize a chat object
78+
//Initialize a chat object
6779
$result['id'] = $result['chat_id'];
68-
6980
$chat = new Chat($result);
7081

7182
if ($chat->isPrivateChat()) {
72-
$text .= '- P '.$chat->tryMention()."\n";
83+
$text .= '- P ' . $chat->tryMention() . "\n";
7384
++$user_chats;
7485
} elseif ($chat->isGroupChat()) {
75-
$text .= '- G '.$chat->getTitle()."\n";
86+
$text .= '- G ' . $chat->getTitle() . "\n";
7687
++$group_chats;
7788
} elseif ($chat->isSuperGroup()) {
78-
$text .= '- S '.$chat->getTitle()."\n";
89+
$text .= '- S ' . $chat->getTitle() . "\n";
7990
++$super_group_chats;
8091
}
81-
8292
}
83-
if (($group_chats + $user_chats + $super_group_chats) == 0) {
84-
$text = "No chats found..";
93+
94+
if (($user_chats + $group_chats + $super_group_chats) === 0) {
95+
$text = 'No chats found..';
8596
} else {
86-
$text .= "\nPrivate Chats: ".$user_chats;
87-
$text .= "\nGroup: ".$group_chats;
88-
$text .= "\nSuper Group: ".$super_group_chats;
89-
$text .= "\nTot: ".($group_chats + $user_chats + $super_group_chats);
97+
$text .= "\n" . 'Private Chats: ' . $user_chats;
98+
$text .= "\n" . 'Group: ' . $group_chats;
99+
$text .= "\n" . 'Super Group: ' . $super_group_chats;
100+
$text .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
90101
}
91102

92-
$data = [];
93-
$data['chat_id'] = $chat_id;
94-
$data['text'] = $text;
95-
$result = Request::sendMessage($data);
96-
return $result->isOk();
103+
$data = [
104+
'chat_id' => $chat_id,
105+
'text' => $text,
106+
];
107+
108+
return Request::sendMessage($data)->isOk();
97109
}
98110
}

src/Admin/SendtoallCommand.php

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,72 @@
11
<?php
2-
3-
/*
2+
/**
43
* This file is part of the TelegramBot package.
54
*
65
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
76
*
87
* For the full copyright and license information, please view the LICENSE
98
* file that was distributed with this source code.
10-
*/
9+
*/
10+
1111
namespace Longman\TelegramBot\Commands;
1212

13-
use Longman\TelegramBot\Request;
14-
use Longman\TelegramBot\DB;
1513
use Longman\TelegramBot\Command;
16-
use Longman\TelegramBot\Entities\Update;
17-
use Longman\TelegramBot\Exception\TelegramException;
14+
use Longman\TelegramBot\Request;
1815

16+
/**
17+
* Admin "/sendtoall" command
18+
*/
1919
class SendtoallCommand extends Command
2020
{
21+
/**#@+
22+
* {@inheritdoc}
23+
*/
2124
protected $name = 'sendtoall';
2225
protected $description = 'Send the message to all the user\'s bot';
2326
protected $usage = '/sendall <message to send>';
24-
protected $version = '1.2.0';
25-
protected $enabled = true;
27+
protected $version = '1.2.1';
2628
protected $public = true;
27-
//need Mysql
2829
protected $need_mysql = true;
30+
/**#@-*/
2931

32+
/**
33+
* Execution if MySQL is required but not available
34+
*
35+
* @return boolean
36+
*/
3037
public function executeNoDB()
3138
{
32-
//Database not setted or without connection
3339
//Preparing message
3440
$message = $this->getMessage();
3541
$chat_id = $message->getChat()->getId();
36-
$data = [];
37-
$data['chat_id'] = $chat_id;
38-
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
39-
$result = Request::sendMessage($data);
40-
return $result->isOk();
42+
43+
$data = [
44+
'chat_id' => $chat_id,
45+
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
46+
];
47+
48+
return Request::sendMessage($data)->isOk();
4149
}
4250

51+
/**
52+
* Execute command
53+
*
54+
* @todo Don't use empty, as a string of '0' is regarded to be empty
55+
*
56+
* @return boolean
57+
*/
4358
public function execute()
4459
{
45-
$update = $this->getUpdate();
4660
$message = $this->getMessage();
4761

4862
$chat_id = $message->getChat()->getId();
49-
$message_id = $message->getMessageId();
50-
$text = $message->getText(true);
5163

5264
if (empty($text)) {
53-
$text = 'Write the message to sent: /sendall <message>';
65+
$text = 'Write the message to send: /sendall <message>';
5466
} else {
5567
$results = Request::sendToActiveChats(
5668
'sendMessage', //callback function to execute (see Request.php methods)
57-
array('text'=> $text), //Param to evaluate the request
69+
['text' => $text], //Param to evaluate the request
5870
true, //Send to groups (group chat)
5971
true, //Send to super groups chats (super group chat)
6072
true, //Send to users (single chat)
@@ -65,7 +77,7 @@ public function execute()
6577
$tot = 0;
6678
$fail = 0;
6779

68-
$text = "Message sended to:\n";
80+
$text = 'Message sent to:' . "\n";
6981
foreach ($results as $result) {
7082
$status = '';
7183
$type = '';
@@ -88,19 +100,19 @@ public function execute()
88100
}
89101
++$tot;
90102

91-
$text .= $tot.') '.$status.' '.$type.' '.$name."\n";
103+
$text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
92104
}
93-
$text .= "Delivered: ".($tot - $fail).'/'.$tot."\n";
105+
$text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
94106
}
95-
if ($tot == 0) {
96-
$text = "No users or chats found..";
107+
if ($tot === 0) {
108+
$text = 'No users or chats found..';
97109
}
98110

99-
$data = [];
100-
$data['chat_id'] = $chat_id;
101-
$data['text'] = $text;
111+
$data = [
112+
'chat_id' => $chat_id,
113+
'text' => $text,
114+
];
102115

103-
$result = Request::sendMessage($data);
104-
return $result->isOk();
116+
return Request::sendMessage($data)->isOk();
105117
}
106118
}

0 commit comments

Comments
 (0)