Skip to content

Commit acb64bc

Browse files
committed
added Helpers::tabsToSpaces()
1 parent 48f1346 commit acb64bc

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ class Demo
137137
}
138138
```
139139

140+
Tabs versus spaces
141+
------------------
142+
143+
The generated code uses tabs for indentation, which makes it very easy to change it to any number of spaces:
144+
145+
```php
146+
use Nette\PhpGenerator\Helpers;
147+
148+
$class = new Nette\PhpGenerator\ClassType('Demo');
149+
// ...
150+
151+
echo Helpers::tabsToSpaces((string) $class); // 4 spaces indentation
152+
echo Helpers::tabsToSpaces((string) $class, 2); // 2 spaces indentation
153+
```
154+
155+
140156
Literals
141157
--------
142158

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)