Skip to content

Commit c3f2f27

Browse files
add cleancode check
fix MissingImport warning
1 parent 40b431c commit c3f2f27

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

phpmd.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727

2828
<rule ref="rulesets/design.xml" />
2929

30+
<rule ref="rulesets/cleancode.xml" />
3031
</ruleset>

src/CrashPad.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TelegramBot;
55

66
use Exception;
7+
use RuntimeException;
78
use Throwable;
89

910
/**
@@ -30,7 +31,7 @@ public static function enableCrashHandler(): void
3031
}
3132

3233
if (!defined('DEBUG_MODE')) {
33-
throw new \RuntimeException(
34+
throw new RuntimeException(
3435
'Something went wrong, Unfortunately, we can not handle this error.', 0, $throwable
3536
);
3637
}
@@ -72,7 +73,7 @@ public static function setDebugMode(int $admin_id = -1): void
7273
public static function sendCrash(int $chat_id, Exception|Throwable $exception, string|null $update = null): bool
7374
{
7475
if ($chat_id === -1) {
75-
throw new \RuntimeException(sprintf(
76+
throw new RuntimeException(sprintf(
7677
'The given `chat_id` is not valid. given: %s',
7778
$chat_id
7879
));
@@ -83,7 +84,7 @@ public static function sendCrash(int $chat_id, Exception|Throwable $exception, s
8384
}
8485

8586
if (($token = self::loadToken()) === null) {
86-
throw new \RuntimeException(
87+
throw new RuntimeException(
8788
'The token is not set. Please set the token using `Telegram::setToken()` method.'
8889
);
8990
}

src/Entities/Keyboard.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace TelegramBot\Entities;
55

6+
use ReflectionClass;
67
use TelegramBot\Entity;
78

89
/**
@@ -105,7 +106,7 @@ public function addRow(array $row): Keyboard
105106
*/
106107
public function getType(): string
107108
{
108-
$reflection = new \ReflectionClass(static::class);
109+
$reflection = new ReflectionClass(static::class);
109110

110111
$class_name = $reflection->getShortName();
111112

src/Entities/Response.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TelegramBot\Entities;
44

5+
use InvalidArgumentException;
56
use TelegramBot\Entity;
67

78
/**
@@ -47,7 +48,7 @@ public function __construct(array $data)
4748
if ($is_ok) {
4849
foreach ($this->requiredFields as $field) {
4950
if (!isset($data[$field])) {
50-
throw new \InvalidArgumentException("The field '{$field}' is required.");
51+
throw new InvalidArgumentException("The field '{$field}' is required.");
5152
}
5253
}
5354
}

src/Entity.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace TelegramBot;
55

6+
use BadMethodCallException;
7+
68
/**
79
* Entity class
810
*
@@ -169,7 +171,7 @@ public function __call(string $name, array $arguments): mixed {
169171
return $this;
170172
}
171173

172-
throw new \BadMethodCallException(`Method {$name} does not exist`);
174+
throw new BadMethodCallException(`Method {$name} does not exist`);
173175
}
174176

175177
}

src/UpdateHandler.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TelegramBot;
44

5+
use InvalidArgumentException;
6+
use RuntimeException;
57
use TelegramBot\Entities\Update;
68
use TelegramBot\Exception\InvalidBotTokenException;
79
use TelegramBot\Interfaces\HandlerInterface;
@@ -91,7 +93,7 @@ public function addPlugins(Plugin|array $plugins): UpdateHandler {
9193

9294
foreach ($plugins as $plugin) {
9395
if (!is_subclass_of($plugin, Plugin::class)) {
94-
throw new \RuntimeException(
96+
throw new RuntimeException(
9597
sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
9698
);
9799
}
@@ -138,10 +140,10 @@ private function isFiltered(Update $update): bool {
138140
} elseif (is_bool($value)) {
139141
return $value;
140142
}
141-
throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean');
143+
throw new InvalidArgumentException('The value of the filter must be a callable or a boolean');
142144
}
143145
}
144-
throw new \InvalidArgumentException('Invalid filter');
146+
throw new InvalidArgumentException('Invalid filter');
145147
}
146148

147149
return false;
@@ -169,7 +171,7 @@ public function resolve(Update|null $update = null, array $config = []): void {
169171
}
170172

171173
if (!method_exists($this, '__process')) {
172-
throw new \RuntimeException('The method __process does not exist');
174+
throw new RuntimeException('The method __process does not exist');
173175
}
174176

175177
if (is_array($config)) {
@@ -203,15 +205,15 @@ private function loadPlugins(array $plugins): void {
203205

204206
foreach ($plugins as $plugin) {
205207
if (!is_subclass_of($plugin['class'], Plugin::class)) {
206-
throw new \InvalidArgumentException(sprintf(
208+
throw new InvalidArgumentException(sprintf(
207209
'The plugin %s must be an instance of %s',
208210
get_class($plugin['class']), Plugin::class
209211
));
210212
}
211213
}
212214

213215
if (!$update instanceof Update) {
214-
throw new \InvalidArgumentException(sprintf(
216+
throw new InvalidArgumentException(sprintf(
215217
'The update must be an instance of %s. %s given',
216218
Update::class, gettype($update)
217219
));

src/Util/Toolkit.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace TelegramBot\Util;
44

5+
use ReflectionClass;
6+
use ReflectionException;
7+
use RuntimeException;
8+
59
/**
610
* Toolkit class
711
*
@@ -75,15 +79,15 @@ public static function isJson(?string $string): bool
7579
* Ignored reflection class
7680
*
7781
* @param class-string|object $reflectionClass
78-
* @return \ReflectionClass
82+
* @return ReflectionClass
7983
*/
80-
public static function reflectionClass(string|object $reflectionClass): \ReflectionClass
84+
public static function reflectionClass(string|object $reflectionClass): ReflectionClass
8185
{
8286
try {
83-
return new \ReflectionClass($reflectionClass);
84-
} catch (\ReflectionException $e) {
85-
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
87+
return new ReflectionClass($reflectionClass);
88+
} catch (ReflectionException $e) {
89+
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
8690
}
8791
}
8892

89-
}
93+
}

0 commit comments

Comments
 (0)