1

对齐还是不对齐...

以下方法的优缺点。

我喜欢使用第一种情况。因为我认为这是最佳的,但其他人却不这么认为。

  • 向文件添加新常量时 - diff 是 1 个字符串,而不是更多
  • 更清晰,更美丽
  • 链式调用 (-> -> -> ->) - 更清晰

你怎么看?可能有关于它的 PSR 吗?

案例风格1

class A {

    const AB = 1;
    const PAGE = 2;
    const PP_SOMETHING_VERY_LONG = 3;

    public function testLoooooooooong()
    {
        $a = 1;
        $somethingWeryLong = 2;
        $c = 3;
        $res = $a + $c;

        $b = 1;
        $n = 100;

        return 0;
    }

    public function test2($objectttttttttttttttLooooooong) 
    {
        $objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
            ->call(
                $this->testLoooooooooong(),
                $this->testLoooooooooong2(),
                $this->testLoooooooooong3()
            )
            ->call(
                $this->testLoooooooooong(),
                $this->testLoooooooooong2(),
                $this->testLoooooooooong3()
            );
    }

}

案例风格2

class A {

    const AB                     = 1;
    const PAGE                   = 2;
    const PP_SOMETHING_VERY_LONG = 3;

    public function testLoooooooooong() {
        $a                 = 1;
        $somethingWeryLong = 2;
        $c                 = 3;
        $res               = $a + $c;

        $b = 1;
        $n = 100;

        return 0;
    }

    public function test2($objectttttttttttttttLooooooong) {
        $objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
                                       ->call(
                                           $this->testLoooooooooong(),
                                           $this->testLoooooooooong2(),
                                           $this->testLoooooooooong3()
                                       )
                                       ->call(
                                           $this->testLoooooooooong(),
                                           $this->testLoooooooooong2(),
                                           $this->testLoooooooooong3()
                                       );
    }

}
4

1 回答 1

1

官方回答:有关于样式化PHP代码的PSR-2,你可以在这里找到它: http ://www.php-fig.org/psr/psr-2/

除了这些建议之外,完全取决于编码人员您喜欢如何设置代码样式。这只是关于约定。

在现代 IDE(IntelliJ、Netbeans、Eclipse、PHPStorm 等)中,您可以自动将代码重新格式化为您喜欢的方式(例如使用 ALT+L 之类的快捷方式)。

答案的固执部分:我更喜欢案例 1 的 = 样式和 -> 样式,因为处理案例 2 中的所有这些空格和制表符很烦人。

不过我更喜欢

public function testLoooooooooong() {

}

从案例 2 开始

public function testLoooooooooong() 
{

}

出于同样的原因,从案例 1 开始。我不想编写额外的选项卡,我认为它不会使代码更具可读性。无论如何,这都是一个口味问题,许多人也更喜欢这里的案例 1。

于 2017-12-29T14:11:05.757 回答