Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #352 from AKSW/fix/FileCommentSniff
Browse files Browse the repository at this point in the history
fixed the errors about empty arrays
  • Loading branch information
white-gecko committed May 20, 2016
2 parents 0558949 + f4c2d80 commit b82467d
Showing 1 changed file with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,43 @@ public function register()
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
exec('([ -d .git ] && echo .git) || git rev-parse --git-dir 2> /dev/null', $git_test);
if(!empty($git_test))
{
exec('git log --reverse ' . $phpcsFile->getFilename() . ' | head -3' , $output1);
preg_match("/( )[0-9]{4}( )/", $output1[2],$git_year_array1);
$git_year1=str_replace(' ','',$git_year_array1[0]);
exec('git log -1 ' . $phpcsFile->getFilename(), $output2);
preg_match("/( )[0-9]{4}( )/", $output2[2],$git_year_array2);
$git_year2=str_replace(' ','',$git_year_array2[0]);
if(strcmp($git_year1,$git_year2)!=0)
{
$git_year1 .='-';
$git_year1 .=$git_year2;
}
//$year = " * @copyright Copyright (c) " . date('Y') . ", {@link http://aksw.org AKSW}\n";
$year = " * @copyright Copyright (c) " . $git_year1 . ", {@link http://aksw.org AKSW}\n";
$this->copyright[4]= $year;
// Find the next non whitespace token.
$commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
$noGit= true;
//test if a git exists to get the years from 'git log'
exec('([ -d .git ] && echo .git) || git rev-parse --git-dir 2> /dev/null', $gitTest);
if(!empty($gitTest)){
//test if a git entry exists to get the years from 'git log'
exec('git log --reverse ' . $phpcsFile->getFilename() . ' | head -3' , $outputCreationYear);
if(!empty($outputCreationYear)) {
preg_match("/( )[0-9]{4}( )/", $outputCreationYear[2],$gitOldYearArray);
$gitYearOld=str_replace(' ','',$gitOldYearArray[0]);
exec('git log -1 ' . $phpcsFile->getFilename(), $outputLastEditYear);
preg_match("/( )[0-9]{4}( )/", $outputLastEditYear[2],$gitNewYearArray);
$gitYearNew=str_replace(' ','',$gitNewYearArray[0]);
if(strcmp($gitYearOld,$gitYearNew)!=0)
{
$gitYearOld .='-';
$gitYearOld .=$gitYearNew;
}
$year = " * @copyright Copyright (c) " . $gitYearOld . ", {@link http://aksw.org AKSW}\n";
$this->copyright[4]= $year;
$noGit = false;
}
}
else {
preg_match("/( )[0-9]{4}(-[0-9]{4})?/",$tokens[16]['content'],$non_git_year);
$year = " * @copyright Copyright (c) " . str_replace(' ','',$non_git_year[0]) . ", {@link http://aksw.org AKSW}\n";
$this->copyright[4]= $year;
if($noGit) {
if(count($tokens)>15)
preg_match("/( )[0-9]{4}(-[0-9]{4})?/",$tokens[$commentStart+15]['content'],$nonGitYear);
//tests if the file has no year/wrong editing and the year can't be found
if(!empty($nonGitYear))
{
$year = " * @copyright Copyright (c) " . str_replace(' ','',$nonGitYear[0]) . ", {@link http://aksw.org AKSW}\n";
$this->copyright[4]= $year;
}
}
$tokenizer = new PHP_CodeSniffer_Tokenizers_Comment();
$expectedString = implode($this->copyright);
$expectedTokens = $tokenizer->tokenizeString($expectedString, PHP_EOL, 0);
// Find the next non whitespace token.
$commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);

// Allow namespace statements at the top of the file.
if ($tokens[$commentStart]['code'] === T_NAMESPACE) {
$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($commentStart + 1));
Expand All @@ -108,7 +116,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($fix === true) {
$phpcsFile->fixer->replaceToken($commentStart, "/**");
}

return;
}

Expand Down Expand Up @@ -140,8 +147,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[$i]['content'] !== $expectedTokens[$j]["content"]) {
$error = 'Found wrong part of copyright notice. Expected "%s", but found "%s"';
$data = array(
trim($expectedTokens[$j]["content"]),
trim($tokens[$i]['content']),
$expectedTokens[$j]["content"],
$tokens[$i]['content'],
);
$fix = $phpcsFile->addFixableError($error, $i, 'WrongText', $data);

Expand Down

0 comments on commit b82467d

Please sign in to comment.