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

Simplify Conditional Logic by Refactoring Its Implementation

1. The current implementation is as follows:

PHP

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);
        }
    }


2. Referring to 10.1 Decompose Conditional, adjust it as follows:

PHP

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);
    }
}


3. 10.2 Consolidate Conditional Expression. Extracting the check condition into a separate function is very useful for clarifying the meaning of the code, because it replaces statements describing “what to do” with “why to do it”.

PHP



    /**
     * 对比两个「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. If a certain condition is extremely rare, you should check that condition separately and return from the function immediately when the condition is true. Such separate checks are often called “guard clauses”.

PHP

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);
    }
}


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系