Skip to content

Commit

Permalink
Merge pull request #3 from shin1x1/use-statement-followed-by-namespac…
Browse files Browse the repository at this point in the history
…e-declaration

Change the position of the use statements to be added
  • Loading branch information
koriym authored Jul 4, 2020
2 parents 0edfacb + aa2ba21 commit dbbce07
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
40 changes: 40 additions & 0 deletions src/DeclareCollectVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Koriym\Spaceman;

use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;

class DeclareCollectVisitor extends NodeVisitorAbstract
{
/**
* @var Node\Stmt\Declare_[]
*/
public $declares = [];

public function enterNode(Node $node): void
{
if ($this->isTargetNode($node)) {
$this->declares[] = $node;
}
}

/**
* @param Node $node
* @return int|void
*/
public function leaveNode(Node $node)
{
if ($this->isTargetNode($node)) {
return NodeTraverser::REMOVE_NODE;
}
}

private function isTargetNode(Node $node): bool
{
return $node instanceof Node\Stmt\Declare_;
}
}
35 changes: 19 additions & 16 deletions src/Spaceman.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function __invoke(string $code, string $namespace) : string
if ($this->hasNamespace($oldStmts)) {
return '';
}
$newStmts = $this->addNamespace($this->resolveName($newStmts), $namespace);

[$newStmts, $declareStmts, $useStmt] = $this->resolveName($newStmts);
$newStmts = $this->addNamespace($newStmts, $declareStmts, $useStmt, $namespace);
assert_options(ASSERT_ACTIVE, 0);
$code = (new Standard)->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
assert_options(ASSERT_ACTIVE, 1);
Expand Down Expand Up @@ -54,14 +56,15 @@ private function getAstToken(string $code) : array
/**
* @return Node[]
*/
private function addNamespace(array $ast, string $namespace) : array
private function addNamespace(array $ast, array $declareStmts, Node\Stmt $useStmt, string $namespace) : array
{
$factory = new BuilderFactory;
$node = $factory->namespace($namespace)
->addStmts($ast)
->getNode();
$nodes = count($declareStmts) > 0 ? $declareStmts : [];

$nodes[] = (new BuilderFactory())->namespace($namespace)->getNode();
$nodes[] = $useStmt;
$nodes = array_merge($nodes, $ast);

return [$node];
return $nodes;
}

private function hasNamespace(array $ast) : bool
Expand All @@ -75,7 +78,7 @@ private function hasNamespace(array $ast) : bool
}

/**
* @return Node[]
* @return array{Node[], Node\Stmt\Declare_[], Node\Stmt}
*/
private function resolveName($ast) : array
{
Expand All @@ -87,26 +90,26 @@ private function resolveName($ast) : array
$nodeTraverser->addVisitor($nameResolver);
$watchVisitor = new GlobalNameClassWatchVisitor;
$nodeTraverser->addVisitor($watchVisitor);
$declareVisitor = new DeclareCollectVisitor();
$nodeTraverser->addVisitor($declareVisitor);
$travesedAst = $nodeTraverser->traverse($ast);

return $this->importGlobalClass(array_unique($watchVisitor->globalClassNames), $travesedAst);
$useStmt = $this->createUseStmt(array_unique($watchVisitor->globalClassNames));

return [$travesedAst, $declareVisitor->declares, $useStmt];
}

/**
* @param list<class-string> $globalClassNames
* @return Node\Stmt\Nop|Node\Stmt\Use_
*/
private function importGlobalClass(array $globalClassNames, array $ast) : array
private function createUseStmt(array $globalClassNames) : Node\Stmt
{
$useUse = [];
foreach ($globalClassNames as $name) {
$useUse[] = new Node\Stmt\UseUse(new Node\Name($name));
}
if ($globalClassNames) {
$use = new Node\Stmt\Use_($useUse);
array_push($ast, $use);
}

return $ast;
return $useUse ? new Node\Stmt\Use_($useUse) : new Node\Stmt\Nop();
}

private function addPhpEol(string $code) : string
Expand Down
1 change: 1 addition & 0 deletions tests/Fake/Fake.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);
use Foo\Bar;

class Fake
Expand Down
4 changes: 3 additions & 1 deletion tests/SpacemanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public function test__invoke() : void
$expected = /** @lang php */<<<EOT
<?php
declare(strict_types=1);
namespace Newname\Space;
use Author, LogicException;
use Foo\Bar;
class Fake
{
public function run()
Expand All @@ -47,7 +50,6 @@ public function run()
new LogicException;
}
}
use Author, LogicException;
EOT;
$this->assertSame($expected, $sourceCode);
Expand Down

0 comments on commit dbbce07

Please sign in to comment.