Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Apr 15, 2024
1 parent 886993a commit b343265
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 92 deletions.
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"nikic/php-parser": "^5.0.2",
"revolt/event-loop": "^1.0.6",
"danog/async-orm": "^1.0.2",
"symfony/thanks": "^1.3"
"symfony/thanks": "^1.3",
"danog/telegram-entities": "^1.0"
},
"require-dev": {
"ext-ctype": "*",
Expand All @@ -72,7 +73,8 @@
"amphp/http-server": "^3.3",
"revolt/event-loop-adapter-react": "^1.1.1",
"dg/bypass-finals": "dev-master",
"brianium/paratest": "^6.11.1"
"brianium/paratest": "^6.11.1",
"vimeo/psalm": "dev-master"
},
"suggest": {
"ext-primemodule": "Install the primemodule and FFI extensions to speed up MadelineProto (https://prime.madelineproto.xyz)",
Expand Down Expand Up @@ -132,11 +134,6 @@
"post-install-cmd": ["@composer bin all install --ansi"],
"post-update-cmd": ["@composer bin all update --ansi"]
},
"extra": {
"phabel": {
"revision": 0
}
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
Expand Down
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from 116268 to 87f2e3
4 changes: 2 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
ignoreInternalFunctionNullReturn="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor-bin/check/vendor/vimeo/psalm/config.xsd"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
autoloader="vendor-bin/check/vendor/autoload.php"
autoloader="vendor/autoload.php"
>
<enableExtensions>
<extension name="ffi"/>
Expand Down
7 changes: 5 additions & 2 deletions src/BotApiFileId.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace danog\MadelineProto;

use AssertionError;
use danog\Decoder\FileId;
use danog\Decoder\FileIdType;
use danog\MadelineProto\EventHandler\Media;
Expand Down Expand Up @@ -54,15 +55,17 @@ public function __construct(
*/
public function getTypeClass(): string
{
return match (FileId::fromBotAPI($this->fileId)->type) {
$f = FileId::fromBotAPI($this->fileId);
return match ($f->type) {
FileIdType::PHOTO => Photo::class,
FileIdType::VOICE => Voice::class,
FileIdType::VIDEO => Video::class,
FileIdType::DOCUMENT => Document::class,
FileIdType::STICKER => AbstractSticker::class,
FileIdType::VIDEO_NOTE => RoundVideo::class,
FileIdType::AUDIO => Audio::class,
FileIdType::ANIMATION => Gif::class
FileIdType::ANIMATION => Gif::class,
default => throw new AssertionError("Cannot use bot API file ID of type ".$f->type->value)
};
}
}
2 changes: 1 addition & 1 deletion src/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
final class Conversion
{
/**
* Prepare API instance.
* Import authorization from raw auth key and DC id.
*
* @param array<int, string> $authorization Authorization info, DC ID => auth key
*/
Expand Down
2 changes: 1 addition & 1 deletion src/DataCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function getDNSClient(): DnsResolver
*
* @internal
*/
private static function normalizeBindToOption(string $bindTo = null): ?string
private static function normalizeBindToOption(?string $bindTo = null): ?string
{
if ($bindTo === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/DoHWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function loadConfig(): DnsConfig
*
* @return ConnectionContext[]
*/
public function generateContexts(string $uri, ConnectContext $context = null): array
public function generateContexts(string $uri, ?ConnectContext $context = null): array
{
$ctxs = [];
$combos = [
Expand Down
85 changes: 10 additions & 75 deletions src/StrTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
use danog\MadelineProto\TL\Conversion\DOMEntities;
use danog\MadelineProto\TL\Conversion\Extension;
use danog\MadelineProto\TL\Conversion\MarkdownEntities;
use danog\TelegramEntities\Entities;
use danog\TelegramEntities\EntityTools;
use Throwable;
use Webmozart\Assert\Assert;

/**
* Some tools.
Expand All @@ -55,15 +56,7 @@ abstract class StrTools extends Extension
*/
public static function mbStrlen(string $text): int
{
$length = 0;
$textlength = \strlen($text);
for ($x = 0; $x < $textlength; $x++) {
$char = \ord($text[$x]);
if (($char & 0xc0) != 0x80) {
$length += 1 + ($char >= 0xf0 ? 1 : 0);
}
}
return $length;
return EntityTools::mbStrlen($text);
}
/**
* Telegram UTF-8 multibyte substring.
Expand All @@ -74,15 +67,7 @@ public static function mbStrlen(string $text): int
*/
public static function mbSubstr(string $text, int $offset, ?int $length = null): string
{
return mb_convert_encoding(
substr(
mb_convert_encoding($text, 'UTF-16'),
$offset<<1,
$length === null ? null : ($length<<1),
),
'UTF-8',
'UTF-16',
);
return EntityTools::mbSubstr($text, $offset, $length);
}
/**
* Telegram UTF-8 multibyte split.
Expand All @@ -93,13 +78,7 @@ public static function mbSubstr(string $text, int $offset, ?int $length = null):
*/
public static function mbStrSplit(string $text, int $length): array
{
$result = [];
foreach (str_split(mb_convert_encoding($text, 'UTF-16'), $length<<1) as $chunk) {
$chunk = mb_convert_encoding($chunk, 'UTF-8', 'UTF-16');
Assert::string($chunk);
$result []= $chunk;
}
return $result;
return EntityTools::mbStrSplit($text, $length);
}
/**
* Manually convert HTML to a message and a set of entities.
Expand Down Expand Up @@ -222,7 +201,7 @@ public static function toSnakeCase(string $input): string
*/
public static function htmlEscape(string $what): string
{
return htmlspecialchars($what, ENT_QUOTES|ENT_SUBSTITUTE|ENT_XML1);
return EntityTools::htmlEscape($what);
}
/**
* Escape string for markdown.
Expand All @@ -231,51 +210,7 @@ public static function htmlEscape(string $what): string
*/
public static function markdownEscape(string $what): string
{
return str_replace(
[
'\\',
'_',
'*',
'[',
']',
'(',
')',
'~',
'`',
'>',
'#',
'+',
'-',
'=',
'|',
'{',
'}',
'.',
'!',
],
[
'\\\\',
'\\_',
'\\*',
'\\[',
'\\]',
'\\(',
'\\)',
'\\~',
'\\`',
'\\>',
'\\#',
'\\+',
'\\-',
'\\=',
'\\|',
'\\{',
'\\}',
'\\.',
'\\!',
],
$what
);
return EntityTools::markdownEscape($what);
}
/**
* Escape string for markdown codeblock.
Expand All @@ -284,7 +219,7 @@ public static function markdownEscape(string $what): string
*/
public static function markdownCodeblockEscape(string $what): string
{
return str_replace('```', '\\```', $what);
return EntityTools::markdownCodeblockEscape($what);
}
/**
* Escape string for markdown code section.
Expand All @@ -293,7 +228,7 @@ public static function markdownCodeblockEscape(string $what): string
*/
public static function markdownCodeEscape(string $what): string
{
return str_replace('`', '\\`', $what);
return EntityTools::markdownCodeEscape($what);
}
/**
* Escape string for URL.
Expand All @@ -302,7 +237,7 @@ public static function markdownCodeEscape(string $what): string
*/
public static function markdownUrlEscape(string $what): string
{
return str_replace(')', '\\)', $what);
return EntityTools::markdownUrlEscape($what);
}
/**
* Escape type name.
Expand Down
2 changes: 0 additions & 2 deletions vendor-bin/check/composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"require": {
"vimeo/psalm": "dev-master",
"ennexa/amp-update-cache": "dev-master",
"phpunit/phpunit": "^9",
"amphp/php-cs-fixer-config": "v2.x-dev",
"slevomat/coding-standard": "^8.7",
"squizlabs/php_codesniffer": "^3.7",
Expand Down

0 comments on commit b343265

Please sign in to comment.