[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/lime/ -> lime.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@gmail.com>
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Unit test library.
  13   *
  14   * @package    lime
  15   * @author     Fabien Potencier <fabien.potencier@gmail.com>
  16   * @version    SVN: $Id: lime.php 3298 2007-01-17 06:08:15Z fabien $
  17   */
  18  
  19  class lime_test
  20  {
  21    public $plan = null;
  22    public $test_nb = 0;
  23    public $failed = 0;
  24    public $passed = 0;
  25    public $skipped = 0;
  26    public $output = null;
  27  
  28    function __construct($plan = null, $output_instance = null)
  29    {
  30      $this->plan = $plan;
  31      $this->output = $output_instance ? $output_instance : new lime_output();
  32  
  33      null !== $this->plan and $this->output->echoln(sprintf("1..%d", $this->plan));
  34    }
  35  
  36    function __destruct()
  37    {
  38      $total = $this->passed + $this->failed + $this->skipped;
  39  
  40      null === $this->plan and $this->plan = $total and $this->output->echoln(sprintf("1..%d", $this->plan));
  41  
  42      if ($total > $this->plan)
  43      {
  44        $this->output->diag(sprintf("Looks like you planned %d tests but ran %d extra.", $this->plan, $total - $this->plan));
  45      }
  46      elseif ($total < $this->plan)
  47      {
  48        $this->output->diag(sprintf("Looks like you planned %d tests but only ran %d.", $this->plan, $total));
  49      }
  50  
  51      if ($this->failed)
  52      {
  53        $this->output->diag(sprintf("Looks like you failed %d tests of %d.", $this->failed, $this->plan));
  54      }
  55  
  56      flush();
  57    }
  58  
  59    function ok($exp, $message = '')
  60    {
  61      if ($result = (boolean) $exp)
  62      {
  63        ++$this->passed;
  64      }
  65      else
  66      {
  67        ++$this->failed;
  68      }
  69      $this->output->echoln(sprintf("%s %d%s", $result ? 'ok' : 'not ok', ++$this->test_nb, $message = $message ? sprintf('%s %s', 0 === strpos($message, '#') ? '' : ' -', $message) : ''));
  70  
  71      if (!$result)
  72      {
  73        $traces = debug_backtrace();
  74        if ($_SERVER['PHP_SELF'])
  75        {
  76          $i = strstr($traces[0]['file'], $_SERVER['PHP_SELF']) ? 0 : 1;
  77        }
  78        else
  79        {
  80          $i = 0;
  81        }
  82        $this->output->diag(sprintf('    Failed test (%s at line %d)', str_replace(getcwd(), '.', $traces[$i]['file']), $traces[$i]['line']));
  83      }
  84  
  85      return $result;
  86    }
  87  
  88    function is($exp1, $exp2, $message = '')
  89    {
  90      if (is_object($exp1) || is_object($exp2))
  91      {
  92        $value = $exp1 === $exp2;
  93      }
  94      else
  95      {
  96        $value = $exp1 == $exp2;
  97      }
  98  
  99      if (!$result = $this->ok($value, $message))
 100      {
 101        $this->output->diag(sprintf("           got: %s", str_replace("\n", '', var_export($exp1, true))), sprintf("      expected: %s", str_replace("\n", '', var_export($exp2, true))));
 102      }
 103  
 104      return $result;
 105    }
 106  
 107    function isnt($exp1, $exp2, $message = '')
 108    {
 109      if (!$result = $this->ok($exp1 != $exp2, $message))
 110      {
 111        $this->output->diag(sprintf("      %s", str_replace("\n", '', var_export($exp1, true))), '          ne', sprintf("      %s", str_replace("\n", '', var_export($exp2, true))));
 112      }
 113  
 114      return $result;
 115    }
 116  
 117    function like($exp, $regex, $message = '')
 118    {
 119      if (!$result = $this->ok(preg_match($regex, $exp), $message))
 120      {
 121        $this->output->diag(sprintf("                    '%s'", $exp), sprintf("      doesn't match '%s'", $regex));
 122      }
 123  
 124      return $result;
 125    }
 126  
 127    function unlike($exp, $regex, $message = '')
 128    {
 129      if (!$result = $this->ok(!preg_match($regex, $exp), $message))
 130      {
 131        $this->output->diag(sprintf("               '%s'", $exp), sprintf("      matches '%s'", $regex));
 132      }
 133  
 134      return $result;
 135    }
 136  
 137    function cmp_ok($exp1, $op, $exp2, $message = '')
 138    {
 139      eval(sprintf("\$result = \$exp1 $op \$exp2;"));
 140      if (!$this->ok($result, $message))
 141      {
 142        $this->output->diag(sprintf("      %s", str_replace("\n", '', var_export($exp1, true))), sprintf("          %s", $op), sprintf("      %s", str_replace("\n", '', var_export($exp2, true))));
 143      }
 144  
 145      return $result;
 146    }
 147  
 148    function can_ok($object, $methods, $message = '')
 149    {
 150      $result = true;
 151      $failed_messages = array();
 152      foreach ((array) $methods as $method)
 153      {
 154        if (!method_exists($object, $method))
 155        {
 156          $failed_messages[] = sprintf("      method '%s' does not exist", $method);
 157          $result = false;
 158        }
 159      }
 160  
 161      !$this->ok($result, $message);
 162  
 163      !$result and $this->output->diag($failed_messages);
 164  
 165      return $result;
 166    }
 167  
 168    function isa_ok($var, $class, $message = '')
 169    {
 170      $type = is_object($var) ? get_class($var) : gettype($var);
 171      if (!$result = $this->ok($type == $class, $message))
 172      {
 173        $this->output->diag(sprintf("      isa_ok isn't a '%s' it's a '%s'", $class, $type));
 174      }
 175  
 176      return $result;
 177    }
 178  
 179    function is_deeply($exp1, $exp2, $message = '')
 180    {
 181      if (!$result = $this->ok($this->test_is_deeply($exp1, $exp2), $message))
 182      {
 183        $this->output->diag(sprintf("           got: %s", str_replace("\n", '', var_export($exp1, true))), sprintf("      expected: %s", str_replace("\n", '', var_export($exp2, true))));
 184      }
 185  
 186      return $result;
 187    }
 188  
 189    function pass($message = '')
 190    {
 191      return $this->ok(true, $message);
 192    }
 193  
 194    function fail($message = '')
 195    {
 196      return $this->ok(false, $message);
 197    }
 198  
 199    function diag($message)
 200    {
 201      $this->output->diag($message);
 202    }
 203  
 204    function skip($message = '', $nb_tests = 1)
 205    {
 206      for ($i = 0; $i < $nb_tests; $i++)
 207      {
 208        ++$this->skipped and --$this->passed;
 209        $this->pass(sprintf("# SKIP%s", $message ? ' '.$message : ''));
 210      }
 211    }
 212  
 213    function todo($message = '')
 214    {
 215      ++$this->skipped and --$this->passed;
 216      $this->pass(sprintf("# TODO%s", $message ? ' '.$message : ''));
 217    }
 218  
 219    function include_ok($file, $message = '')
 220    {
 221      if (!$result = $this->ok((@include($file)) == 1, $message))
 222      {
 223        $this->output->diag(sprintf("      Tried to include '%s'", $file));
 224      }
 225  
 226      return $result;
 227    }
 228  
 229    private function test_is_deeply($var1, $var2)
 230    {
 231      if (gettype($var1) != gettype($var2))
 232      {
 233        return false;
 234      }
 235  
 236      if (is_array($var1))
 237      {
 238        ksort($var1);
 239        ksort($var2);
 240        if (array_diff(array_keys($var1), array_keys($var2)))
 241        {
 242          return false;
 243        }
 244        $is_equal = true;
 245        foreach ($var1 as $key => $value)
 246        {
 247          $is_equal = $this->test_is_deeply($var1[$key], $var2[$key]);
 248          if ($is_equal === false)
 249          {
 250            break;
 251          }
 252        }
 253  
 254        return $is_equal;
 255      }
 256      else
 257      {
 258        return $var1 === $var2;
 259      }
 260    }
 261  
 262    function comment($message)
 263    {
 264      $this->output->comment($message);
 265    }
 266  
 267    static function get_temp_directory()
 268    {
 269      if ('\\' == DIRECTORY_SEPARATOR)
 270      {
 271        foreach (array('TEMP', 'TMP', 'windir') as $dir)
 272        {
 273          if ($var = isset($_ENV[$dir]) ? $_ENV[$dir] : getenv($dir))
 274          {
 275            return $var;
 276          }
 277        }
 278  
 279        return getenv('SystemRoot').'\temp';
 280      }
 281  
 282      if ($var = isset($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR'))
 283      {
 284        return $var;
 285      }
 286  
 287      return '/tmp';
 288    }
 289  }
 290  
 291  class lime_output
 292  {
 293    function diag()
 294    {
 295      $messages = func_get_args();
 296      foreach ($messages as $message)
 297      {
 298        array_map(array($this, 'comment'), (array) $message);
 299      }
 300    }
 301  
 302    function comment($message)
 303    {
 304      echo "# $message\n";
 305    }
 306  
 307    function echoln($message)
 308    {
 309      echo "$message\n";
 310    }
 311  }
 312  
 313  class lime_output_color extends lime_output
 314  {
 315    public $colorizer = null;
 316  
 317    function __construct()
 318    {
 319      $this->colorizer = new lime_colorizer();
 320    }
 321  
 322    function diag()
 323    {
 324      $messages = func_get_args();
 325      foreach ($messages as $message)
 326      {
 327        echo $this->colorizer->colorize('# '.join("\n# ", (array) $message), 'COMMENT')."\n";
 328      }
 329    }
 330  
 331    function comment($message)
 332    {
 333      echo $this->colorizer->colorize(sprintf('# %s', $message), 'COMMENT')."\n";
 334    }
 335  
 336    function echoln($message, $colorizer_parameter = null)
 337    {
 338      $message = preg_replace('/(?:^|\.)((?:not ok|dubious) *\d*)\b/e', '$this->colorizer->colorize(\'$1\', \'ERROR\')', $message);
 339      $message = preg_replace('/(?:^|\.)(ok *\d*)\b/e', '$this->colorizer->colorize(\'$1\', \'INFO\')', $message);
 340      $message = preg_replace('/"(.+?)"/e', '$this->colorizer->colorize(\'$1\', \'PARAMETER\')', $message);
 341      $message = preg_replace('/(\->|\:\:)?([a-zA-Z0-9_]+?)\(\)/e', '$this->colorizer->colorize(\'$1$2()\', \'PARAMETER\')', $message);
 342  
 343      echo ($colorizer_parameter ? $this->colorizer->colorize($message, $colorizer_parameter) : $message)."\n";
 344    }
 345  }
 346  
 347  class lime_colorizer
 348  {
 349    static public $styles = array();
 350  
 351    static function style($name, $options = array())
 352    {
 353      self::$styles[$name] = $options;
 354    }
 355  
 356    static function colorize($text = '', $parameters = array())
 357    {
 358      // disable colors if not supported (windows or non tty console)
 359      if (DIRECTORY_SEPARATOR == '\\' || !function_exists('posix_isatty') || !@posix_isatty(STDOUT))
 360      {
 361        return $text;
 362      }
 363  
 364      static $options    = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8);
 365      static $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37);
 366      static $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
 367  
 368      !is_array($parameters) && isset(self::$styles[$parameters]) and $parameters = self::$styles[$parameters];
 369  
 370      $codes = array();
 371      isset($parameters['fg']) and $codes[] = $foreground[$parameters['fg']];
 372      isset($parameters['bg']) and $codes[] = $background[$parameters['bg']];
 373      foreach ($options as $option => $value)
 374      {
 375        isset($parameters[$option]) && $parameters[$option] and $codes[] = $value;
 376      }
 377  
 378      return "\033[".implode(';', $codes).'m'.$text."\033[0m";
 379    }
 380  }
 381  
 382  lime_colorizer::style('ERROR', array('bg' => 'red', 'fg' => 'white', 'bold' => true));
 383  lime_colorizer::style('INFO',  array('fg' => 'green', 'bold' => true));
 384  lime_colorizer::style('PARAMETER', array('fg' => 'cyan'));
 385  lime_colorizer::style('COMMENT',  array('fg' => 'yellow'));
 386  
 387  class lime_harness extends lime_registration
 388  {
 389    public $php_cli = '';
 390    public $stats = array();
 391    public $output = null;
 392  
 393    function __construct($output_instance, $php_cli = null)
 394    {
 395      $this->php_cli = null === $php_cli ? PHP_BINDIR.DIRECTORY_SEPARATOR.'php' : $php_cli;
 396      if (!is_executable($this->php_cli))
 397      {
 398        $this->php_cli = $this->find_php_cli();
 399      }
 400  
 401      $this->output = $output_instance ? $output_instance : new lime_output();
 402    }
 403  
 404    protected function find_php_cli()
 405    {
 406      $path = getenv('PATH') ? getenv('PATH') : getenv('Path');
 407      $exe_suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array('');
 408      foreach (array('php5', 'php') as $php_cli)
 409      {
 410        foreach ($exe_suffixes as $suffix)
 411        {
 412          foreach (explode(PATH_SEPARATOR, $path) as $dir)
 413          {
 414            $file = $dir.DIRECTORY_SEPARATOR.$php_cli.$suffix;
 415            if (is_executable($file))
 416            {
 417              return $file;
 418            }
 419          }
 420        }
 421      }
 422  
 423      throw new Exception("Unable to find PHP executable.");
 424    }
 425  
 426    function run()
 427    {
 428      if (!count($this->files))
 429      {
 430        throw new Exception('You must register some test files before running them!');
 431      }
 432  
 433      // sort the files to be able to predict the order
 434      sort($this->files);
 435  
 436      $this->stats =array(
 437        '_failed_files' => array(),
 438        '_failed_tests' => 0,
 439        '_nb_tests'     => 0,
 440      );
 441  
 442      foreach ($this->files as $file)
 443      {
 444        $this->stats[$file] = array(
 445          'plan'     =>   null,
 446          'nb_tests' => 0,
 447          'failed'   => array(),
 448          'passed'   => array(),
 449        );
 450        $this->current_file = $file;
 451        $this->current_test = 0;
 452        $relative_file = $this->get_relative_file($file);
 453  
 454        ob_start(array($this, 'process_test_output'));
 455        passthru(sprintf('%s -d html_errors=off -d open_basedir= -q "%s" 2>&1', $this->php_cli, $file), $return);
 456        ob_end_clean();
 457  
 458        if ($return > 0)
 459        {
 460          $this->stats[$file]['status'] = 'dubious';
 461          $this->stats[$file]['status_code'] = $return;
 462        }
 463        else
 464        {
 465          $delta = $this->stats[$file]['plan'] - $this->stats[$file]['nb_tests'];
 466          if ($delta > 0)
 467          {
 468            $this->output->echoln(sprintf('%s%s%s', substr($relative_file, -67), str_repeat('.', 70 - min(67, strlen($relative_file))), $this->output->colorizer->colorize(sprintf('# Looks like you planned %d tests but only ran %d.', $this->stats[$file]['plan'], $this->stats[$file]['nb_tests']), 'COMMENT')));
 469            $this->stats[$file]['status'] = 'dubious';
 470            $this->stats[$file]['status_code'] = 255;
 471            $this->stats['_nb_tests'] += $delta;
 472            for ($i = 1; $i <= $delta; $i++)
 473            {
 474              $this->stats[$file]['failed'][] = $this->stats[$file]['nb_tests'] + $i;
 475            }
 476          }
 477          else if ($delta < 0)
 478          {
 479            $this->output->echoln(sprintf('%s%s%s', substr($relative_file, -67), str_repeat('.', 70 - min(67, strlen($relative_file))), $this->output->colorizer->colorize(sprintf('# Looks like you planned %s test but ran %s extra.', $this->stats[$file]['plan'], $this->stats[$file]['nb_tests'] - $this->stats[$file]['plan']), 'COMMENT')));
 480            $this->stats[$file]['status'] = 'dubious';
 481            $this->stats[$file]['status_code'] = 255;
 482            for ($i = 1; $i <= -$delta; $i++)
 483            {
 484              $this->stats[$file]['failed'][] = $this->stats[$file]['plan'] + $i;
 485            }
 486          }
 487          else
 488          {
 489            $this->stats[$file]['status_code'] = 0;
 490            $this->stats[$file]['status'] = $this->stats[$file]['failed'] ? 'not ok' : 'ok';
 491          }
 492        }
 493  
 494        $this->output->echoln(sprintf('%s%s%s', substr($relative_file, -67), str_repeat('.', 70 - min(67, strlen($relative_file))), $this->stats[$file]['status']));
 495        if (($nb = count($this->stats[$file]['failed'])) || $return > 0)
 496        {
 497          if ($nb)
 498          {
 499            $this->output->echoln(sprintf("    Failed tests: %s", implode(', ', $this->stats[$file]['failed'])));
 500          }
 501          $this->stats['_failed_files'][] = $file;
 502          $this->stats['_failed_tests']  += $nb;
 503        }
 504  
 505        if ('dubious' == $this->stats[$file]['status'])
 506        {
 507          $this->output->echoln(sprintf('    Test returned status %s', $this->stats[$file]['status_code']));
 508        }
 509      }
 510  
 511      if (count($this->stats['_failed_files']))
 512      {
 513        $format = "%-30s  %4s  %5s  %5s  %s";
 514        $this->output->echoln(sprintf($format, 'Failed Test', 'Stat', 'Total', 'Fail', 'List of Failed'));
 515        $this->output->echoln("------------------------------------------------------------------");
 516        foreach ($this->stats as $file => $file_stat)
 517        {
 518          if (!in_array($file, $this->stats['_failed_files'])) continue;
 519  
 520          $this->output->echoln(sprintf($format, substr($this->get_relative_file($file), -30), $file_stat['status_code'], count($file_stat['failed']) + count($file_stat['passed']), count($file_stat['failed']), implode(' ', $file_stat['failed'])));
 521        }
 522  
 523        $this->output->echoln(sprintf('Failed %d/%d test scripts, %.2f%% okay. %d/%d subtests failed, %.2f%% okay.',
 524          $nb_failed_files = count($this->stats['_failed_files']),
 525          $nb_files = count($this->files),
 526          ($nb_files - $nb_failed_files) * 100 / $nb_files,
 527          $nb_failed_tests = $this->stats['_failed_tests'],
 528          $nb_tests = $this->stats['_nb_tests'],
 529          $nb_tests > 0 ? ($nb_tests - $nb_failed_tests) * 100 / $nb_tests : 0
 530        ), 'ERROR');
 531      }
 532      else
 533      {
 534        $this->output->echoln('All tests successful.', 'INFO');
 535        $this->output->echoln(sprintf('Files=%d, Tests=%d', count($this->files), $this->stats['_nb_tests']), 'INFO');
 536      }
 537  
 538      return $this->stats['_failed_tests'] ? false : true;
 539    }
 540  
 541    private function process_test_output($lines)
 542    {
 543      foreach (explode("\n", $lines) as $text)
 544      {
 545        if (false !== strpos($text, 'not ok '))
 546        {
 547          ++$this->current_test;
 548          $test_number = (int) substr($text, 7);
 549          $this->stats[$this->current_file]['failed'][] = $test_number;
 550  
 551          ++$this->stats[$this->current_file]['nb_tests'];
 552          ++$this->stats['_nb_tests'];
 553        }
 554        else if (false !== strpos($text, 'ok '))
 555        {
 556          ++$this->stats[$this->current_file]['nb_tests'];
 557          ++$this->stats['_nb_tests'];
 558        }
 559        else if (preg_match('/^1\.\.(\d+)/', $text, $match))
 560        {
 561          $this->stats[$this->current_file]['plan'] = $match[1];
 562        }
 563      }
 564  
 565      return;
 566    }
 567  }
 568  
 569  class lime_coverage extends lime_registration
 570  {
 571    public $files = array();
 572    public $extension = '.php';
 573    public $base_dir = '';
 574    public $harness = null;
 575    public $verbose = false;
 576  
 577    function __construct($harness)
 578    {
 579      $this->harness = $harness;
 580    }
 581  
 582    function run()
 583    {
 584      if (!function_exists('xdebug_start_code_coverage'))
 585      {
 586        throw new Exception('You must install and enable xdebug before using lime coverage.');
 587      }
 588  
 589      if (!count($this->harness->files))
 590      {
 591        throw new Exception('You must register some test files before running coverage!');
 592      }
 593  
 594      if (!count($this->files))
 595      {
 596        throw new Exception('You must register some files to cover!');
 597      }
 598  
 599      $coverage = array();
 600      $tmp_file = lime_test::get_temp_directory().DIRECTORY_SEPARATOR.'test.php';
 601      foreach ($this->harness->files as $file)
 602      {
 603        $tmp = <<<EOF
 604  <?php
 605  xdebug_start_code_coverage();
 606  ob_start();
 607  include('$file');
 608  ob_end_clean();
 609  echo '<PHP_SER>'.serialize(xdebug_get_code_coverage()).'</PHP_SER>';
 610  EOF;
 611        file_put_contents($tmp_file, $tmp);
 612        ob_start();
 613        passthru(sprintf('%s -d html_errors=off -d open_basedir= -q "%s" 2>&1', $this->harness->php_cli, $tmp_file), $return);
 614        $retval = ob_get_clean();
 615        if (0 == $return)
 616        {
 617          if (false === $cov = unserialize(substr($retval, strpos($retval, '<PHP_SER>') + 9, strpos($retval, '</PHP_SER>') - 9)))
 618          {
 619            throw new Exception(sprintf('Unable to unserialize coverage for file "%s"', $file));
 620          }
 621  
 622          foreach ($cov as $file => $lines)
 623          {
 624            if (!isset($coverage[$file]))
 625            {
 626              $coverage[$file] = array();
 627            }
 628  
 629            foreach ($lines as $line => $count)
 630            {
 631              if (!isset($coverage[$file][$line]))
 632              {
 633                $coverage[$file][$line] = 0;
 634              }
 635              $coverage[$file][$line] = $coverage[$file][$line] + $count;
 636            }
 637          }
 638        }
 639      }
 640      unlink($tmp_file);
 641  
 642      ksort($coverage);
 643      $total_php_lines = 0;
 644      $total_covered_lines = 0;
 645      foreach ($this->files as $file)
 646      {
 647        $cov = isset($coverage[$file]) ? $coverage[$file] : array();
 648  
 649        list($cov, $php_lines) = $this->compute(file_get_contents($file), $cov);
 650  
 651        $output = $this->harness->output;
 652        $percent = count($php_lines) ? count($cov) * 100 / count($php_lines) : 100;
 653  
 654        $total_php_lines += count($php_lines);
 655        $total_covered_lines += count($cov);
 656  
 657        $output->echoln(sprintf("%-70s %3.0f%%", substr($this->get_relative_file($file), -70), $percent), $percent == 100 ? 'INFO' : ($percent > 90 ? 'PARAMETER' : ($percent < 20 ? 'ERROR' : '')));
 658        if ($this->verbose && $percent != 100)
 659        {
 660          $output->comment(sprintf("missing: %s", $this->format_range(array_keys(array_diff_key($php_lines, $cov)))));
 661        }
 662      }
 663  
 664      $output->echoln(sprintf("TOTAL COVERAGE: %3.0f%%", $total_covered_lines * 100 / $total_php_lines));
 665    }
 666  
 667    static function get_php_lines($content)
 668    {
 669      if (is_file($content))
 670      {
 671        $content = file_get_contents($content);
 672      }
 673  
 674      $tokens = token_get_all($content);
 675      $php_lines = array();
 676      $current_line = 1;
 677      $in_class = false;
 678      $in_function = false;
 679      $in_function_declaration = false;
 680      $end_of_current_expr = true;
 681      $open_braces = 0;
 682      foreach ($tokens as $token)
 683      {
 684        if (is_string($token))
 685        {
 686          switch ($token)
 687          {
 688            case '=':
 689              if (false === $in_class || (false !== $in_function && !$in_function_declaration))
 690              {
 691                $php_lines[$current_line] = true;
 692              }
 693              break;
 694            case '{':
 695              ++$open_braces;
 696              $in_function_declaration = false;
 697              break;
 698            case ';':
 699              $in_function_declaration = false;
 700              $end_of_current_expr = true;
 701              break;
 702            case '}':
 703              $end_of_current_expr = true;
 704              --$open_braces;
 705              if ($open_braces == $in_class)
 706              {
 707                $in_class = false;
 708              }
 709              if ($open_braces == $in_function)
 710              {
 711                $in_function = false;
 712              }
 713              break;
 714          }
 715  
 716          continue;
 717        }
 718  
 719        list($id, $text) = $token;
 720  
 721        switch ($id)
 722        {
 723          case T_CURLY_OPEN:
 724          case T_DOLLAR_OPEN_CURLY_BRACES:
 725            ++$open_braces;
 726            break;
 727          case T_WHITESPACE:
 728          case T_OPEN_TAG:
 729          case T_CLOSE_TAG:
 730            $end_of_current_expr = true;
 731            $current_line += count(explode("\n", $text)) - 1;
 732            break;
 733          case T_COMMENT:
 734          case T_DOC_COMMENT:
 735            $current_line += count(explode("\n", $text)) - 1;
 736            break;
 737          case T_CLASS:
 738            $in_class = $open_braces;
 739            break;
 740          case T_FUNCTION:
 741            $in_function = $open_braces;
 742            $in_function_declaration = true;
 743            break;
 744          case T_AND_EQUAL:
 745          case T_CASE:
 746          case T_CATCH:
 747          case T_CLONE:
 748          case T_CONCAT_EQUAL:
 749          case T_CONTINUE:
 750          case T_DEC:
 751          case T_DECLARE:
 752          case T_DEFAULT:
 753          case T_DIV_EQUAL:
 754          case T_DO:
 755          case T_ECHO:
 756          case T_ELSEIF:
 757          case T_EMPTY:
 758          case T_ENDDECLARE:
 759          case T_ENDFOR:
 760          case T_ENDFOREACH:
 761          case T_ENDIF:
 762          case T_ENDSWITCH:
 763          case T_ENDWHILE:
 764          case T_EVAL:
 765          case T_EXIT:
 766          case T_FOR:
 767          case T_FOREACH:
 768          case T_GLOBAL:
 769          case T_IF:
 770          case T_INC:
 771          case T_INCLUDE:
 772          case T_INCLUDE_ONCE:
 773          case T_INSTANCEOF:
 774          case T_ISSET:
 775          case T_IS_EQUAL:
 776          case T_IS_GREATER_OR_EQUAL:
 777          case T_IS_IDENTICAL:
 778          case T_IS_NOT_EQUAL:
 779          case T_IS_NOT_IDENTICAL:
 780          case T_IS_SMALLER_OR_EQUAL:
 781          case T_LIST:
 782          case T_LOGICAL_AND:
 783          case T_LOGICAL_OR:
 784          case T_LOGICAL_XOR:
 785          case T_MINUS_EQUAL:
 786          case T_MOD_EQUAL:
 787          case T_MUL_EQUAL:
 788          case T_NEW:
 789          case T_OBJECT_OPERATOR:
 790          case T_OR_EQUAL:
 791          case T_PLUS_EQUAL:
 792          case T_PRINT:
 793          case T_REQUIRE:
 794          case T_REQUIRE_ONCE:
 795          case T_RETURN:
 796          case T_SL:
 797          case T_SL_EQUAL:
 798          case T_SR:
 799          case T_SR_EQUAL:
 800          case T_THROW:
 801          case T_TRY:
 802          case T_UNSET:
 803          case T_UNSET_CAST:
 804          case T_USE:
 805          case T_WHILE:
 806          case T_XOR_EQUAL:
 807            $php_lines[$current_line] = true;
 808            $end_of_current_expr = false;
 809            break;
 810          default:
 811            if (false === $end_of_current_expr)
 812            {
 813              $php_lines[$current_line] = true;
 814            }
 815            //print "$current_line: ".token_name($id)."\n";
 816        }
 817      }
 818  
 819      return $php_lines;
 820    }
 821  
 822    function compute($content, $cov)
 823    {
 824      $php_lines = self::get_php_lines($content);
 825  
 826      // we remove from $cov non php lines
 827      foreach (array_diff_key($cov, $php_lines) as $line => $tmp)
 828      {
 829        unset($cov[$line]);
 830      }
 831  
 832      return array($cov, $php_lines);
 833    }
 834  
 835    function format_range($lines)
 836    {
 837      sort($lines);
 838      $formatted = '';
 839      $first = -1;
 840      $last = -1;
 841      foreach ($lines as $line)
 842      {
 843        if ($last + 1 != $line)
 844        {
 845          if ($first != -1)
 846          {
 847            $formatted .= $first == $last ? "$first " : "[$first - $last] ";
 848          }
 849          $first = $line;
 850          $last = $line;
 851        }
 852        else
 853        {
 854          $last = $line;
 855        }
 856      }
 857      if ($first != -1)
 858      {
 859        $formatted .= $first == $last ? "$first " : "[$first - $last] ";
 860      }
 861  
 862      return $formatted;
 863    }
 864  }
 865  
 866  class lime_registration
 867  {
 868    public $files = array();
 869    public $extension = '.php';
 870    public $base_dir = '';
 871  
 872    function register($files_or_directories)
 873    {
 874      foreach ((array) $files_or_directories as $f_or_d)
 875      {
 876        if (is_file($f_or_d))
 877        {
 878          $this->files[] = realpath($f_or_d);
 879        }
 880        elseif (is_dir($f_or_d))
 881        {
 882          $this->register_dir($f_or_d);
 883        }
 884        else
 885        {
 886          throw new Exception(sprintf('The file or directory "%s" does not exist.', $f_or_d));
 887        }
 888      }
 889    }
 890  
 891    function register_glob($glob)
 892    {
 893      if ($dirs = glob($glob))
 894      {
 895        foreach ($dirs as $file)
 896        {
 897          $this->files[] = realpath($file);
 898        }
 899      }
 900    }
 901  
 902    function register_dir($directory)
 903    {
 904      if (!is_dir($directory))
 905      {
 906        throw new Exception(sprintf('The directory "%s" does not exist.', $directory));
 907      }
 908  
 909      $files = array();
 910  
 911      $current_dir = opendir($directory);
 912      while ($entry = readdir($current_dir))
 913      {
 914        if ($entry == '.' || $entry == '..') continue;
 915  
 916        if (is_dir($entry))
 917        {
 918          $this->register_dir($entry);
 919        }
 920        elseif (preg_match('#'.$this->extension.'$#', $entry))
 921        {
 922          $files[] = realpath($directory.DIRECTORY_SEPARATOR.$entry);
 923        }
 924      }
 925  
 926      $this->files = array_merge($this->files, $files);
 927    }
 928  
 929    protected function get_relative_file($file)
 930    {
 931      return str_replace(DIRECTORY_SEPARATOR, '/', str_replace(array(realpath($this->base_dir).DIRECTORY_SEPARATOR, $this->extension), '', $file));
 932    }
 933  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7