小编Mar*_*ech的帖子

重构PHP类

我正在玩重构PHP类.我的原始类有一个构造函数和一个大型方法,它通过各种操作运行字符串并在结尾处吐出结果.

class String
{
    public function __contstruct()
    {
        // a couple of initialisation things
    }

    public function make($string)
    {
        // very large method that does lots of different types of filtering to an input string
    }
}
Run Code Online (Sandbox Code Playgroud)

我的类通过scrutinizer-ci运行,其中一个建议是使用Composer方法模式将大方法重构为较小的自包含方法.

https://scrutinizer-ci.com/docs/refactorings/compose-method

我做了这个,看起来更整洁,但这是我的问题/查询......

我的新课程现在看起来像这样

class String
{
    public function __construct()
    {
        // a couple of initialisation things
    }

    public function make($string)
    {
        $string = $this->smallMethodOne($string);
        $string = $this->smallMethodTwo($string);
        $string = $this->smallMethodThree($string);
        $string = $this->smallMethodFour($string);
        $string = $this->smallMethodFive($string);
    }

    private function smallMethodOne($string) {
        // …
Run Code Online (Sandbox Code Playgroud)

php methods refactoring class

3
推荐指数
1
解决办法
574
查看次数

标签 统计

class ×1

methods ×1

php ×1

refactoring ×1