Skip to content

Commit 0f55c12

Browse files
authored
Merge pull request #205 from PHPCSStandards/develop
Release PHPCSExtra 1.0.1
2 parents 5ab4e9e + a1ff522 commit 0f55c12

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

.github/release-checklist.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PR for tracking changes for the x.x.x release. Target release date: **DOW MONTH
1616
- [ ] Merge this PR
1717
- [ ] Make sure all CI builds are green.
1818
- [ ] Tag and create a release (careful, GH defaults to `develop`!) & copy & paste the changelog to it.
19-
:pencil2: Don't forget to copy the link collection from the bottom of the changelog!
19+
:pencil2: Check if anything from the link collection at the bottom of the changelog needs to be copied in!
2020
- [ ] Make sure all CI builds are green.
2121
- [ ] Close the milestone
2222
- [ ] Open a new milestone for the next release

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses
1515
_Nothing yet._
1616

1717

18+
## [1.0.1] - 2023-01-05
19+
20+
### Fixed
21+
22+
#### Universal
23+
24+
* `Universal.CodeAnalysis.ConstructorDestructorReturn`: fixed false positive for return statements in nested functions/closures declared within constructor/destructor methods. Thanks [@anomiex] for reporting! [#201], [#202]
25+
26+
[#201]: https://github.com/PHPCSStandards/PHPCSExtra/issues/201
27+
[#202]: https://github.com/PHPCSStandards/PHPCSExtra/pull/202
28+
29+
1830
## [1.0.0] - 2023-01-04
1931

2032
:warning: Important: this package now requires [PHPCSUtils 1.0.0]. Please make sure you use `--with-[all-]dependencies` when running `composer update`. :exclamation:
@@ -375,10 +387,12 @@ This initial alpha release contains the following sniffs:
375387
[Composer PHPCS plugin]: https://github.com/PHPCSStandards/composer-installer
376388

377389
[Unreleased]: https://github.com/PHPCSStandards/PHPCSExtra/compare/stable...HEAD
390+
[1.0.1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0...1.0.1
378391
[1.0.0]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-rc1...1.0.0
379392
[1.0.0-RC1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha3...1.0.0-rc1
380393
[1.0.0-alpha3]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha2...1.0.0-alpha3
381394
[1.0.0-alpha2]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha1...1.0.0-alpha2
382395

396+
[@anomiex]: https://github.com/anomiex
383397
[@derickr]: https://github.com/derickr
384398
[@GaryJones]: https://github.com/GaryJones

Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHP_CodeSniffer\Sniffs\Sniff;
1515
use PHP_CodeSniffer\Util\Tokens;
1616
use PHPCSUtils\BackCompat\BCFile;
17+
use PHPCSUtils\Tokens\Collections;
1718
use PHPCSUtils\Utils\FunctionDeclarations;
1819
use PHPCSUtils\Utils\GetTokensAsString;
1920
use PHPCSUtils\Utils\NamingConventions;
@@ -128,12 +129,24 @@ public function process(File $phpcsFile, $stackPtr)
128129
$current = $tokens[$stackPtr]['scope_opener'];
129130
$end = $tokens[$stackPtr]['scope_closer'];
130131

132+
// Not searching for arrow functions as those have an implicit return, so no
133+
$search = Collections::functionDeclarationTokens();
134+
$search[\T_RETURN] = \T_RETURN;
135+
131136
do {
132-
$current = $phpcsFile->findNext(\T_RETURN, ($current + 1), $end);
137+
$current = $phpcsFile->findNext($search, ($current + 1), $end);
133138
if ($current === false) {
134139
break;
135140
}
136141

142+
if (isset(Collections::functionDeclarationTokens()[$tokens[$current]['code']])
143+
&& isset($tokens[$current]['scope_closer'])
144+
) {
145+
// Skip over nested function/closure declarations.
146+
$current = $tokens[$current]['scope_closer'];
147+
continue;
148+
}
149+
137150
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($current + 1), $end, true);
138151
if ($next === false
139152
|| $tokens[$next]['code'] === \T_SEMICOLON

Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,34 @@ interface InterfaceMethodReturnTypes {
123123

124124
public function __destruct(): void;
125125
}
126+
127+
// Issue #201 - prevent false positives for nested functions/closures.
128+
class NestedFunctions {
129+
public function __construct() {
130+
function global_function() {
131+
return true;
132+
}
133+
}
134+
135+
public function __construct() {
136+
function global_function($foo) {
137+
return $foo;
138+
}
139+
}
140+
}
141+
142+
class NestedClosures {
143+
public function __construct() {
144+
do_something(
145+
function () {
146+
return true;
147+
},
148+
);
149+
}
150+
151+
public function __construct() {
152+
$this->callback = function () {
153+
return 10;
154+
};
155+
}
156+
}

Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc.fixed

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,34 @@ interface InterfaceMethodReturnTypes {
122122

123123
public function __destruct();
124124
}
125+
126+
// Issue #201 - prevent false positives for nested functions/closures.
127+
class NestedFunctions {
128+
public function __construct() {
129+
function global_function() {
130+
return true;
131+
}
132+
}
133+
134+
public function __construct() {
135+
function global_function($foo) {
136+
return $foo;
137+
}
138+
}
139+
}
140+
141+
class NestedClosures {
142+
public function __construct() {
143+
do_something(
144+
function () {
145+
return true;
146+
},
147+
);
148+
}
149+
150+
public function __construct() {
151+
$this->callback = function () {
152+
return 10;
153+
};
154+
}
155+
}

0 commit comments

Comments
 (0)