1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Tools\Php\LineAfterOpenTag; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\Fs\File; |
6
|
|
|
use Funivan\Cs\Report\Report; |
7
|
|
|
use Funivan\PhpTokenizer\Token; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class LineAfterOpenTagFixer extends AbstractLineAfterOpenTag { |
13
|
|
|
|
14
|
|
|
const NAME_FIXER = 'php_line_after_open_tag_fixer'; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @inheritdoc |
19
|
|
|
*/ |
20
|
|
|
public function getName() { |
21
|
|
|
return self::NAME_FIXER; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public function getDescription() { |
29
|
|
|
return 'Set one empty line after php open tag'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param File $file |
35
|
|
|
* @param Report $report |
36
|
|
|
* @void |
37
|
|
|
*/ |
38
|
8 |
|
public function process(File $file, Report $report) { |
39
|
|
|
|
40
|
8 |
|
$collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get()); |
41
|
8 |
|
$items = $this->getInvalidStartTokens($collection); |
42
|
8 |
|
if (count($items) === 0) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
8 |
|
foreach ($items as $tokenInfo) { |
47
|
|
|
/** @var Token $token */ |
48
|
8 |
|
$whitespace = $tokenInfo->getWhitespace(); |
49
|
|
|
|
50
|
8 |
|
$token = $tokenInfo->getToken(); |
51
|
8 |
|
$tokenValue = $token->getValue(); |
52
|
8 |
|
$whitespaceValue = $whitespace->getValue() ? $whitespace->getValue() : ''; |
53
|
8 |
|
$whitespace->remove(); |
54
|
|
|
|
55
|
8 |
|
preg_match('!([ ]+)$!', $whitespaceValue, $endSpaces); |
56
|
|
|
|
57
|
8 |
|
$whitespaceValue = !empty($endSpaces[1]) ? $endSpaces[1] : ''; |
58
|
|
|
|
59
|
8 |
|
$lines = explode("\n", $tokenValue); |
60
|
8 |
|
$tokenValue = reset($lines); |
61
|
|
|
|
62
|
|
|
|
63
|
8 |
|
$append = $tokenValue . "\n\n" . $whitespaceValue; |
64
|
|
|
|
65
|
8 |
|
$token->setValue($append); |
66
|
8 |
|
$report->addMessage($file, $this, 'Set one empty line after php open tag', $token->getLine()); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
$file->getContent()->set($collection->assemble()); |
70
|
8 |
|
} |
71
|
|
|
|
72
|
|
|
} |