PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file Hgblame.php

Documentation is available at Hgblame.php

  1. <?php
  2. /**
  3.  * Mercurial blame report for PHP_CodeSniffer.
  4.  *
  5.  * @author    Ben Selby <benmatselby@gmail.com>
  6.  * @author    Greg Sherwood <gsherwood@squiz.net>
  7.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  8.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  9.  */
  10.  
  11. namespace PHP_CodeSniffer\Reports;
  12.  
  13. class Hgblame extends VersionControl
  14. {
  15.  
  16.     /**
  17.      * The name of the report we want in the output
  18.      *
  19.      * @var string 
  20.      */
  21.     protected $reportName 'MERCURIAL';
  22.  
  23.  
  24.     /**
  25.      * Extract the author from a blame line.
  26.      *
  27.      * @param string $line Line to parse.
  28.      *
  29.      * @return mixed string or false if impossible to recover.
  30.      */
  31.     protected function getAuthor($line)
  32.     {
  33.         $blameParts = array();
  34.         $line       preg_replace('|\s+|'' '$line);
  35.  
  36.         preg_match(
  37.             '|(.+[0-9]{2}:[0-9]{2}:[0-9]{2}\s[0-9]{4}\s.[0-9]{4}:)|',
  38.             $line,
  39.             $blameParts
  40.         );
  41.  
  42.         if (isset($blameParts[0]=== false{
  43.             return false;
  44.         }
  45.  
  46.         $parts explode(' '$blameParts[0]);
  47.  
  48.         if (count($parts< 6{
  49.             return false;
  50.         }
  51.  
  52.         $parts array_slice($parts0(count($parts- 6));
  53.  
  54.         return trim(preg_replace('|<.+>|'''implode($parts' ')));
  55.  
  56.     }//end getAuthor()
  57.  
  58.  
  59.     /**
  60.      * Gets the blame output.
  61.      *
  62.      * @param string $filename File to blame.
  63.      *
  64.      * @return array 
  65.      */
  66.     protected function getBlameContent($filename)
  67.     {
  68.         $cwd getcwd();
  69.  
  70.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  71.             echo 'Getting MERCURIAL blame info for '.basename($filename).'... ';
  72.         }
  73.  
  74.         $fileParts explode(DIRECTORY_SEPARATOR$filename);
  75.         $found     = false;
  76.         $location  '';
  77.         while (empty($fileParts=== false{
  78.             array_pop($fileParts);
  79.             $location implode($filePartsDIRECTORY_SEPARATOR);
  80.             if (is_dir($location.DIRECTORY_SEPARATOR.'.hg'=== true{
  81.                 $found = true;
  82.                 break;
  83.             }
  84.         }
  85.  
  86.         if ($found === true{
  87.             chdir($location);
  88.         else {
  89.             echo 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL;
  90.             exit(3);
  91.         }
  92.  
  93.         $command 'hg blame -u -d -v "'.$filename.'" 2>&1';
  94.         $handle  popen($command'r');
  95.         if ($handle === false{
  96.             echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
  97.             exit(3);
  98.         }
  99.  
  100.         $rawContent stream_get_contents($handle);
  101.         fclose($handle);
  102.  
  103.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  104.             echo 'DONE'.PHP_EOL;
  105.         }
  106.  
  107.         $blames explode("\n"$rawContent);
  108.         chdir($cwd);
  109.  
  110.         return $blames;
  111.  
  112.     }//end getBlameContent()
  113.  
  114.  
  115. }//end class

Documentation generated on Mon, 11 Mar 2019 14:53:29 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.