1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Marco Muths |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Squeeze; |
27
|
|
|
|
28
|
|
|
use PhpParser\Comment; |
29
|
|
|
use PhpParser\Node; |
30
|
|
|
use PhpParser\PrettyPrinter\Standard; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @SuppressWarnings("PMD") |
34
|
|
|
*/ |
35
|
|
|
class Printer extends Standard |
36
|
|
|
{ |
37
|
|
|
/** @var bool */ |
38
|
|
|
private $hasCommentSupport = true; |
39
|
|
|
|
40
|
|
|
public function disableComments() |
41
|
|
|
{ |
42
|
|
|
$this->hasCommentSupport = false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function prettyPrintFile(array $stmts) |
46
|
|
|
{ |
47
|
|
|
$p = rtrim($this->prettyPrint($stmts)); |
48
|
|
|
|
49
|
|
|
$p = preg_replace('/^\?>\n?/', '', $p, -1); |
50
|
|
|
$p = preg_replace('/<\?php$/', '', $p); |
51
|
|
|
|
52
|
|
|
if (!$this->findNamespaceIn($stmts)) { |
53
|
|
|
$p = 'namespace {' . $p . '}'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $p; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $stmts |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
private function findNamespaceIn(array $stmts) |
64
|
|
|
{ |
65
|
|
|
foreach ($stmts as $stmt) { |
66
|
|
|
if ($stmt instanceof \PhpParser\Node\Stmt\Namespace_) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function preprocessNodes(array $nodes) |
75
|
|
|
{ |
76
|
|
|
$this->canUseSemicolonNamespaces = false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function pStmts(array $nodes, $indent = true) |
80
|
|
|
{ |
81
|
|
|
$result = ''; |
82
|
|
|
foreach ($nodes as $node) { |
83
|
|
|
/** @var Node $node */ |
84
|
|
|
$result .= "\n" |
85
|
|
|
. $this->pComments($node->getAttribute('comments', array())) |
86
|
|
|
. $this->p($node) |
87
|
|
|
. ($node instanceof Node\Expr ? ';' : ''); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "", $result); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function pComments(array $comments) |
94
|
|
|
{ |
95
|
|
|
$result = ''; |
96
|
|
|
|
97
|
|
|
if (!$this->hasCommentSupport) { |
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($comments as $comment) { |
102
|
|
|
/** @var Comment $comment */ |
103
|
|
|
$text = $comment->getReformattedText(); |
104
|
|
|
if (strpos($text, '//') === 0) { |
105
|
|
|
$text = str_replace(array('/*', '*/'), '', $text); |
106
|
|
|
$text = '/*' . substr($text, 2) . '*/'; |
107
|
|
|
} |
108
|
|
|
$result .= $text . "\n"; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|