Skip to content

Commit 7051954

Browse files
committed
PHP-safe indentation replaced with Helpers::unindent() because requires too much memory
Revert "implemented PHP-safe indentation" This reverts commit 5eae391.
1 parent 4662099 commit 7051954

File tree

10 files changed

+39
-274
lines changed

10 files changed

+39
-274
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
],
1717
"require": {
1818
"php": ">=7.1",
19-
"ext-tokenizer": "*",
2019
"nette/utils": "^2.4.2 || ^3.0"
2120
},
2221
"require-dev": {

src/PhpGenerator/Dumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function dump($var, int $column = 0): string
3838
private function dumpVar(&$var, array $parents = [], int $level = 0, int $column = 0): string
3939
{
4040
if ($var instanceof Literal) {
41-
return ltrim(Helpers::indentPhp(trim((string) $var), $level), "\t");
41+
return ltrim(Nette\Utils\Strings::indent(trim((string) $var), $level), "\t");
4242

4343
} elseif ($var === null) {
4444
return 'null';

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private function loadMethodBodies(\ReflectionClass $from): array
193193
/** @var Node\Stmt\ClassMethod $method */
194194
if ($method->stmts) {
195195
$body = $this->extractBody($nodeFinder, $code, $method->stmts);
196-
$bodies[$method->name->toString()] = Helpers::indentPhp($body, -2);
196+
$bodies[$method->name->toString()] = Helpers::unindent($body, 2);
197197
}
198198
}
199199
return $bodies;
@@ -215,7 +215,7 @@ private function loadFunctionBody(\ReflectionFunction $from): string
215215
});
216216

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

221221

src/PhpGenerator/Helpers.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,9 @@ public static function unformatDocComment(string $comment): string
6161
}
6262

6363

64-
public static function indentPhp(string $s, int $level = 1, string $chars = "\t"): string
64+
public static function unindent(string $s, int $level = 1): string
6565
{
66-
$tbl = [];
67-
$s = str_replace("\r\n", "\n", $s);
68-
69-
if ($level && strpos($s, "\n") !== false && preg_match('#\?>|<<<|"|\'#', $s)) {
70-
static $save = [T_CONSTANT_ENCAPSED_STRING => 1, T_ENCAPSED_AND_WHITESPACE => 1, T_INLINE_HTML => 1, T_START_HEREDOC => 1, T_CLOSE_TAG => 1];
71-
$tokens = token_get_all("<?php\n" . $s);
72-
unset($tokens[0]);
73-
$s = '';
74-
foreach ($tokens as $token) {
75-
if (isset($save[$token[0]]) && strpos($token[1], "\n") !== false) {
76-
$s .= $id = "\00" . count($tbl) . "\00";
77-
$tbl[$id] = $token[1];
78-
} else {
79-
$s .= is_array($token) ? $token[1] : $token;
80-
}
81-
}
82-
}
83-
84-
if ($level > 0) {
85-
$s = Nette\Utils\Strings::indent($s, $level, $chars);
86-
} elseif ($level < 0) {
87-
$s = preg_replace('#^(\t|\ \ \ \ ){1,' . (-$level) . '}#m', '', $s);
88-
}
89-
return strtr($s, $tbl);
66+
return preg_replace('#^(\t|\ \ \ \ ){1,' . $level . '}#m', '', $s);
9067
}
9168

9269

src/PhpGenerator/Printer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function setTypeResolving(bool $state = true): self
217217
protected function indent(string $s): string
218218
{
219219
$s = str_replace("\t", $this->indentation, $s);
220-
return Helpers::indentPhp($s, 1, $this->indentation);
220+
return Strings::indent($s, 1, $this->indentation);
221221
}
222222

223223

tests/PhpGenerator/Helpers.indentPhp.phpt

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Helpers::unindent()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\Helpers;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
Assert::same('', Helpers::unindent('', 1));
17+
Assert::same("\n", Helpers::unindent("\n", 1));
18+
Assert::same('word', Helpers::unindent('word', 1));
19+
Assert::same("\nword", Helpers::unindent("\nword", 1));
20+
Assert::same("\nword\n", Helpers::unindent("\nword\n", 1));
21+
Assert::same('word', Helpers::unindent("\tword", 1));
22+
Assert::same("\tword", Helpers::unindent("\t\tword", 1));
23+
Assert::same('word', Helpers::unindent("\t\tword", 2));
24+
Assert::same("\nword", Helpers::unindent("\n\tword", 1));
25+
Assert::same("word\t", Helpers::unindent("word\t", 1));
26+
Assert::same("word\tword", Helpers::unindent("word\tword", 1));
27+
Assert::same("word\t\nword", Helpers::unindent("word\t\nword", 1));
28+
Assert::same('word', Helpers::unindent(' word', 1));
29+
Assert::same(' word', Helpers::unindent(' word', 1));

tests/PhpGenerator/Helpers.unindentPhp.phpt

Lines changed: 0 additions & 125 deletions
This file was deleted.

tests/PhpGenerator/expected/ClassType.from.bodies.expect

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ abstract class Class7
6565
;
6666
$s3 = "a\n\tb\n\t\tc"
6767
;
68+
// inline HTML is not supported
6869
?>
69-
a
70-
b
70+
a
71+
b
7172
c
7273
<?php
7374
}

tests/PhpGenerator/fixtures/class-body.phpf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ a
7878
c
7979
DOC
8080
;
81+
// inline HTML is not supported
8182
?>
8283
a
8384
b

0 commit comments

Comments
 (0)