Skip to content

Commit

Permalink
Fixed bug #645 : FunctionSignature and ScopeIndent sniffs don't detec…
Browse files Browse the repository at this point in the history
…t indents correctly when PHP open tag is not on a line by itself
  • Loading branch information
gsherwood committed Jul 21, 2015
1 parent 1ed6eff commit c253376
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,19 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$openScopes = array();
$adjustments = array();

$tokens = $phpcsFile->getTokens();
$currentIndent = ($tokens[$stackPtr]['column'] - 1);
$tokens = $phpcsFile->getTokens();
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
$trimmed = ltrim($tokens[$first]['content']);
if ($trimmed === '') {
$currentIndent = ($tokens[$stackPtr]['column'] - 1);
} else {
$currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed));
}

if ($this->_debug === true) {
$line = $tokens[$stackPtr]['line'];
echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL;
}

if (empty($this->_ignoreIndentationTokens) === true) {
$this->_ignoreIndentationTokens = array(T_INLINE_HTML => true);
Expand Down Expand Up @@ -269,8 +280,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}//end if

// Closing parenthesis should just be indented to at least
// the same level as where they were opened (but can be more).
// Closing short array bracket should just be indented to at least
// the same level as where it was opened (but can be more).
if ($checkToken !== null
&& $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,14 @@ array(
},
);

?>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<?php

try {
echo 'foo';
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,14 @@ array(
},
);

?>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<?php

try {
echo 'foo';
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,14 @@ array(
},
);

?>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<?php

try {
echo 'foo';
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,14 @@ array(
},
);

?>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<?php

try {
echo 'foo';
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
802 => 1,
803 => 1,
823 => 1,
848 => 1,
860 => 1,
861 => 1,
863 => 1,
865 => 1,
856 => 1,
868 => 1,
869 => 1,
871 => 1,
873 => 1,
);

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,24 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr,
// We need to work out how far indented the function
// call itself is, so we can work out how far to
// indent the arguments.
$functionIndent = 0;
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
$i++;
break;
}
}

if ($i > 0 && $tokens[$i]['code'] === T_WHITESPACE) {
if ($i <= 0) {
$functionIndent = 0;
} else if ($tokens[$i]['code'] === T_WHITESPACE) {
$functionIndent = strlen($tokens[$i]['content']);
} else {
$trimmed = ltrim($tokens[$i]['content']);
if ($trimmed === '') {
$functionIndent = ($tokens[$i]['column'] - 1);
} else {
$functionIndent = (strlen($tokens[$i]['content']) - strlen($trimmed));
}
}

if ($tokens[($openBracket + 1)]['content'] !== $phpcsFile->eolChar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,16 @@ test( $arg, $arg2 );
test( $arg, $arg2 );
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesAfterOpen 0
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesBeforeClose 0

?>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<?php
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,16 @@ test( $arg, $arg2 );
test( $arg, $arg2 );
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesAfterOpen 0
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesBeforeClose 0

?>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<script>
var foo = <?php echo bar(
'baz'
); ?>;
</script>
<?php
5 changes: 3 additions & 2 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #636 : Some class names cause CSS tokenizer to hang
- Fixed bug #642 : Method params incorrectly detected when default value uses short array syntax
-- Thanks to Josh Davis for the patch
- Fixed bug #645 : FunctionSignature and ScopeIndent sniffs don't detect indents correctly when PHP open tag is not on a line by itself
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -2355,7 +2356,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Added more guard code for syntax errors to various sniffs
- Improved support for older HHVM versions
-- Thanks to Kunal Mehta for the patch
- Squiz ValidLogicalOperatorsSniff now ignores XOR as type casting is different when using the ^ operator (request #567)
- Squiz ValidLogicalOperatorsSniff now ignores XOR as type casting is different when using the ^ operator (request #567)
- Squiz CommentedOutCodeSniff is now better at ignoring URLs inside comments
- Squiz ControlSignatureSniff is now better at checking embedded PHP code
- Squiz ScopeClosingBraceSniff is now better at checking embedded PHP code
Expand Down Expand Up @@ -2421,7 +2422,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Squiz ConcatenationSpacingSniff now has a setting to ignore newline characters around operators (request #511)
-- Default remains FALSE, so newlines are not allowed
-- Override the "ignoreNewlines" setting in a ruleset.xml file to change
- Squiz InlineCommentSniff no longer checks the last char of a comment if the first char is not a letter (request #505)
- Squiz InlineCommentSniff no longer checks the last char of a comment if the first char is not a letter (request #505)
- The Squiz standard has increased the max padding for statement alignment from 12 to 20
- Fixed bug #479 : Yielded values are not recognised as returned values in Squiz FunctionComment sniff
- Fixed bug #512 : Endless loop whilst parsing mixture of control structure styles
Expand Down

0 comments on commit c253376

Please sign in to comment.