Skip to content

Commit 7ea8312

Browse files
committed
Code. Psalm L1 implemented.
1 parent 51f87a5 commit 7ea8312

File tree

8 files changed

+163
-346
lines changed

8 files changed

+163
-346
lines changed

example.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@
2323

2424
// Take params from config
2525
$config_url = 'https://moderate.cleantalk.org';
26-
$auth_key = null; // Set Cleantalk auth key
27-
26+
$auth_key = ''; // Set Cleantalk auth key
27+
$validation = CleantalkAPI::method__notice_validate_key($auth_key, 'php-api');
28+
$validation = json_decode($validation) ? json_decode($validation) : false;
29+
$is_valid = is_object($validation) && $validation->valid;
30+
31+
echo "Access key validation result:";
32+
echo CleantalkAPI::method__notice_validate_key($auth_key, 'php-api');
33+
echo "\n";
34+
35+
if (!$is_valid) {
36+
echo "Access key is not valid. Please check access key in the config.\n";
37+
exit;
38+
}
2839

2940
// The facility in which to store the query parameters
3041
$ct_request = new CleantalkRequest();
@@ -49,5 +60,3 @@
4960
} else {
5061
echo 'Comment blocked. Reason ' . $ct_result->comment;
5162
}
52-
echo "<br/>CleantalkAPI call example:<br/>";
53-
var_dump(CleantalkAPI::method__notice_validate_key('', ''));

lib/Cleantalk.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class Cleantalk
6969

7070
/**
7171
* Codepage of the data
72-
* @var bool
72+
* @var string|null
7373
*/
74-
public $data_codepage = null;
74+
public $data_codepage;
7575

7676
/**
7777
* API version to use
@@ -109,6 +109,7 @@ class Cleantalk
109109
* @param CleantalkRequest $request
110110
*
111111
* @return CleantalkResponse
112+
* @throws TransportException
112113
*/
113114
public function isAllowMessage(CleantalkRequest $request)
114115
{
@@ -127,6 +128,7 @@ public function isAllowMessage(CleantalkRequest $request)
127128
* @param CleantalkRequest $request
128129
*
129130
* @return CleantalkResponse
131+
* @throws TransportException
130132
*/
131133
public function isAllowUser(CleantalkRequest $request)
132134
{
@@ -145,6 +147,7 @@ public function isAllowUser(CleantalkRequest $request)
145147
* @param CleantalkRequest $request
146148
*
147149
* @return CleantalkResponse
150+
* @throws TransportException
148151
*/
149152
public function sendFeedback(CleantalkRequest $request)
150153
{
@@ -163,6 +166,7 @@ public function sendFeedback(CleantalkRequest $request)
163166
* @param CleantalkRequest $request
164167
*
165168
* @return CleantalkResponse
169+
* @throws TransportException
166170
*/
167171
public function checkBot(CleantalkRequest $request)
168172
{
@@ -246,11 +250,11 @@ private function filterRequest(CleantalkRequest $request)
246250
/**
247251
* Compress data and encode to base64
248252
*
249-
* @param string
250-
*
253+
* @param string $data
254+
* @psalm-suppress UnusedMethod
251255
* @return string
252256
*/
253-
private function compressData($data = null)
257+
private function compressData($data = '')
254258
{
255259
if ( strlen($data) > $this->dataMaxSise && function_exists('gzencode') && function_exists('base64_encode') ) {
256260
$localData = gzencode($data, $this->compressRate, FORCE_GZIP);
@@ -334,7 +338,7 @@ private function createMsg($method, CleantalkRequest $request)
334338
* @param $url
335339
* @param int $server_timeout
336340
*
337-
* @return boolean|\CleantalkResponse
341+
* @return boolean|CleantalkResponse
338342
*/
339343
private function sendRequest($data, $url, $server_timeout = 3)
340344
{
@@ -397,7 +401,7 @@ private function sendRequest($data, $url, $server_timeout = 3)
397401

398402
if ( ! $result ) {
399403
$allow_url_fopen = ini_get('allow_url_fopen');
400-
if ( function_exists('file_get_contents') && isset($allow_url_fopen) && $allow_url_fopen == '1' ) {
404+
if ( function_exists('file_get_contents') && !empty($allow_url_fopen) && $allow_url_fopen == '1' ) {
401405
$opts = array(
402406
'http' =>
403407
array(
@@ -413,7 +417,7 @@ private function sendRequest($data, $url, $server_timeout = 3)
413417
}
414418
}
415419

416-
if ( ! $result || ! CleantalkHelper::is_json($result) ) {
420+
if ( !is_string($result) || ! CleantalkHelper::is_json($result) ) {
417421
$response = null;
418422
$response['errno'] = 1;
419423
if ( $curl_error ) {
@@ -429,7 +433,7 @@ private function sendRequest($data, $url, $server_timeout = 3)
429433

430434
$errstr = null;
431435
$response = json_decode($result);
432-
if ( $result !== false && is_object($response) ) {
436+
if ( is_object($response) ) {
433437
$response->errno = 0;
434438
$response->errstr = $errstr;
435439
} else {
@@ -451,6 +455,7 @@ private function sendRequest($data, $url, $server_timeout = 3)
451455
* @param $msg
452456
*
453457
* @return CleantalkResponse
458+
* @throws TransportException
454459
*/
455460
private function httpRequest($msg)
456461
{
@@ -473,15 +478,15 @@ private function httpRequest($msg)
473478
$msg->all_headers = ! empty($ct_tmp) ? json_encode($ct_tmp) : '';
474479

475480
// Using current server without changing it
476-
if ( false && ( ! empty($this->work_url) && ($this->server_changed + $this->server_ttl > time())) ) {
481+
if ( ! empty($this->work_url) && ($this->server_changed + $this->server_ttl > time()) ) {
477482
$url = ! empty($this->work_url) ? $this->work_url : $this->server_url;
478483
$result = $this->sendRequest($msg, $url, $this->server_timeout);
479484
} else {
480485
$result = false;
481486
}
482487

483488
// Changing server
484-
if ( true || ($result === false || $result->errno != 0) ) {
489+
if ($result === false || $result->errno != 0) {
485490
// Split server url to parts
486491
preg_match("@^(https?://)([^/:]+)(.*)@i", $this->server_url, $matches);
487492

@@ -490,7 +495,7 @@ private function httpRequest($msg)
490495
$url_suffix = isset($matches[3]) ? $matches[3] : '';
491496

492497
if ( empty($url_host) ) {
493-
return false;
498+
throw TransportException::fromUrlHostError($url_host);
494499
} elseif ( null !== $servers = $this->get_servers_ip($url_host) ) {
495500
// Loop until find work server
496501
foreach ( $servers as $server ) {
@@ -516,13 +521,13 @@ private function httpRequest($msg)
516521

517522
if ( ! empty($this->data_codepage) && $this->data_codepage !== 'UTF-8' ) {
518523
if ( ! empty($response->comment) ) {
519-
$response->comment = $this->stringFromUTF8($response->comment, $this->data_codepage);
524+
$response->comment = CleantalkHelper::stringFromUTF8($response->comment, $this->data_codepage);
520525
}
521526
if ( ! empty($response->errstr) ) {
522-
$response->errstr = $this->stringFromUTF8($response->errstr, $this->data_codepage);
527+
$response->errstr = CleantalkHelper::stringFromUTF8($response->errstr, $this->data_codepage);
523528
}
524529
if ( ! empty($response->sms_error_text) ) {
525-
$response->sms_error_text = $this->stringFromUTF8($response->sms_error_text, $this->data_codepage);
530+
$response->sms_error_text = CleantalkHelper::stringFromUTF8($response->sms_error_text, $this->data_codepage);
526531
}
527532
}
528533

@@ -534,7 +539,7 @@ private function httpRequest($msg)
534539
*
535540
* @param $host
536541
*
537-
* @return array
542+
* @return array|null
538543
*/
539544
private function get_servers_ip($host) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
540545
{
@@ -577,7 +582,7 @@ private function get_servers_ip($host) // phpcs:ignore PSR1.Methods.CamelCapsMet
577582
);
578583
// If records recieved
579584
} else {
580-
$tmp = null;
585+
$tmp = array();
581586
$fast_server_found = false;
582587

583588
foreach ( $servers as $server ) {
@@ -593,10 +598,8 @@ private function get_servers_ip($host) // phpcs:ignore PSR1.Methods.CamelCapsMet
593598
$fast_server_found = $ping < $this->min_server_timeout ? true : false;
594599
}
595600

596-
if ( count($tmp) ) {
597-
ksort($tmp);
598-
$response = $tmp;
599-
}
601+
ksort($tmp);
602+
$response = $tmp;
600603
}
601604

602605
return empty($response) ? null : $response;
@@ -605,7 +608,7 @@ private function get_servers_ip($host) // phpcs:ignore PSR1.Methods.CamelCapsMet
605608
/**
606609
* Function to check response time
607610
* param string
608-
* @return int
611+
* @return float
609612
*/
610613
private function httpPing($host)
611614
{

0 commit comments

Comments
 (0)