Skip to content

Commit 6e6a3ed

Browse files
committed
Fixed bug #1442 : T_NULLABLE detection not working for nullable parameters and return type hints in some cases
1 parent b940fb7 commit 6e6a3ed

File tree

5 files changed

+23
-41
lines changed

5 files changed

+23
-41
lines changed

CodeSniffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class PHP_CodeSniffer
7373
*
7474
* @var string
7575
*/
76-
const VERSION = '2.9.0';
76+
const VERSION = '2.9.1';
7777

7878
/**
7979
* Package stability; either stable, beta or alpha.

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,5 @@ function foo(string $bar, array $baz, ?MyClass $object) : MyClass {}
189189
declare(strict_types=1);
190190

191191
function foo($c = ((BAR)?10:100)) {}
192+
193+
function foo(string $a = '', ?string $b = ''): ?string {}

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,5 @@ function foo(string $bar, array $baz, ?MyClass $object) : MyClass {}
183183
declare(strict_types=1);
184184

185185
function foo($c = ((BAR) ? 10 : 100)) {}
186+
187+
function foo(string $a = '', ?string $b = ''): ?string {}

CodeSniffer/Tokenizers/PHP.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,27 @@ public function tokenizeString($string, $eolChar='\n')
722722
$newToken = array();
723723
$newToken['content'] = '?';
724724

725+
$prevNonEmpty = null;
725726
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
726727
if (is_array($tokens[$i]) === true) {
727728
$tokenType = $tokens[$i][0];
728729
} else {
729730
$tokenType = $tokens[$i];
730731
}
731732

733+
if ($prevNonEmpty === null
734+
&& isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokenType]) === false
735+
) {
736+
// Found the previous non-empty token.
737+
if ($tokenType === ':' || $tokenType === ',') {
738+
$newToken['code'] = T_NULLABLE;
739+
$newToken['type'] = 'T_NULLABLE';
740+
break;
741+
}
742+
743+
$prevNonEmpty = $tokenType;
744+
}
745+
732746
if ($tokenType === T_FUNCTION) {
733747
$newToken['code'] = T_NULLABLE;
734748
$newToken['type'] = 'T_NULLABLE';
@@ -740,7 +754,7 @@ public function tokenizeString($string, $eolChar='\n')
740754
$insideInlineIf[] = $stackPtr;
741755
break;
742756
}
743-
}
757+
}//end for
744758

745759
$finalTokens[$newStackPtr] = $newToken;
746760
$newStackPtr++;

package.xml

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
1717
<date>2017-05-04</date>
1818
<time>09:30:00</time>
1919
<version>
20-
<release>2.9.0</release>
21-
<api>2.9.0</api>
20+
<release>2.9.1</release>
21+
<api>2.9.1</api>
2222
</version>
2323
<stability>
2424
<release>stable</release>
2525
<api>stable</api>
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29-
- Added Generic.Debug.ESLint sniff to run ESLint over JS files and report errors
30-
-- Set eslint path using: phpcs --config-set eslint_path /path/to/eslint
31-
-- Thanks to Ryan McCue for the contribution
32-
- T_POW is now properly considered an arithmetic operator, and will be checked as such
33-
-- Thanks to Juliette Reinders Folmer for the patch
34-
- T_SPACESHIP and T_COALESCE are now properly considered comparison operators, and will be checked as such
35-
-- Thanks to Juliette Reinders Folmer for the patch
36-
- Generic.PHP.DisallowShortOpenTag now warns about possible short open tags even when short_open_tag is set to OFF
37-
-- Thanks to Juliette Reinders Folmer for the patch
38-
- Generic.WhiteSpace.DisallowTabIndent now finds and fixes inproper use of spaces anywhere inside the line indent
39-
-- Previously, only the first part of the indent was used to determine the indent type
40-
-- Thanks to Juliette Reinders Folmer for the patch
41-
- PEAR.Commenting.ClassComment now supports checking of traits as well as classes and interfaces
42-
-- Thanks to Juliette Reinders Folmer for the patch
43-
- Squiz.Commenting.FunctionCommentThrowTag now supports re-throwing exceptions (request #946)
44-
-- Thanks to Samuel Levy for the patch
45-
- Squiz.PHP.DisallowMultipleAssignments now ignores PHP4-style member var assignments
46-
-- Thanks to Juliette Reinders Folmer for the patch
47-
- Squiz.WhiteSpace.FunctionSpacing now ignores spacing above functions when they are preceded by inline comments
48-
-- Stops conflicts between this sniff and comment spacing sniffs
49-
- Squiz.WhiteSpace.OperatorSpacing no longer checks the equal sign in declare statements
50-
-- Thanks to Juliette Reinders Folmer for the patch
51-
- Added missing error codes for a couple of sniffs so they can now be customised as normal
52-
- Fixed bug #1266 : PEAR.WhiteSpace.ScopeClosingBrace can throw an error while fixing mixed PHP/HTML
53-
- Fixed bug #1364 : Yield From values are not recognised as returned values in Squiz FunctionComment sniff
54-
- Fixed bug #1373 : Error in tab expansion results in white-space of incorrect size
55-
-- Thanks to Mark Clements for the patch
56-
- Fixed bug #1381 : Tokenizer: derefencing incorrectly identified as short array
57-
- Fixed bug #1387 : Squiz.ControlStructures.ControlSignature does not handle alt syntax when checking space after closing brace
58-
- Fixed bug #1392 : Scope indent calculated incorrectly when using array destructuring
59-
- Fixed bug #1394 : integer type hints appearing as TypeHintMissing instead of ScalarTypeHintMissing
60-
-- PHP 7 type hints were also being shown when run under PHP 5 in some cases
61-
- Fixed bug #1405 : Squiz.WhiteSpace.ScopeClosingBrace fails to fix closing brace within indented PHP tags
62-
- Fixed bug #1421 : Ternaries used in constant scalar expression for param default misidentified by tokenizer
63-
- Fixed bug #1431 : PHPCBF can't fix short open tags when they are not followed by a space
64-
-- Thanks to Gonçalo Queirós for the patch
65-
- Fixed bug #1432 : PHPCBF can make invalid fixes to inline JS control structures that make use of JS objects
29+
- Fixed bug #1442 : T_NULLABLE detection not working for nullable parameters and return type hints in some cases
6630
</notes>
6731
<contents>
6832
<dir name="/">

0 commit comments

Comments
 (0)