Skip to content

Commit dc37a35

Browse files
committed
fix(file): Improve function statement sorting logic for better accuracy
- Adds a check to ensure all function names are in sorted order before processing - Implements a reduce method to insert Nop statements when function statements overlap or are out of order - Enhances code clarity and maintains proper statement sequence for file sorting functionality
1 parent 48cb12c commit dc37a35

File tree

8 files changed

+41
-6
lines changed

8 files changed

+41
-6
lines changed

baselines/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 12 errors
1+
# total 13 errors
22

33
includes:
44
- complexity.functionLike.neon

baselines/method.nonObject.neon

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3 errors
1+
# total 4 errors
22

33
parameters:
44
ignoreErrors:
@@ -7,6 +7,11 @@ parameters:
77
count: 1
88
path: ../src/Rector/Array_/SimplifyListIndexRector.php
99

10+
-
11+
message: '#^Cannot call method all\(\) on mixed\.$#'
12+
count: 1
13+
path: ../src/Rector/File/SortFileFunctionStmtRector.php
14+
1015
-
1116
message: '#^Cannot call method all\(\) on mixed\.$#'
1217
count: 1

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@
375375
"rector-rules:list-files": "Guanguans\\RectorRules\\Support\\ComposerScripts::listFiles",
376376
"rector:custom-rule": "@rector custom-rule",
377377
"rector:list-rules": "@rector list-rules",
378-
"rector:process": "@rector process",
378+
"rector:process": "@rector process --ansi",
379379
"rector:process-clear-cache": "@rector:process --clear-cache",
380380
"rector:process-clear-cache-dry-run": "@rector:process-clear-cache --dry-run",
381381
"rector:process-dry-run": "@rector:process --dry-run",

docs/rules-overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ Sort file function stmt
266266

267267
-function c(): void {}
268268
+function a(): void {}
269+
269270
function b(): void {}
271+
270272
-function a(): void {}
271273
+function c(): void {}
272274
```

src/Rector/File/SortFileFunctionStmtRector.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Guanguans\RectorRules\Rector\File;
1616

1717
use Guanguans\RectorRules\Rector\AbstractRector;
18+
use Illuminate\Support\Collection;
1819
use PhpParser\Node;
1920
use PhpParser\Node\Arg;
2021
use PhpParser\Node\Expr\BooleanNot;
@@ -26,6 +27,7 @@
2627
use PhpParser\Node\Stmt\Function_;
2728
use PhpParser\Node\Stmt\If_;
2829
use PhpParser\Node\Stmt\Namespace_;
30+
use PhpParser\Node\Stmt\Nop;
2931
use Rector\PhpParser\Node\FileNode;
3032
use Rector\PhpParser\Node\Value\ValueResolver;
3133
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -70,6 +72,10 @@ public function refactor(Node $node): ?Node
7072
|| !collect($rootNode->stmts)->containsStrict(
7173
fn (Stmt $stmtNode): ?string => $this->parseFuncName($stmtNode)
7274
)
75+
|| collect($rootNode->stmts)
76+
->map(fn (Stmt $stmtNode): ?string => $this->parseFuncName($stmtNode))
77+
->filter()
78+
->pipe(static fn (Collection $funcNames): bool => $funcNames->all() === $funcNames->sort()->all())
7379
) {
7480
return null;
7581
}
@@ -81,7 +87,19 @@ public function refactor(Node $node): ?Node
8187
? $aName <=> $bName
8288
: 0
8389
)
84-
// ->values()
90+
->reduce(
91+
static function (Collection $stmtNodes, Stmt $stmtNode): Collection {
92+
if (
93+
($prevStmtNode = $stmtNodes->last()) instanceof Stmt
94+
&& $stmtNode->getStartLine() <= $prevStmtNode->getEndLine()
95+
) {
96+
$stmtNodes->push(new Nop);
97+
}
98+
99+
return $stmtNodes->push($stmtNode);
100+
},
101+
collect()
102+
)
85103
->all();
86104

87105
if ($rootNode->stmts === $sortedStmts) {
@@ -105,15 +123,19 @@ protected function codeSamples(): array
105123
namespace Guanguans\RectorRulesTests\Rector\File\SortFileFunctionStmtRector\Fixture;
106124
107125
function c(): void {}
126+
108127
function b(): void {}
128+
109129
function a(): void {}
110130
PHP,
111131
<<<'PHP'
112132
/** @noinspection ALL */
113133
namespace Guanguans\RectorRulesTests\Rector\File\SortFileFunctionStmtRector\Fixture;
114134
115135
function a(): void {}
136+
116137
function b(): void {}
138+
117139
function c(): void {}
118140
PHP
119141
),

tests/Pest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
| "expect()" function gives you access to a set of "expectations" methods that you can use
6060
| to assert different things. Of course, you may extend the Expectation API at any time.
6161
|
62-
*/
62+
*/
6363

6464
/**
6565
* @see expect()->toBetween()
@@ -92,7 +92,7 @@ function (Closure $assertions): Expectation {
9292
| project that you don't want to repeat in every file. Here you can also expose helpers as
9393
| global functions to help you to reduce the number of lines of code in your test files.
9494
|
95-
*/
95+
*/
9696

9797
/**
9898
* @param class-string|object $class

tests/Rector/File/SortFileFunctionStmtRector/Fixture/fixture.php.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
namespace Guanguans\RectorRulesTests\Rector\File\SortFileFunctionStmtRector\Fixture;
55

66
function c(): void {}
7+
78
function b(): void {}
9+
810
function a(): void {}
911

1012
?>
@@ -15,7 +17,9 @@ function a(): void {}
1517
namespace Guanguans\RectorRulesTests\Rector\File\SortFileFunctionStmtRector\Fixture;
1618

1719
function a(): void {}
20+
1821
function b(): void {}
22+
1923
function c(): void {}
2024

2125
?>

tests/Rector/File/SortFileFunctionStmtRector/Fixture/fixture_if_function_exists.php.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ if (!function_exists('Guanguans\RectorRulesTests\Rector\File\SortFileFunctionStm
3535
{
3636
}
3737
}
38+
3839
function b(): void
3940
{
4041
}
42+
4143
if (!function_exists('Guanguans\RectorRulesTests\Rector\File\SortFileFunctionStmtRector\Fixture\c')) {
4244
function c(): void
4345
{

0 commit comments

Comments
 (0)