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.         chdir(dirname($filename));
  70.         $command 'git blame --date=short "'.$filename.'" 2>&1';
  71.         $handle  popen($command'r');
  72.         if ($handle === false{
  73.             echo 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
  74.             exit(3);
  75.         }
  76.  
  77.         $rawContent stream_get_contents($handle);
  78.         fclose($handle);
  79.  
  80.         $blames explode("\n"$rawContent);
  81.         chdir($cwd);
  82.  
  83.         return $blames;
  84.  
  85.     }//end getBlameContent()
  86.  
  87.  
  88. }//end class

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