Skip to content

Commit 1336d8b

Browse files
committed
Fixed bug #1432 : PHPCBF can make invalid fixes to inline JS control structures that make use of JS objects
1 parent 937a382 commit 1336d8b

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

CodeSniffer/File.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,7 @@ private static function _recurseScopeMap(
22582258
// current scope opener, so it must be a string offset.
22592259
if (PHP_CODESNIFFER_VERBOSITY > 1) {
22602260
echo str_repeat("\t", $depth);
2261-
echo '* ignoring curly brace *'.PHP_EOL;
2261+
echo '* ignoring curly brace inside condition *'.PHP_EOL;
22622262
}
22632263

22642264
$ignore++;
@@ -2269,15 +2269,21 @@ private static function _recurseScopeMap(
22692269
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === true) {
22702270
continue;
22712271
} else {
2272-
// If the first non-whitespace/comment token is a
2273-
// variable or object operator then this is an opener
2274-
// for a string offset and not a scope.
2275-
if ($tokens[$x]['code'] === T_VARIABLE
2276-
|| $tokens[$x]['code'] === T_OBJECT_OPERATOR
2277-
) {
2272+
// If the first non-whitespace/comment token looks like this
2273+
// brace is a string offset, or this brace is mid-way through
2274+
// a new statement, it isn't a scope opener.
2275+
$disallowed = PHP_CodeSniffer_Tokens::$assignmentTokens;
2276+
$disallowed += array(
2277+
T_VARIABLE => true,
2278+
T_OBJECT_OPERATOR => true,
2279+
T_COMMA => true,
2280+
T_OPEN_PARENTHESIS => true,
2281+
);
2282+
2283+
if (isset($disallowed[$tokens[$x]['code']]) === true) {
22782284
if (PHP_CODESNIFFER_VERBOSITY > 1) {
22792285
echo str_repeat("\t", $depth);
2280-
echo '* ignoring curly brace *'.PHP_EOL;
2286+
echo '* ignoring curly brace after condition *'.PHP_EOL;
22812287
}
22822288

22832289
$ignore++;

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ do i++; while (i < 5);
2323
SomeClass.prototype.switch = function() {
2424
// do something
2525
};
26+
27+
if ($("#myid").rotationDegrees()=='90')
28+
$('.modal').css({'transform': 'rotate(90deg)'});
29+
30+
if ($("#myid").rotationDegrees()=='90')
31+
$foo = {'transform': 'rotate(90deg)'};

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ do { i++;
2929
SomeClass.prototype.switch = function() {
3030
// do something
3131
};
32+
33+
if ($("#myid").rotationDegrees()=='90') {
34+
$('.modal').css({'transform': 'rotate(90deg)'});
35+
}
36+
37+
if ($("#myid").rotationDegrees()=='90') {
38+
$foo = {'transform': 'rotate(90deg)'};
39+
}

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
9191
13 => 1,
9292
15 => 1,
9393
21 => 1,
94+
27 => 1,
95+
30 => 1,
9496
);
9597
break;
9698
default:

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ControlSignatureUnitTest.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,9 @@ if (window.jQuery)(function($) {
127127
});
128128
};
129129
})(jQuery);
130+
131+
if ($("#myid").rotationDegrees()=='90')
132+
$('.modal').css({'transform': 'rotate(90deg)'});
133+
134+
if ($("#myid").rotationDegrees()=='90')
135+
$foo = {'transform': 'rotate(90deg)'};

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ControlSignatureUnitTest.js.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,9 @@ if (window.jQuery)(function($) {
133133
});
134134
};
135135
})(jQuery);
136+
137+
if ($("#myid").rotationDegrees()=='90')
138+
$('.modal').css({'transform': 'rotate(90deg)'});
139+
140+
if ($("#myid").rotationDegrees()=='90')
141+
$foo = {'transform': 'rotate(90deg)'};

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6161
- Fixed bug #1421 : Ternaries used in constant scalar expression for param default misidentified by tokenizer
6262
- Fixed bug #1431 : PHPCBF can't fix short open tags when they are not followed by a space
6363
-- Thanks to Gonçalo Queirós for the patch
64+
- Fixed bug #1432 : PHPCBF can make invalid fixes to inline JS control structures that make use of JS objects
6465
</notes>
6566
<contents>
6667
<dir name="/">

0 commit comments

Comments
 (0)