2
2
3
3
/*
4
4
* This file is part of the PHPDoc Formatter application.
5
+ * https://github.com/SinSquare/phpdoc-formatter
5
6
*
6
7
* (c) Ábel Katona
7
8
*
12
13
13
14
use Symfony \Component \Stopwatch \Stopwatch ;
14
15
16
+ /**
17
+ * @author Abel Katona
18
+ */
15
19
class Application
16
20
{
17
21
private $ config ;
18
22
23
+ /**
24
+ * @param Config $config
25
+ *
26
+ * @throws \Exception if the config is not valid
27
+ */
19
28
public function __construct (Config $ config )
20
29
{
30
+ $ config ->validate ();
21
31
$ this ->config = $ config ;
22
32
}
23
33
34
+ /**
35
+ * Fix all the files that the finder returns.
36
+ */
24
37
public function fixFiles ()
25
38
{
26
39
$ stopwatch = new Stopwatch ();
@@ -48,18 +61,11 @@ public function fixFiles()
48
61
49
62
$ norm = $ this ->getDocBody ($ value );
50
63
$ norm = $ this ->normalizeDocBody ($ norm );
51
- $ norm = $ this ->formatDocBody ($ norm , $ ident );
64
+ $ norm = $ this ->reconstructDocBody ($ norm , $ ident );
52
65
$ docComments [$ key ]['formatted ' ] = $ norm ;
53
66
}
54
67
55
- $ newFile = '' ;
56
- $ offset = 0 ;
57
- foreach ($ docComments as $ key => $ value ) {
58
- $ newFile .= substr ($ content , $ offset , $ value ['offset ' ] - $ offset );
59
- $ offset = $ value ['offset ' ] + $ value ['length ' ];
60
- $ newFile .= $ value ['formatted ' ];
61
- }
62
- $ newFile .= substr ($ content , $ offset );
68
+ $ newFile = $ this ->reconstructFile ($ file , $ docComments );
63
69
64
70
if (sha1 ($ content ) !== sha1 ($ newFile )) {
65
71
$ d = file_put_contents ($ file , $ newFile );
@@ -72,6 +78,31 @@ public function fixFiles()
72
78
}
73
79
}
74
80
81
+ /**
82
+ * @param string $file File content
83
+ * @param string[] $docComments
84
+ */
85
+ private function reconstructFile (string $ content , array $ docComments )
86
+ {
87
+ $ newFile = '' ;
88
+ $ offset = 0 ;
89
+ foreach ($ docComments as $ key => $ value ) {
90
+ $ newFile .= substr ($ content , $ offset , $ value ['offset ' ] - $ offset );
91
+ $ offset = $ value ['offset ' ] + $ value ['length ' ];
92
+ $ newFile .= $ value ['formatted ' ];
93
+ }
94
+ $ newFile .= substr ($ content , $ offset );
95
+
96
+ return $ newFile ;
97
+ }
98
+
99
+ /**
100
+ * Gets the file's dominant line ending.
101
+ *
102
+ * @param string $file File content
103
+ *
104
+ * @return string Dominant line ending
105
+ */
75
106
private function getFileDominantLineEnding (string $ file )
76
107
{
77
108
static $ eols = array (
@@ -88,33 +119,47 @@ private function getFileDominantLineEnding(string $file)
88
119
"\0x0A " , // [ASCII] LF: Multics, Unix, Unix-like, BeOS, Amiga, RISC OS
89
120
"\0x0D " , // [ASCII] CR: Commodore 8-bit, BBC Acorn, TRS-80, Apple II, Mac OS <=v9, OS-9
90
121
"\0x1E " , // [ASCII] RS: QNX (pre-POSIX)
91
- //"\0x76", // [?????] NEWLINE: ZX80, ZX81 [DEPRECATED]
92
122
"\0x15 " , // [EBCDEIC] NEL: OS/390, OS/400
93
123
);
94
124
$ cur_cnt = 0 ;
95
125
$ cur_eol = "\n" ;
96
- foreach ($ eols as $ eol ){
97
- if (($ count = substr_count ($ file , $ eol )) > $ cur_cnt ){
126
+ foreach ($ eols as $ eol ) {
127
+ if (($ count = substr_count ($ file , $ eol )) > $ cur_cnt ) {
98
128
$ cur_cnt = $ count ;
99
129
$ cur_eol = $ eol ;
100
130
}
101
131
}
132
+
102
133
return $ cur_eol ;
103
134
}
104
135
136
+ /**
137
+ * Gets the opening tag's ident.
138
+ *
139
+ * @param string $doc The whole unformatted PHPDoc block
140
+ *
141
+ * @return string The whitespace before the opening tag
142
+ */
105
143
private function getDocBodyIdent (string $ doc )
106
144
{
107
145
$ lines = explode ("\n" , $ doc );
108
146
109
147
foreach ($ lines as $ line ) {
110
- if (preg_match ("#/\*\* # " , $ line , $ matches, PREG_OFFSET_CAPTURE )) {
111
- return $ matches [0 ][ 1 ];
148
+ if (preg_match ("#([^ /\*]*)/\*\* # " , $ line , $ matches )) {
149
+ return $ matches [1 ];
112
150
}
113
151
}
114
152
115
153
return null ;
116
154
}
117
155
156
+ /**
157
+ * Gets the body of the PHPDoc without the opening tags.
158
+ *
159
+ * @param string $doc The whole unformatted PHPDoc block
160
+ *
161
+ * @return string
162
+ */
118
163
private function getDocBody (string $ doc )
119
164
{
120
165
$ lines = explode ("\n" , $ doc );
@@ -136,6 +181,13 @@ private function getDocBody(string $doc)
136
181
return $ lines ;
137
182
}
138
183
184
+ /**
185
+ * Fixing the idention of the body.
186
+ *
187
+ * @param string $doc The PHPDoc body without the opening tags
188
+ *
189
+ * @return string
190
+ */
139
191
private function normalizeDocBody (string $ doc )
140
192
{
141
193
$ lines = explode ("\n" , $ doc );
@@ -159,7 +211,7 @@ private function normalizeDocBody(string $doc)
159
211
}
160
212
161
213
$ ident += $ c ;
162
- if ($ ident < 0 ) {
214
+ if ($ ident < 0 ) {
163
215
//TODO warning - possible open-close tag mismatch
164
216
$ ident = 0 ;
165
217
}
@@ -168,24 +220,39 @@ private function normalizeDocBody(string $doc)
168
220
return implode ("\n" , $ lines );
169
221
}
170
222
171
- private function formatDocBody (string $ doc , int $ ident )
223
+ /**
224
+ * Reconstruct the the body (adding opening and closing tag with correct idention).
225
+ *
226
+ * @param string $doc The PHPDoc body without the opening tags
227
+ * @param string $ident The opening tag's ident
228
+ *
229
+ * @return string The reconstructed PHPDoc
230
+ */
231
+ private function reconstructDocBody (string $ doc , string $ ident )
172
232
{
173
233
$ newLine = $ this ->config ->getNewLine ();
174
234
$ lines = explode ("\n" , $ doc );
175
235
176
236
foreach ($ lines as $ key => &$ line ) {
177
237
$ line = trim ($ line , "\r" );
178
- $ line = str_repeat ( ' ' , $ ident) .' * ' .$ line ;
238
+ $ line = $ ident .' * ' .$ line ;
179
239
$ line = rtrim ($ line );
180
240
}
181
241
182
242
$ doc = implode ($ newLine , $ lines );
183
- $ doc = str_repeat ( ' ' , $ ident) .'/** ' .$ newLine .$ doc ;
184
- $ doc .= $ newLine .str_repeat ( ' ' , $ ident) .' */ ' .$ newLine ;
243
+ $ doc = $ ident .'/** ' .$ newLine .$ doc ;
244
+ $ doc .= $ newLine .$ ident .' */ ' .$ newLine ;
185
245
186
246
return $ doc ;
187
247
}
188
248
249
+ /**
250
+ * Finds all the PHPDoc blocks in the file.
251
+ *
252
+ * @param string $file File content
253
+ *
254
+ * @return string[] Array of PHPDoc blocks
255
+ */
189
256
private function findAllDocDomment (string $ file )
190
257
{
191
258
$ regex = "#[\h]*/\*\*[\s]?([\s]*\*[^ \n]*[\s]?)+[\h]*\*/[\s]?# " ;
0 commit comments