Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to preserve new lines in method body? #61

Open
matak opened this issue Apr 24, 2019 · 5 comments
Open

How to preserve new lines in method body? #61

matak opened this issue Apr 24, 2019 · 5 comments

Comments

@matak
Copy link
Contributor

matak commented Apr 24, 2019

Hi, wonderfull library, but after several hours, i realize that i cant use it.

I would like to use this library to scaffolding my code. With help of AutoIt I want to replace the whole code in text window with scaffolded dependency injection of use statements etc.

But the problem is, that it is not possible to preserve the new lines and whitespaces in the body of methods, is it true? Am I right?

So when i want to update this code

public function renderDefault()
{
	$stockinRow = $this->stockinRepositoryService->findById($this->stockin_id);
		
	$pageHeader = $this['pageHeader'];
	$pageHeader->setTitle("Naskladnění [" . $stockinRow->id . "] " . $stockinRow->dateArrival->format("d.m.Y") . " , profil dodavatele: " . $stockinRow->supplierProfile->name);

	$template = $this->template;
	$template->dataStockinItems = $this->stockinItemRepositoryService->selectAll()->where("stockin_id = ?", $this->stockin_id)->order("EAN ASC")->fetchAll();
}

I get this code,

public function renderDefault() {
	$stockinRow = $this->stockinRepositoryService->findById($this->stockin_id);
	$pageHeader = $this['pageHeader'];
	$pageHeader->setTitle("Naskladnění [" . $stockinRow->id . "] " . $stockinRow->dateArrival->format("d.m.Y") . " , profil dodavatele: " . $stockinRow->supplierProfile->name);
	$template = $this->template;
	$template->dataStockinItems = $this->stockinItemRepositoryService->selectAll()->where("stockin_id = ?", $this->stockin_id)->order("EAN ASC")->fetchAll();
}

so quite messy. Can I somehow prevent it?

As i searched in code, it seems that builder knows that originally the row was on line 31 and next row was on line 33, so there should be new line string.

@gossi
Copy link
Member

gossi commented Apr 24, 2019

For my understanding:

  1. You read in the first block of code into a model
  2. You regenerator the code from the model
  3. The regenerated code doesn't contain the blank lines of the body

Is that the problem you are describing? Can you provide your code generator config?

@matak
Copy link
Contributor Author

matak commented Apr 24, 2019

Yes exactly!

$phpClass = PhpClass::fromFile($filePath);

$codeGeneratorConfig = new CodeGeneratorConfig([
			'generateScalarTypeHints' => true,
			'generateReturnTypeHints' => true,
			'generateEmptyDocblock' => false,
			'enableSorting' => false,
			'useStatementSorting' => false,
			'constantSorting' => false,
			'propertySorting' => false,
			'methodSorting' => false,
]);

$generator = new CodeGenerator($codeGeneratorConfig);
return '<?php' . "\n\n" . $generator->generate($phpClass);

this is the most simple sample of my use case, the return is as i desribed in previous post

@gossi
Copy link
Member

gossi commented Apr 25, 2019

Ok, thanks. Could be a bug. Isn't even covered by tests, I'll try to tackle it on the weekend.

matak added a commit to matak/php-code-generator that referenced this issue Apr 25, 2019
This could be solution to phpowermove#61. This counts lines difference between stms and insert new line character for every empty line.
@matak
Copy link
Contributor Author

matak commented Apr 25, 2019

Maybe my pull request is not the best solution, maybe you could inject different PHP PrettyPrinter?

@gossi
Copy link
Member

gossi commented May 30, 2019

I merged your PR - thanks for that, it's on master. I'll keep this open, since tests for this are missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants