Skip to content

Commit

Permalink
do not override endline
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 26, 2020
1 parent 02a01e2 commit 6c0bc2d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
34 changes: 11 additions & 23 deletions src/PhpParser/Node/Manipulator/ClassInsertManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\TraitUse;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -50,17 +49,6 @@ public function addAsFirstMethod(Class_ $class, Stmt $stmt): void
$class->stmts[] = $stmt;
}

/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function insertBeforeAndFollowWithNewline(array $nodes, Stmt $stmt, int $key): array
{
$nodes = $this->insertBefore($nodes, $stmt, $key);

return $this->insertBefore($nodes, new Nop(), $key);
}

public function addConstantToClass(Class_ $class, string $constantName, ClassConst $classConst): void
{
if ($this->hasClassConstant($class, $constantName)) {
Expand All @@ -87,6 +75,17 @@ public function addAsFirstTrait(Class_ $class, string $traitName): void
$this->addStatementToClassBeforeTypes($class, $trait, TraitUse::class, Property::class, ClassMethod::class);
}

/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function insertBefore(array $nodes, Stmt $stmt, int $key): array
{
array_splice($nodes, $key, 0, [$stmt]);

return $nodes;
}

private function tryInsertBeforeFirstMethod(Class_ $classNode, Stmt $stmt): bool
{
foreach ($classNode->stmts as $key => $classStmt) {
Expand Down Expand Up @@ -117,17 +116,6 @@ private function tryInsertAfterLastProperty(Class_ $classNode, Stmt $stmt): bool
return false;
}

/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
private function insertBefore(array $nodes, Stmt $stmt, int $key): array
{
array_splice($nodes, $key, 0, [$stmt]);

return $nodes;
}

private function hasClassConstant(Class_ $class, string $constantName)
{
foreach ($class->getConstants() as $classConst) {
Expand Down
10 changes: 6 additions & 4 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori

$content = parent::printFormatPreserving($stmts, $origStmts, $origTokens);

// php attributes
$content = $this->contentPhpAttributePlaceholderReplacer->decorateContent($content);
// add new line in case of added stmts
if (count($stmts) !== count($origStmts) && ! (bool) Strings::match($content, "#\n$#")) {
$content .= $this->nl;
}

// keep EOL
return rtrim($content) . $this->nl;
// php attributes
return $this->contentPhpAttributePlaceholderReplacer->decorateContent($content);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/Namespace_/PseudoNamespaceToNamespaceRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function afterTraverse(array $nodes): array
$namespaceNode = new Namespace_(new Name($this->newNamespace));
foreach ($nodes as $key => $node) {
if ($node instanceof Use_ || $node instanceof Class_) {
$nodes = $this->classInsertManipulator->insertBeforeAndFollowWithNewline($nodes, $namespaceNode, $key);
$nodes = $this->classInsertManipulator->insertBefore($nodes, $namespaceNode, $key);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

class PHPUnit_Abc
{
public function run()
{
return 1;
}
}

?>
-----
<?php


namespace PHPUnit;

class Abc
{
public function run()
{
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Rector_Core_Tests_Rector_Namespace__PseudoNamespaceToNamespaceRector_Fixtu
-----
<?php


namespace Rector\Core\Tests\Rector\Namespace_\PseudoNamespaceToNamespaceRector\Fixture;

use Rector\Core\Tests\Rector\Namespace_\PseudoNamespaceToNamespaceRector\Source\Keep_This;
Expand Down

0 comments on commit 6c0bc2d

Please sign in to comment.