-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndentWithTabsFixer.php
49 lines (42 loc) · 1.07 KB
/
IndentWithTabsFixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Symfony\CS\Fixer\Contrib;
use Symfony\CS\Fixer\PSR2\IndentationFixer;
use Symfony\CS\Tokenizer\Tokens;
class IndentWithTabsFixer extends IndentationFixer
{
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, $content)
{
$content = parent::fix($file, $content);
$tokens = Tokens::fromCode($content);
foreach ($tokens as $index => $token) {
if ($token->isWhitespace() || $token->isComment()) {
$lines = preg_split('/(\R)/', $token->getContent(), -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($lines as &$line) {
$line = preg_replace_callback('/^( {4,})/', function ($matches) {
return str_replace(' ', "\t", $matches[0]);
}, $line);
}
$tokens[$index]->setContent(implode('', $lines));
}
}
return $tokens->generateCode();
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
return 'Code MUST use tabs for indenting, and MUST NOT use spaces.';
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
// will be run almost last (just before PSR2 EofEndingFixer)
return -49;
}
}