Php 折射代码:私有变量

Php 折射代码:私有变量,php,Php,我必须对大量代码进行折射,我反复遇到了一些设计问题,我无法决定什么更好: class LintRequestDispatcher { private $_info; private $_build_log; private $_results; public function __construct($info, $build_log) { $this->_info = $info; $this->_build_log = $build_log;

我必须对大量代码进行折射,我反复遇到了一些设计问题,我无法决定什么更好:

class LintRequestDispatcher {

  private $_info;
  private $_build_log;
  private $_results;

  public function __construct($info, $build_log) {
    $this->_info = $info;
    $this->_build_log = $build_log;
  }

  public function processLog() {
    $file_contents = file_get_contents($this->_build_log);
    $lines = explode("\n", $file_contents);
    $errors = array();
    foreach ($lines as $line) {
      if (preg_match('/^.+=.+ in (.+) on line (\d+)$/', $line, $tokens)) {
        $errors[] = array('file' => $tokens[1] , 'line_number' => $tokens[2], 'message' => $tokens[0]);
      }
    }

    $arr = array('job_name' => $this->_info['job_name'], 'build_number' => $this->_info['build_number'], 'errors' => $errors);
    $this->_results = $arr;
  }

  public function post(Client $client) {
    $request = $client->createRequest(
        'POST',
        '/app.php/api/reports/lint',
        array('Content-Type' => 'application/json'),
        json_encode($this->_results)
    );

    $request->send();
  }
}
变量$info、$build\u log应该通过构造函数传入并存储为私有变量,并且可能有getter/setter方法来访问变量,还是应该只传入使用它的函数?在本例中,唯一使用此功能的函数是processLog。我觉得通过构造函数传递变量并存储变量是不必要的

processLog和post是否应合并为一个函数?processLog将结果存储在$\u results私有变量中,然后调用post来使用它。。但是单元测试可能很难

这些应该是值得担心的问题吗

如果我使用类作为容器将函数分组在一起,而不是使用它作为完整对象的定义,那么我更喜欢传入变量,而不是将它们存储为成员

如果您确信processLog的结果只会用作post的输入,那么可以将它们组合成一个函数

你是否应该担心这一点取决于其他几件事。你是团队的一员吗?此代码是否由其他人维护?您确定即使在多线程环境中,也会始终以正确的顺序调用代码吗?您将实例化一个LintRequestDispatcher类型的对象并重用它吗?这不是最好的方法,但是如果你想很快地把东西拼凑起来,我就不会担心了。如果这是某个更大项目的一部分,我建议将解析器拆分为自己的类

在我看来:

如果您想使用构造函数,可以使用下面的代码,因为变量只能用于特定的函数。或者,如果不想使用构造函数,只需将参数移动到processLog,而不需要$\u info和$\u build\u log。至于性能,我建议第二种

class LintRequestDispatcher {

    private $_info;
    private $_build_log;
    private $_results;

    public function __construct($info = null, $build_log = null) {
        $this->_info = $info;
        $this->_build_log = $build_log;
    }
}
根据1号的建议,最好将这两种功能结合起来。但是,如果您希望能够对这两个函数进行单元测试,只需将post函数放入processLog

我不认为这些是关键问题,但考虑在processLog上添加一些验证。您不知道输入是什么,可能是空字符串,也可能是错误的文件名/路径

public function processLog(Client $client, $info, $build_log) {
    $file_contents = file_get_contents($build_log);
    $lines = explode("\n", $file_contents);
    $errors = array();
    foreach ($lines as $line) {
        if (preg_match('/^.+=.+ in (.+) on line (\d+)$/', $line, $tokens)) {
            $errors[] = array('file' => $tokens[1] , 'line_number' => $tokens[2], 'message' => $tokens[0]);
        }
    }

    $arr = array('job_name' => $info['job_name'], 'build_number' => $info['build_number'], 'errors' => $errors);
    $this->post($client, $arr);
}

public function post(Client $client, $results) {
    $request = $client->createRequest(
        'POST',
        '/app.php/api/reports/lint',
        array('Content-Type' => 'application/json'),
        json_encode($results)
    );

   $request->send();
}