Skip to content

Commit

Permalink
PHP-safe indentation replaced with Helpers::unindent() because requir…
Browse files Browse the repository at this point in the history
…es too much memory

Revert "implemented PHP-safe indentation"

This reverts commit 5eae391.
  • Loading branch information
dg committed Jun 19, 2020
1 parent 4662099 commit 7051954
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 274 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
],
"require": {
"php": ">=7.1",
"ext-tokenizer": "*",
"nette/utils": "^2.4.2 || ^3.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function dump($var, int $column = 0): string
private function dumpVar(&$var, array $parents = [], int $level = 0, int $column = 0): string
{
if ($var instanceof Literal) {
return ltrim(Helpers::indentPhp(trim((string) $var), $level), "\t");
return ltrim(Nette\Utils\Strings::indent(trim((string) $var), $level), "\t");

} elseif ($var === null) {
return 'null';
Expand Down
4 changes: 2 additions & 2 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private function loadMethodBodies(\ReflectionClass $from): array
/** @var Node\Stmt\ClassMethod $method */
if ($method->stmts) {
$body = $this->extractBody($nodeFinder, $code, $method->stmts);
$bodies[$method->name->toString()] = Helpers::indentPhp($body, -2);
$bodies[$method->name->toString()] = Helpers::unindent($body, 2);
}
}
return $bodies;
Expand All @@ -215,7 +215,7 @@ private function loadFunctionBody(\ReflectionFunction $from): string
});

$body = $this->extractBody($nodeFinder, $code, $function->stmts);
return Helpers::indentPhp($body, -1);
return Helpers::unindent($body, 1);
}


Expand Down
27 changes: 2 additions & 25 deletions src/PhpGenerator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,9 @@ public static function unformatDocComment(string $comment): string
}


public static function indentPhp(string $s, int $level = 1, string $chars = "\t"): string
public static function unindent(string $s, int $level = 1): string
{
$tbl = [];
$s = str_replace("\r\n", "\n", $s);

if ($level && strpos($s, "\n") !== false && preg_match('#\?>|<<<|"|\'#', $s)) {
static $save = [T_CONSTANT_ENCAPSED_STRING => 1, T_ENCAPSED_AND_WHITESPACE => 1, T_INLINE_HTML => 1, T_START_HEREDOC => 1, T_CLOSE_TAG => 1];
$tokens = token_get_all("<?php\n" . $s);
unset($tokens[0]);
$s = '';
foreach ($tokens as $token) {
if (isset($save[$token[0]]) && strpos($token[1], "\n") !== false) {
$s .= $id = "\00" . count($tbl) . "\00";
$tbl[$id] = $token[1];
} else {
$s .= is_array($token) ? $token[1] : $token;
}
}
}

if ($level > 0) {
$s = Nette\Utils\Strings::indent($s, $level, $chars);
} elseif ($level < 0) {
$s = preg_replace('#^(\t|\ \ \ \ ){1,' . (-$level) . '}#m', '', $s);
}
return strtr($s, $tbl);
return preg_replace('#^(\t|\ \ \ \ ){1,' . $level . '}#m', '', $s);
}


Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function setTypeResolving(bool $state = true): self
protected function indent(string $s): string
{
$s = str_replace("\t", $this->indentation, $s);
return Helpers::indentPhp($s, 1, $this->indentation);
return Strings::indent($s, 1, $this->indentation);
}


Expand Down
117 changes: 0 additions & 117 deletions tests/PhpGenerator/Helpers.indentPhp.phpt

This file was deleted.

29 changes: 29 additions & 0 deletions tests/PhpGenerator/Helpers.unindent.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Test: Nette\PhpGenerator\Helpers::unindent()
*/

declare(strict_types=1);

use Nette\PhpGenerator\Helpers;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::same('', Helpers::unindent('', 1));
Assert::same("\n", Helpers::unindent("\n", 1));
Assert::same('word', Helpers::unindent('word', 1));
Assert::same("\nword", Helpers::unindent("\nword", 1));
Assert::same("\nword\n", Helpers::unindent("\nword\n", 1));
Assert::same('word', Helpers::unindent("\tword", 1));
Assert::same("\tword", Helpers::unindent("\t\tword", 1));
Assert::same('word', Helpers::unindent("\t\tword", 2));
Assert::same("\nword", Helpers::unindent("\n\tword", 1));
Assert::same("word\t", Helpers::unindent("word\t", 1));
Assert::same("word\tword", Helpers::unindent("word\tword", 1));
Assert::same("word\t\nword", Helpers::unindent("word\t\nword", 1));
Assert::same('word', Helpers::unindent(' word', 1));
Assert::same(' word', Helpers::unindent(' word', 1));
125 changes: 0 additions & 125 deletions tests/PhpGenerator/Helpers.unindentPhp.phpt

This file was deleted.

5 changes: 3 additions & 2 deletions tests/PhpGenerator/expected/ClassType.from.bodies.expect
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ abstract class Class7
;
$s3 = "a\n\tb\n\t\tc"
;
// inline HTML is not supported
?>
a
b
a
b
c
<?php
}
Expand Down
1 change: 1 addition & 0 deletions tests/PhpGenerator/fixtures/class-body.phpf
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ a
c
DOC
;
// inline HTML is not supported
?>
a
b
Expand Down

0 comments on commit 7051954

Please sign in to comment.