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
8 changes: 5 additions & 3 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,10 @@ private function updateSourceCodeFromNewStmts()
$this->oldTokens
);

// this fake property is a placeholder for a linebreak
$newCode = str_replace([' private $__EXTRA__LINE;', 'use __EXTRA__LINE;', ' $__EXTRA__LINE;'], '', $newCode);
// replace the 3 "fake" items that may be in the code (allowing for different indentation)
$newCode = preg_replace('/(\ |\t)*private\ \$__EXTRA__LINE;/', '', $newCode);
$newCode = preg_replace('/use __EXTRA__LINE;/', '', $newCode);
$newCode = preg_replace('/(\ |\t)*\$__EXTRA__LINE;/', '', $newCode);

// process comment lines
foreach ($this->pendingComments as $i => $comment) {
Expand Down Expand Up @@ -829,7 +831,7 @@ private function addMethod(Node\Stmt\ClassMethod $methodNode)
$newStatements[] = $methodNode;

if (null === $existingIndex) {
// just them on the end!
// add them to the end!

$classNode->stmts = array_merge($classNode->stmts, $newStatements);
} else {
Expand Down
26 changes: 26 additions & 0 deletions src/Util/PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
*/
final class PrettyPrinter extends Standard
{
/**
* Overridden to fix indentation problem with tabs.
*
* If the original source code uses tabs, then the tokenizer
* will see this as "1" indent level, and will indent new lines
* with just 1 space. By changing 1 indent to 4, we effectively
* "correct" this problem when printing.
*
* For code that is even further indented (e.g. 8 spaces),
* the printer uses the first indentation (here corrected
* from 1 space to 4) and already (without needing any other
* changes) adds 4 spaces onto that. This is why we don't
* also need to handle indent levels of 5, 9, etc: these
* do not occur (at least in the code we generate);
*
* @param int $level
*/
protected function setIndentLevel(int $level)
{
if (1 === $level) {
$level = 4;
}

parent::setIndentLevel($level);
}

/**
* Overridden to change coding standards.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,22 @@ public function getAddOneToOneRelationTests()
->setIsOwning(true)
];
}

public function testGenerationWithTabs()
{
$source = file_get_contents(__DIR__.'/fixtures/source/ProductWithTabs.php');
$expectedSource = file_get_contents(__DIR__.'/fixtures/with_tabs/ProductWithTabs.php');

$manipulator = new ClassSourceManipulator($source);

$method = (new \ReflectionObject($manipulator))->getMethod('addProperty');
$method->setAccessible(true);
$method->invoke($manipulator, 'name', ['@ORM\Column(type="string", length=255)']);

$method = (new \ReflectionObject($manipulator))->getMethod('addGetter');
$method->setAccessible(true);
$method->invoke($manipulator, 'id', 'int', false);

$this->assertSame($expectedSource, $manipulator->getSourceCode());
}
}
18 changes: 18 additions & 0 deletions tests/Util/fixtures/source/ProductWithTabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
}
28 changes: 28 additions & 0 deletions tests/Util/fixtures/with_tabs/ProductWithTabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=255)
*/
private $name;

public function getId(): int
{
return $this->id;
}
}