Просмотр исходного кода

refactor(core):优化代码实现与类型声明

- 使用 Attribute 类常量替代全限定类名
- 替换字符串插值写法提升可读性- 使用 str_starts_with 和 str_ends_with 替代 strpos 和 substr
-修正类常量类型声明,明确指定为字符串类型- 修复数组遍历时的键值处理逻辑- 更新 XML生成逻辑中的标签拼接方式
runphp 5 месяцев назад
Родитель
Сommit
78fa4b43fc
5 измененных файлов с 14 добавлено и 10 удалено
  1. 3 1
      src/Attribute/Cron.php
  2. 3 1
      src/Attribute/Hook.php
  3. 1 1
      src/Plugin.php
  4. 5 5
      src/Response/Xml.php
  5. 2 2
      src/Service/CoreService.php

+ 3 - 1
src/Attribute/Cron.php

@@ -3,7 +3,9 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Attribute;
 
-#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
+use Attribute;
+
+#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
 class Cron
 {
     public function __construct(

+ 3 - 1
src/Attribute/Hook.php

@@ -3,7 +3,9 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Attribute;
 
-#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
+use Attribute;
+
+#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
 class Hook
 {
     public function __construct(

+ 1 - 1
src/Plugin.php

@@ -13,7 +13,7 @@ use Composer\Script\ScriptEvents;
 
 class Plugin implements PluginInterface, EventSubscriberInterface
 {
-    public const EXTENSION_TYPE = 'sixshop-extension';
+    public const string EXTENSION_TYPE = 'sixshop-extension';
 
     public static array $installedSixShopExtensions = [];
 

+ 5 - 5
src/Response/Xml.php

@@ -10,20 +10,20 @@ class Xml extends \think\response\Xml
         if (is_array($attr)) {
             $array = [];
             foreach ($attr as $key => $value) {
-                $array[] = "{$key}=\"{$value}\"";
+                $array[] = "$key=\"$value\"";
             }
             $attr = implode(' ', $array);
         }
 
         $attr = trim($attr);
-        $attr = empty($attr) ? '' : " {$attr}";
-        $xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
+        $attr = empty($attr) ? '' : " $attr";
+        $xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>";
         if ($this->options['xslt']) {
             $xml .= '<?xml-stylesheet type="text/xsl" href="' . $this->options['xslt'] . '"?>';
         }
-        $xml .= "<{$root}{$attr}>";
+        $xml .= "<$root$attr>";
         $xml .= $this->dataToXml($data, $item, $id);
-        $xml .= "</{$root}>";
+        $xml .= "</$root>";
 
         return $xml;
 

+ 2 - 2
src/Service/CoreService.php

@@ -57,7 +57,7 @@ class CoreService extends Service
         }
         $files = array_diff(scandir($runtimePath), ['.', '..']);
         foreach ($files as $file) {
-            if (strpos($file, 'extension_') === 0 && substr($file, -4) === '.php') {
+            if (str_starts_with($file, 'extension_') && str_ends_with($file, '.php')) {
                 unlink($runtimePath . $file);
             }
         }
@@ -67,7 +67,7 @@ class CoreService extends Service
             $composer = $composerJson->read();
             $extensionID = $composer['extra']['sixshop']['id'] ?? $composer['name'];
             self::$extensionComposerMap[$extensionID] = $composer;
-        };
+        }
         $header = '// This file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL . 'declare (strict_types = 1);' . PHP_EOL;
         $content = '<?php ' . PHP_EOL . $header . "return " . var_export(self::$extensionComposerMap, true) . ';';
         file_put_contents($extensionComposerFile, $content);