没有不值得去解决的问题,也没有不值得去学习的技术!

基于 重构 简化条件逻辑 重构条件代码实现

作者:

,
1、现有的实现如下
<pre class="wp-block-syntaxhighlighter-code">

<?php

namespace Modules\ThemeStoreDB\ThemeSetting;

use Illuminate\Support\Str;

class ThemeVersion
{
    const SPECIAL_IDENTIFIER = '-h.'; // 特殊版本标识
    const HYPHEN = '-'; // 连字符

    public string $semantic;

    function __construct(string $semantic)
    {
        $this->semantic = $semantic;
    }

    /**
     * 对比两个「PHP 规范化」的版本数字字符串,添加例外:-h.
     * @param self $version
     * @return int 在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数
     */
    public function compare(self $version): int
    {
        if (Str::contains($this->semantic, self::SPECIAL_IDENTIFIER) && !Str::contains($version->semantic, self::SPECIAL_IDENTIFIER) && Str::before($this->semantic, self::HYPHEN) == Str::before($version->semantic, self::HYPHEN)) {
            return 1;
        } elseif (!Str::contains($this->semantic, self::SPECIAL_IDENTIFIER) && Str::contains($version->semantic, self::SPECIAL_IDENTIFIER) && Str::before($this->semantic, self::HYPHEN) == Str::before($version->semantic, self::HYPHEN)) {
            return -1;
        } else {
            return version_compare($this->semantic, $version->semantic);
        }
    }


</pre>
2、参考 10.1 分解条件表达式(Decompose Conditional),调整为如下
<pre class="wp-block-syntaxhighlighter-code">

<?php

namespace Modules\ThemeStoreDB\ThemeSetting;

use Illuminate\Support\Str;

class ThemeVersion
{
    const SPECIAL_IDENTIFIER = '-h.'; // 特殊版本标识
    const HYPHEN = '-'; // 连字符

    public string $semantic;

    function __construct(string $semantic)
    {
        $this->semantic = $semantic;
    }

    /**
     * 对比两个「PHP 规范化」的版本数字字符串,添加例外:-h.
     * @param self $version
     * @return int 在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数
     */
    public function compare(self $version): int
    {
        if (self::isSpecial($this->semantic) && !self::isSpecial($version->semantic) && self::isMainEqual($this->semantic, $version->semantic)) {
            return 1;
        } elseif (!self::isSpecial($this->semantic) && self::isSpecial($version->semantic) && self::isMainEqual($this->semantic, $version->semantic)) {
            return -1;
        } else {
            return version_compare($this->semantic, $version->semantic);
        }
    }

    /**
     * 返回版本是否是特殊版本
     * @param string $semantic
     * @return bool
     */
    private static function isSpecial(string $semantic): bool
    {
        return Str::contains($semantic, self::SPECIAL_IDENTIFIER);
    }

    /**
     * 返回两个版本的主版本号是否相等
     * @param string $semantic1
     * @param string $semantic2
     * @return bool
     */
    private static function isMainEqual(string $semantic1, string $semantic2): bool
    {
        return self::getMain($semantic1) == self::getMain($semantic2);
    }

    /**
     * 获取版本的主版本号
     * @param string $semantic
     * @return string
     */
    private static function getMain(string $semantic): string
    {
        return Str::before($semantic, self::HYPHEN);
    }
}


</pre>
3、10.2 合并条件表达式(Consolidate Conditional Expression)。将检查条件提炼成一个独立的函数对于厘清代码意义非常有用,因为它把描述“做什么”的语句换成了“为什么这样做”。




    /**
     * 对比两个「PHP 规范化」的版本数字字符串,添加例外:-h.
     * @param self $version
     * @return int 在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数
     */
    public function compare(self $version): int
    {
        if (self::isSpecialGt($this->semantic, $version->semantic)) {
            return 1;
        } elseif (self::isSpecialLt($this->semantic, $version->semantic)) {
            return -1;
        } else {
            return version_compare($this->semantic, $version->semantic);
        }
    }

    /**
     * 当两个版本中仅存在一个特殊版本时,第一个版本大于第二个版本
     * @param string $semantic1
     * @param string $semantic2
     * @return bool
     */
    private static function isSpecialGt(string $semantic1, string $semantic2): bool
    {
        return self::isSpecial($semantic1) && !self::isSpecial($semantic2) && self::isMainEqual($semantic1, $semantic2);
    }

    /**
     * 当两个版本中仅存在一个特殊版本时,第一个版本小于第二个版本
     * @param string $semantic1
     * @param string $semantic2
     * @return bool
     */
    private static function isSpecialLt(string $semantic1, string $semantic2): bool
    {
        return !self::isSpecial($semantic1) && self::isSpecial($semantic2) && self::isMainEqual($semantic1, $semantic2);
    }
}



4、10.3 以卫语句取代嵌套条件表达式(Replace Nested Conditional with Guard Clauses)。如果某个条件极其罕见,就应该单独检查该条件,并在该条件为真时立刻从函数中返回。这样的单独检查常常被称为“卫语句”(guard clauses)。
<pre class="wp-block-syntaxhighlighter-code">

<?php

namespace Modules\ThemeStoreDB\ThemeSetting;

use Illuminate\Support\Str;

class ThemeVersion
{
    const SPECIAL_IDENTIFIER = '-h.'; // 特殊版本标识
    const HYPHEN = '-'; // 连字符

    public string $semantic;

    function __construct(string $semantic)
    {
        $this->semantic = $semantic;
    }

    /**
     * 对比两个「PHP 规范化」的版本数字字符串,添加例外:-h.
     * @param self $version
     * @return int 在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数
     */
    public function compare(self $version): int
    {
        if (self::isSpecialGt($this->semantic, $version->semantic)) {
            return 1;
        }
        if (self::isSpecialLt($this->semantic, $version->semantic)) {
            return -1;
        }

        return version_compare($this->semantic, $version->semantic);
    }

    /**
     * 返回版本是否是特殊版本
     * @param string $semantic
     * @return bool
     */
    private static function isSpecial(string $semantic): bool
    {
        return Str::contains($semantic, self::SPECIAL_IDENTIFIER);
    }

    /**
     * 返回两个版本的主版本号是否相等
     * @param string $semantic1
     * @param string $semantic2
     * @return bool
     */
    private static function isMainEqual(string $semantic1, string $semantic2): bool
    {
        return self::getMain($semantic1) == self::getMain($semantic2);
    }

    /**
     * 获取版本的主版本号
     * @param string $semantic
     * @return string
     */
    private static function getMain(string $semantic): string
    {
        return Str::before($semantic, self::HYPHEN);
    }

    /**
     * 当两个版本中仅存在一个特殊版本时,第一个版本大于第二个版本
     * @param string $semantic1
     * @param string $semantic2
     * @return bool
     */
    private static function isSpecialGt(string $semantic1, string $semantic2): bool
    {
        return self::isSpecial($semantic1) && !self::isSpecial($semantic2) && self::isMainEqual($semantic1, $semantic2);
    }

    /**
     * 当两个版本中仅存在一个特殊版本时,第一个版本小于第二个版本
     * @param string $semantic1
     * @param string $semantic2
     * @return bool
     */
    private static function isSpecialLt(string $semantic1, string $semantic2): bool
    {
        return !self::isSpecial($semantic1) && self::isSpecial($semantic2) && self::isMainEqual($semantic1, $semantic2);
    }
}


</pre>

需要长期技术维护或远程问题排查?

我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理