Skip to content

Commit

Permalink
Merge pull request #8 from HydraWiki/develop
Browse files Browse the repository at this point in the history
Fix Tab handling and array formatting
  • Loading branch information
valeryan authored Jan 30, 2019
2 parents ada596f + be87dd5 commit 5b5c87a
Show file tree
Hide file tree
Showing 10 changed files with 969 additions and 124 deletions.
29 changes: 29 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,php,css}]
charset = utf-8

# 4 space indentation
[*.php]
indent_style = tab
indent_size = 4

# Tab indentation (no size specified)
[Makefile]
indent_style = tab


# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
106 changes: 106 additions & 0 deletions HydraWiki/Sniffs/Arrays/NoExtraSpacesSingleLineSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Curse Inc.
* NoExtraSpacesSniff
*
* This file was copied from PHP_CodeSniffer before being modified
* File: Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
* From repository: https://github.com/squizlabs/PHP_CodeSniffer
*
* Ensures there is no extra space in single line arrays.
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD-3-Clause
*/

namespace HydraWiki\Sniffs\Arrays;

use PHP_CodeSniffer\Sniffs\AbstractArraySniff;

class NoExtraSpacesSingleLineSniff extends AbstractArraySniff {

/**
* The number of spaces each array key should be indented.
*
* @var integer
*/
public $indent = 4;

/**
* Processes a single-line array definition.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* @param int $arrayStart The token that starts the array definition.
* @param int $arrayEnd The token that ends the array definition.
* @param array $indices An array of token positions for the array keys, double arrows, and values.
*
* @return void
*/
public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices) {
$this->handleArrayStart($phpcsFile, $arrayStart);
$this->handleArrayEnd($phpcsFile, $arrayEnd);
}

/**
* Processes a multi-line array definition.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* @param int $arrayStart The token that starts the array definition.
* @param int $arrayEnd The token that ends the array definition.
* @param array $indices An array of token positions for the array keys, double arrows, and values.
*
* @return void
*/
public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices) {
// No multiline processing.
}

/**
* Process the starting brace of a short array
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile
* @param int $arrayStart
* @return void
*/
private function handleArrayStart($phpcsFile, $arrayStart) {
$tokens = $phpcsFile->getTokens();
$spacing = 0;
if ($tokens[($arrayStart + 1)]['code'] === T_WHITESPACE) {
$spacing = $tokens[($arrayStart + 1)]['length'];
}
if ($spacing == 0) {
return;
}
$message = 'No Spaces allowed after the opening brace of a single-line short array; %s found';
$fix = $phpcsFile->addFixableError($message, $arrayStart, 'SpaceAfterOpeningBrace', [$spacing]);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($arrayStart + 1), '');
}
}

/**
* Process the closing brace of a short array
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile
* @param int $arrayEnd
* @return void
*/
private function handleArrayEnd($phpcsFile, $arrayEnd) {
$tokens = $phpcsFile->getTokens();
$spacing = 0;
if ($tokens[($arrayEnd - 1)]['code'] === T_WHITESPACE) {
$spacing = $tokens[($arrayEnd - 1)]['length'];
}
if ($spacing == 0) {
return;
}
$message = 'No Spaces allowed before the closing brace of a single-line short array; %s found';
$fix = $phpcsFile->addFixableError($message, $arrayEnd, 'SpaceBeforeClosingBrace', [$spacing]);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($arrayEnd - 1), '');
}
}
}
4 changes: 2 additions & 2 deletions HydraWiki/Sniffs/Commenting/LicenseCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LicenseCommentSniff implements Sniff {
* @return array
*/
public function register() {
return [ T_DOC_COMMENT_OPEN_TAG ];
return [T_DOC_COMMENT_OPEN_TAG];
}

/**
Expand Down Expand Up @@ -215,7 +215,7 @@ private function hasTextAfterTag($tag, $next) {
*/
private function isValidProprietaryLicense($license) {
if (strtolower($license) == 'proprietary') {
return true;
return true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion HydraWiki/Sniffs/WhiteSpace/NoSpaceAfterNotSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function process(File $phpcsFile, $stackPtr) {
return;
}
$message = 'There must not be a space after a NOT operator; %s found';
$fix = $phpcsFile->addFixableError($message, $stackPtr, 'Incorrect', [$spacing]);
$fix = $phpcsFile->addFixableError($message, $stackPtr, 'SpaceAfterNot', [$spacing]);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
}
Expand Down
171 changes: 111 additions & 60 deletions HydraWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class SpaceBeforeSingleLineCommentSniff implements Sniff {
* @inheritDoc
*/
public function register() {
return [
T_COMMENT
];
return [T_COMMENT];
}

/**
Expand All @@ -41,6 +39,7 @@ public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$currToken = $tokens[$stackPtr];
$preToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
// Test if comment starts on a new line
if ($preToken !== false &&
$tokens[$preToken]['line'] === $tokens[$stackPtr]['line']
) {
Expand All @@ -50,65 +49,117 @@ public function process(File $phpcsFile, $stackPtr) {
'NewLineComment'
);
}
if ($currToken['code'] === T_COMMENT) {
// Accounting for multiple line comments, as single line comments
// use only '//' and '#'
// Also ignoring PHPDoc comments starting with '///',
// as there are no coding standards documented for these
if (substr($currToken['content'], 0, 2) === '/*'
|| substr($currToken['content'], 0, 3) === '///'
) {
return;

// Validate Current Token
if (!$this->isComment($currToken) ||
$this->isDocBlockComment($currToken) ||
$this->isEmptyComment($currToken)
) {
return;
}

// Checking whether there is a space between the comment delimiter
// and the comment
if (substr($currToken['content'], 0, 2) === '//') {
$this->handleDoubleSlashComment($phpcsFile, $stackPtr, $currToken);
return;
}

// Finding what the comment delimiter is and checking whether there is a space
// between the comment delimiter and the comment.
if ($currToken['content'][0] === '#') {
$this->handleHashComment($phpcsFile, $stackPtr, $currToken);
}
}

/**
* Check contents of current token for empty state
*
* @param array $currToken
* @return boolean
*/
private function isEmptyComment($currToken) {
return (
(substr($currToken['content'], 0, 2) === '//' && rtrim($currToken['content']) === '//') ||
($currToken['content'][0] === '#' && rtrim($currToken['content']) === '#')
);
}

/**
* Check contents of current token for doc block
*
* @param array $currToken
* @return boolean
*/
private function isDocBlockComment($currToken) {
// Accounting for multiple line comments, as single line comments
// use only '//' and '#'
// Also ignoring PHPDoc comments starting with '///',
// as there are no coding standards documented for these
return (substr($currToken['content'], 0, 2) === '/*' || substr($currToken['content'], 0, 3) === '///');
}

/**
* Check if current token is a comment token
*
* @param [type] $currToken
* @return boolean
*/
private function isComment($currToken) {
return $currToken['code'] === T_COMMENT;
}

/**
* handle any double slash style'//' comments
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $currToken
* @return void
*/
private function handleDoubleSlashComment($phpcsFile, $stackPtr, $currToken) {
$commentContent = substr($currToken['content'], 2);
$commentTrim = ltrim($commentContent);
if (strlen($commentContent) == strlen($commentTrim)) {
$fix = $phpcsFile->addFixableWarning(
'At least a single space expected after "//"',
$stackPtr,
'SingleSpaceBeforeSingleLineComment'
);
if ($fix) {
$newContent = '// ';
$newContent .= $commentTrim;
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
}
}
}

// Checking whether the comment is an empty one
if ((substr($currToken['content'], 0, 2) === '//' &&
rtrim($currToken['content']) === '//') ||
($currToken['content'][0] === '#' &&
rtrim($currToken['content']) === '#')
) {
return;
// Checking whether there is a space between the comment delimiter
// and the comment
} elseif (substr($currToken['content'], 0, 2) === '//') {
$commentContent = substr($currToken['content'], 2);
$commentTrim = ltrim($commentContent);
if (strlen($commentContent) == strlen($commentTrim)) {
$error = 'At least a single space expected after "//"';
$fix = $phpcsFile->addFixableWarning(
$error,
$stackPtr,
'SingleSpaceBeforeSingleLineComment'
);
if ($fix) {
$newContent = '// ';
$newContent .= $commentTrim;
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
}
}
// Finding what the comment delimiter is and checking whether there is a space
// between the comment delimiter and the comment.
} elseif ($currToken['content'][0] === '#') {
// Find number of `#` used.
$startComment = 0;
while ($currToken['content'][$startComment] === '#') {
$startComment += 1;
}
if ($currToken['content'][$startComment] !== ' ') {
$error = 'At least a single space expected after "#"';
$fix = $phpcsFile->addFixableWarning(
$error,
$stackPtr,
'SingleSpaceBeforeSingleLineComment'
);
if ($fix) {
$content = $currToken['content'];
$newContent = '# ';
$tmpContent = substr($content, 1);
$newContent .= ltrim($tmpContent);
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
}
}
/**
* handle any hash style '#' comments
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $currToken
* @return void
*/
private function handleHashComment($phpcsFile, $stackPtr, $currToken) {
// Find number of `#` used.
$startComment = 0;
while ($currToken['content'][$startComment] === '#') {
$startComment += 1;
}
if ($currToken['content'][$startComment] !== ' ') {
$fix = $phpcsFile->addFixableWarning(
'At least a single space expected after "#"',
$stackPtr,
'SingleSpaceBeforeSingleLineComment'
);
if ($fix) {
$content = $currToken['content'];
$newContent = '# ';
$tmpContent = substr($content, 1);
$newContent .= ltrim($tmpContent);
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
}
}
}
Expand Down
Loading

0 comments on commit 5b5c87a

Please sign in to comment.