Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use rocket\tests\lib\WebTestCase;

/**
* @no-named-arguments
*/
class WithoutNamespaceTest extends \PHPUnit\Framework\TestCase
{
public function testAuth(): void
{
[$headers, $body] = $this->httpGetRaw('/tinymce/auth');

self::assertArrayHasKey(0, $headers);
self::assertStringContainsString('200', $headers[0], 'response contained a proper http status-code');
}
}

?>
-----
<?php

declare(strict_types=1);

use rocket\tests\lib\WebTestCase;

/**
* @no-named-arguments
*/
class WithoutNamespaceTest extends \PHPUnit\Framework\TestCase
{
public function testAuth(): void
{
[$headers, $body] = $this->httpGetRaw('/tinymce/auth');

self::assertArrayHasKey(0, $headers);
self::assertStringContainsString('200', $headers[0], 'response contained a proper http status-code');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
Expand Down Expand Up @@ -83,7 +84,16 @@ public function beforeTraverse(array $nodes): ?array

// when first stmt is Declare_, verify if there is strict_types definition already,
// as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
if ($rootStmt instanceof FileWithoutNamespace) {
$currentNode = current($rootStmt->stmts);
if (! $currentNode instanceof Stmt) {
return null;
}

if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($currentNode)) {
return null;
}
} elseif ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
return null;
}

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

if ($rootStmt instanceof FileWithoutNamespace) {
$stmts = [$this->nodeFactory->createDeclaresStrictType()];
$stmts = array_merge($stmts, [new Nop()]);
$stmts = array_merge($stmts, $rootStmt->stmts);
$rootStmt->stmts = $stmts;

return [$rootStmt];
}

return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
}

Expand Down