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

Source for file Gitblame.php

Documentation is available at Gitblame.php

  1. <?php
  2. /**
  3.  * Git 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 Gitblame extends VersionControl
  14. {
  15.  
  16.     /**
  17.      * The name of the report we want in the output
  18.      *
  19.      * @var string 
  20.      */
  21.     protected $reportName 'GIT';
  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.         preg_match(
  36.             '|\(.+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]+\)|',
  37.             $line,
  38.             $blameParts
  39.         );
  40.  
  41.         if (isset($blameParts[0]=== false{
  42.             return false;
  43.         }
  44.  
  45.         $parts explode(' '$blameParts[0]);
  46.  
  47.         if (count($parts< 2{
  48.             return false;
  49.         }
  50.  
  51.         $parts  array_slice($parts0(count($parts- 2));
  52.         $author preg_replace('|\(|'''implode($parts' '));
  53.         return $author;
  54.  
  55.     }//end getAuthor()
  56.  
  57.  
  58.     /**
  59.      * Gets the blame output.
  60.      *
  61.      * @param string $filename File to blame.
  62.      *
  63.      * @return array 
  64.      */
  65.     protected function getBlameContent($filename)
  66.     {
  67.         $cwd getcwd();
  68.  
  69.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  70.             echo 'Getting GIT blame info for '.basename($filename).'... ';
  71.         }
  72.  
  73.         $fileParts explode(DIRECTORY_SEPARATOR$filename);
  74.         $found     = false;
  75.         $location  '';
  76.         while (empty($fileParts=== false{
  77.             array_pop($fileParts);
  78.             $location implode($filePartsDIRECTORY_SEPARATOR);
  79.             if (is_dir($location.DIRECTORY_SEPARATOR.'.git'=== true{
  80.                 $found = true;
  81.                 break;
  82.             }
  83.         }
  84.  
  85.         if ($found === true{
  86.             chdir($location);
  87.         else {
  88.             echo 'ERROR: Could not locate .git directory '.PHP_EOL.PHP_EOL;
  89.             exit(3);
  90.         }
  91.  
  92.         $command 'git blame --date=short "'.$filename.'" 2>&1';
  93.         $handle  popen($command'r');
  94.         if ($handle === false{
  95.             echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
  96.             exit(3);
  97.         }
  98.  
  99.         $rawContent stream_get_contents($handle);
  100.         fclose($handle);
  101.  
  102.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  103.             echo 'DONE'.PHP_EOL;
  104.         }
  105.  
  106.         $blames explode("\n"$rawContent);
  107.         chdir($cwd);
  108.  
  109.         return $blames;
  110.  
  111.     }//end getBlameContent()
  112.  
  113.  
  114. }//end class

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