Skip to content

Commit 014ed7a

Browse files
committed
added Helpers::tabsToSpaces()
1 parent 84bea01 commit 014ed7a

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ class Demo
132132
}
133133
```
134134

135+
Tabs versus spaces
136+
------------------
137+
138+
The generated code uses tabs for indentation, which makes it very easy to change it to any number of spaces:
139+
140+
```php
141+
use Nette\PhpGenerator\Helpers;
142+
143+
$class = new Nette\PhpGenerator\ClassType('Demo');
144+
// ...
145+
146+
echo Helpers::tabsToSpaces((string) $class); // 4 spaces indentation
147+
echo Helpers::tabsToSpaces((string) $class, 2); // 2 spaces indentation
148+
```
149+
150+
135151
Literals
136152
--------
137153

src/PhpGenerator/Helpers.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,15 @@ public static function extractShortName($name)
285285
{
286286
return ($pos = strrpos($name, '\\')) === false ? $name : substr($name, $pos + 1);
287287
}
288+
289+
290+
/**
291+
* @param string
292+
* @param int
293+
* @return string
294+
*/
295+
public static function tabsToSpaces($s, $count = self::INDENT_LENGTH)
296+
{
297+
return str_replace("\t", str_repeat(' ', $count), $s);
298+
}
288299
}

tests/PhpGenerator/Helpers.dump().phpt

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

2828
Assert::same("''", Helpers::dump(''));
2929
Assert::same("'Hello'", Helpers::dump('Hello'));
30+
Assert::same('"\t\n\t"', Helpers::dump("\t\n\t"));
3031
Assert::same("'I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0liz\xc3\xa6ti\xc3\xb8n'", Helpers::dump("I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0liz\xc3\xa6ti\xc3\xb8n")); // Iñtërnâtiônàlizætiøn
3132
Assert::same('"\rHello \$"', Helpers::dump("\rHello $"));
3233
Assert::same("'He\\llo'", Helpers::dump('He\llo'));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Nette\PhpGenerator\Helpers;
4+
use Tester\Assert;
5+
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
Assert::same('', Helpers::tabsToSpaces(''));
11+
Assert::same("\n \n ", Helpers::tabsToSpaces("\n \n "));
12+
Assert::same("\n a\n b", Helpers::tabsToSpaces("\n\t\ta\n\tb"));
13+
Assert::same("\n a\n b\n", Helpers::tabsToSpaces("\n\t\ta\n\tb\n"));
14+
Assert::same("\n a\n b\n", Helpers::tabsToSpaces("\n\t\ta\n\tb\n", 2));

0 commit comments

Comments
 (0)