Blame view

vendor/phpunit/php-code-coverage/src/CodeCoverage/Driver/PHPDBG.php 2.78 KB
ad2e91f7   Mihail   move multyparser ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  <?php
  /*
   * This file is part of the PHP_CodeCoverage package.
   *
   * (c) Sebastian Bergmann <sebastian@phpunit.de>
   *
   * For the full copyright and license information, please view the LICENSE
   * file that was distributed with this source code.
   */
  
  /**
   * Driver for PHPDBG's code coverage functionality.
   *
   * @since Class available since Release 2.2.0
   * @codeCoverageIgnore
   */
  class PHP_CodeCoverage_Driver_PHPDBG implements PHP_CodeCoverage_Driver
  {
      /**
       * Constructor.
       */
      public function __construct()
      {
          if (PHP_SAPI !== 'phpdbg') {
              throw new PHP_CodeCoverage_Exception(
                  'This driver requires the PHPDBG SAPI'
              );
          }
  
          if (!function_exists('phpdbg_start_oplog')) {
              throw new PHP_CodeCoverage_Exception(
                  'This build of PHPDBG does not support code coverage'
              );
          }
      }
  
      /**
       * Start collection of code coverage information.
       */
      public function start()
      {
          phpdbg_start_oplog();
      }
  
      /**
       * Stop collection of code coverage information.
       *
       * @return array
       */
      public function stop()
      {
          static $fetchedLines = array();
  
          $dbgData = phpdbg_end_oplog();
  
          if ($fetchedLines == array()) {
              $sourceLines = phpdbg_get_executable();
          } else {
              $newFiles = array_diff(
                  get_included_files(),
                  array_keys($fetchedLines)
              );
  
              if ($newFiles) {
                  $sourceLines = phpdbg_get_executable(
                      array('files' => $newFiles)
                  );
              } else {
                  $sourceLines = array();
              }
          }
  
          foreach ($sourceLines as $file => $lines) {
              foreach ($lines as $lineNo => $numExecuted) {
                  $sourceLines[$file][$lineNo] = self::LINE_NOT_EXECUTED;
              }
          }
  
          $fetchedLines = array_merge($fetchedLines, $sourceLines);
  
          return $this->detectExecutedLines($fetchedLines, $dbgData);
      }
  
      /**
       * Convert phpdbg based data into the format CodeCoverage expects
       *
       * @param  array $sourceLines
       * @param  array $dbgData
       * @return array
       */
      private function detectExecutedLines(array $sourceLines, array $dbgData)
      {
          foreach ($dbgData as $file => $coveredLines) {
              foreach ($coveredLines as $lineNo => $numExecuted) {
                  // phpdbg also reports $lineNo=0 when e.g. exceptions get thrown.
                  // make sure we only mark lines executed which are actually executable.
                  if (isset($sourceLines[$file][$lineNo])) {
                      $sourceLines[$file][$lineNo] = self::LINE_EXECUTED;
                  }
              }
          }
  
          return $sourceLines;
      }
  }