Skip to content

Commit c1f61f8

Browse files
committed
added Helpers::tabsToSpaces()
1 parent 4567783 commit c1f61f8

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ class Demo
148148
}
149149
```
150150

151+
Tabs versus spaces
152+
------------------
153+
154+
The generated code uses tabs for indentation, which makes it very easy to change it to any number of spaces:
155+
156+
```php
157+
use Nette\PhpGenerator\Helpers;
158+
159+
$class = new Nette\PhpGenerator\ClassType('Demo');
160+
// ...
161+
162+
echo Helpers::tabsToSpaces((string) $class); // 4 spaces indentation
163+
echo Helpers::tabsToSpaces((string) $class, 2); // 2 spaces indentation
164+
```
165+
151166
Literals
152167
--------
153168

src/PhpGenerator/Helpers.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,10 @@ public static function extractShortName(string $name): string
261261
{
262262
return ($pos = strrpos($name, '\\')) === false ? $name : substr($name, $pos + 1);
263263
}
264+
265+
266+
public static function tabsToSpaces(string $s, int $count = self::INDENT_LENGTH): string
267+
{
268+
return str_replace("\t", str_repeat(' ', $count), $s);
269+
}
264270
}

tests/PhpGenerator/Helpers.dump().phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Assert::same('false', Helpers::dump(false));
2929

3030
Assert::same("''", Helpers::dump(''));
3131
Assert::same("'Hello'", Helpers::dump('Hello'));
32+
Assert::same('"\t\n\t"', Helpers::dump("\t\n\t"));
3233
Assert::same("'I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n'", Helpers::dump("I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n")); // Iñtërnâtiônàlizætiøn
3334
Assert::same('"\rHello \$"', Helpers::dump("\rHello $"));
3435
Assert::same("'He\\llo'", Helpers::dump('He\llo'));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\Helpers;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::same('', Helpers::tabsToSpaces(''));
13+
Assert::same("\n \n ", Helpers::tabsToSpaces("\n \n "));
14+
Assert::same("\n a\n b", Helpers::tabsToSpaces("\n\t\ta\n\tb"));
15+
Assert::same("\n a\n b\n", Helpers::tabsToSpaces("\n\t\ta\n\tb\n"));
16+
Assert::same("\n a\n b\n", Helpers::tabsToSpaces("\n\t\ta\n\tb\n", 2));

0 commit comments

Comments
 (0)