Use str_starts_with, str_ends_with and str_contains instead of strpos (#5485)

This commit is contained in:
Alexey
2022-12-31 16:02:23 +03:00
committed by GitHub
parent 0d31b25fba
commit 172ce659b8
14 changed files with 35 additions and 35 deletions

View File

@ -24,8 +24,7 @@ declare(strict_types=1);
namespace pocketmine\plugin;
use function is_file;
use function strlen;
use function substr;
use function str_ends_with;
/**
* Handles different types of plugins
@ -36,8 +35,7 @@ class PharPluginLoader implements PluginLoader{
){}
public function canLoadPlugin(string $path) : bool{
$ext = ".phar";
return is_file($path) && substr($path, -strlen($ext)) === $ext;
return is_file($path) && str_ends_with($path, ".phar");
}
/**

View File

@ -41,8 +41,8 @@ use function file_exists;
use function fopen;
use function mkdir;
use function rtrim;
use function str_contains;
use function stream_copy_to_stream;
use function strpos;
use function strtolower;
use function trim;
use const DIRECTORY_SEPARATOR;
@ -145,7 +145,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{
$pluginCmds = [];
foreach(Utils::stringifyKeys($this->getDescription()->getCommands()) as $key => $data){
if(strpos($key, ":") !== false){
if(str_contains($key, ":")){
$this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_commandError($key, $this->getDescription()->getFullName(), ":")));
continue;
}
@ -161,7 +161,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{
$aliasList = [];
foreach($data->getAliases() as $alias){
if(strpos($alias, ":") !== false){
if(str_contains($alias, ":")){
$this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->getDescription()->getFullName(), ":")));
continue;
}

View File

@ -34,6 +34,7 @@ use function extension_loaded;
use function implode;
use function in_array;
use function phpversion;
use function str_starts_with;
use function stripos;
use function strlen;
use function substr;
@ -96,7 +97,7 @@ final class PluginLoadabilityChecker{
}
foreach(["<=", "le", "<>", "!=", "ne", "<", "lt", "==", "=", "eq", ">=", "ge", ">", "gt"] as $comparator){
// warning: the > character should be quoted in YAML
if(substr($constr, 0, strlen($comparator)) === $comparator){
if(str_starts_with($constr, $comparator)){
$version = substr($constr, strlen($comparator));
if(!version_compare($gotVersion, $version, $comparator)){
return KnownTranslationFactory::pocketmine_plugin_incompatibleExtensionVersion(extensionName: $extensionName, extensionVersion: $gotVersion, pluginRequirement: $constr);

View File

@ -62,7 +62,7 @@ use function mkdir;
use function realpath;
use function shuffle;
use function sprintf;
use function strpos;
use function str_contains;
use function strtolower;
/**
@ -296,7 +296,7 @@ class PluginManager{
continue;
}
if(strpos($name, " ") !== false){
if(str_contains($name, " ")){
$this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_spacesDiscouraged($name)));
}

View File

@ -28,9 +28,8 @@ use function count;
use function file;
use function implode;
use function is_file;
use function strlen;
use function strpos;
use function substr;
use function str_contains;
use function str_ends_with;
use const FILE_IGNORE_NEW_LINES;
use const FILE_SKIP_EMPTY_LINES;
@ -41,8 +40,7 @@ use const FILE_SKIP_EMPTY_LINES;
class ScriptPluginLoader implements PluginLoader{
public function canLoadPlugin(string $path) : bool{
$ext = ".php";
return is_file($path) && substr($path, -strlen($ext)) === $ext;
return is_file($path) && str_ends_with($path, ".php");
}
/**
@ -66,7 +64,7 @@ class ScriptPluginLoader implements PluginLoader{
$docCommentLines = [];
foreach($content as $line){
if(!$insideHeader){
if(strpos($line, "/**") !== false){
if(str_contains($line, "/**")){
$insideHeader = true;
}else{
continue;
@ -75,7 +73,7 @@ class ScriptPluginLoader implements PluginLoader{
$docCommentLines[] = $line;
if(strpos($line, "*/") !== false){
if(str_contains($line, "*/")){
break;
}
}