Skip to content

Commit a083b2e

Browse files
authored
[CodeQuality] Handle crash on without namespace on DeclareStrictTypesTestsRector (#583)
1 parent 159ca62 commit a083b2e

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use rocket\tests\lib\WebTestCase;
4+
5+
/**
6+
* @no-named-arguments
7+
*/
8+
class WithoutNamespaceTest extends \PHPUnit\Framework\TestCase
9+
{
10+
public function testAuth(): void
11+
{
12+
[$headers, $body] = $this->httpGetRaw('/tinymce/auth');
13+
14+
self::assertArrayHasKey(0, $headers);
15+
self::assertStringContainsString('200', $headers[0], 'response contained a proper http status-code');
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
declare(strict_types=1);
24+
25+
use rocket\tests\lib\WebTestCase;
26+
27+
/**
28+
* @no-named-arguments
29+
*/
30+
class WithoutNamespaceTest extends \PHPUnit\Framework\TestCase
31+
{
32+
public function testAuth(): void
33+
{
34+
[$headers, $body] = $this->httpGetRaw('/tinymce/auth');
35+
36+
self::assertArrayHasKey(0, $headers);
37+
self::assertStringContainsString('200', $headers[0], 'response contained a proper http status-code');
38+
}
39+
}
40+
41+
?>

rules/CodeQuality/Rector/StmtsAwareInterface/DeclareStrictTypesTestsRector.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1414
use Rector\Contract\Rector\HTMLAverseRectorInterface;
1515
use Rector\PhpParser\Node\BetterNodeFinder;
16+
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
1617
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1718
use Rector\Rector\AbstractRector;
1819
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
@@ -83,7 +84,16 @@ public function beforeTraverse(array $nodes): ?array
8384

8485
// when first stmt is Declare_, verify if there is strict_types definition already,
8586
// as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
86-
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
87+
if ($rootStmt instanceof FileWithoutNamespace) {
88+
$currentNode = current($rootStmt->stmts);
89+
if (! $currentNode instanceof Stmt) {
90+
return null;
91+
}
92+
93+
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($currentNode)) {
94+
return null;
95+
}
96+
} elseif ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
8797
return null;
8898
}
8999

@@ -94,6 +104,15 @@ public function beforeTraverse(array $nodes): ?array
94104
$rectorWithLineChange = new RectorWithLineChange(self::class, $rootStmt->getStartLine());
95105
$this->file->addRectorClassWithLine($rectorWithLineChange);
96106

107+
if ($rootStmt instanceof FileWithoutNamespace) {
108+
$stmts = [$this->nodeFactory->createDeclaresStrictType()];
109+
$stmts = array_merge($stmts, [new Nop()]);
110+
$stmts = array_merge($stmts, $rootStmt->stmts);
111+
$rootStmt->stmts = $stmts;
112+
113+
return [$rootStmt];
114+
}
115+
97116
return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
98117
}
99118

0 commit comments

Comments
 (0)