Skip to content

Commit 5a564e3

Browse files
committed
Further improves SectionComments
1 parent b4b0153 commit 5a564e3

1 file changed

Lines changed: 83 additions & 69 deletions

File tree

.github/phpcs/SectionComments.php

Lines changed: 83 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ public function __construct()
9999
];
100100

101101
foreach ($this->comments as $type => $string) {
102-
$regexes[$type] = preg_replace('/\s+/', '\s+', preg_quote($string, '/'));
102+
$words[] = trim($string, "/* \n\t");
103103
}
104104

105-
$this->comment_regex = implode('|', $regexes);
105+
$this->comment_regex = '/^\/[*\s]+(?:' . implode('|', $words) . ')[*\s]+\/$/';
106106
}
107107

108108
public function getName(): string
@@ -196,7 +196,7 @@ public function getPriority(): int
196196
return -110;
197197
}
198198

199-
public function isCandidate(Tokens $tokens): bool
199+
public function isCandidate(Tokens $tokens): bool
200200
{
201201
return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds());
202202
}
@@ -207,32 +207,16 @@ public function isCandidate(Tokens $tokens): bool
207207

208208
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
209209
{
210-
// First remove any existing section comments.
210+
$existing = [];
211+
211212
foreach ($tokens as $key => $token) {
212-
if ($token->getName() === 'T_COMMENT') {
213-
if (preg_match('/^' . $this->comment_regex . '$/', $token->getContent())) {
214-
$tokens->clearAt($key);
215-
216-
if ($tokens[$key + 1]->isWhitespace()) {
217-
$tokens[$key + 1] = new Token([
218-
T_WHITESPACE,
219-
"\n\n\t",
220-
]);
221-
} else {
222-
$tokens->insertAt(
223-
$key + 1,
224-
new Token([
225-
T_WHITESPACE,
226-
"\n\n\t",
227-
]),
228-
);
229-
}
213+
if ($token->getId() === T_COMMENT) {
214+
if (preg_match($this->comment_regex, $token->getContent())) {
215+
$existing[$key] = true;
230216
}
231217
}
232218
}
233219

234-
$tokens->clearEmptyTokens();
235-
236220
// Does this file contain an enumeration?
237221
$is_enum = $tokens->isAnyTokenKindsFound([T_ENUM]);
238222

@@ -254,57 +238,60 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
254238
$in = [];
255239

256240
foreach ($tokens as $key => $token) {
257-
$name = $token->getName();
241+
$id = $token->getId();
242+
243+
$this->timer += -hrtime(true);
258244

259245
// Build up the list of token types so that we can figure out
260246
// which comment type we will want.
261-
if (\in_array(
262-
$name,
247+
if (in_array(
248+
$id,
263249
empty($in) ? [
264-
'T_PUBLIC',
265-
'T_PROTECTED',
266-
'T_PRIVATE',
267-
$is_enum && !$exists['case'] ? 'T_CASE' : NAN,
250+
T_PUBLIC,
251+
T_PROTECTED,
252+
T_PRIVATE,
253+
$is_enum && !$exists['case'] ? T_CASE : -1,
268254
] : [
269-
'T_CONST',
270-
'T_STATIC',
271-
'T_VARIABLE',
272-
'T_FUNCTION',
255+
T_CONST,
256+
T_STATIC,
257+
T_VARIABLE,
258+
T_FUNCTION,
273259
],
260+
true,
274261
)) {
275-
$in[$name] = $key;
262+
$in[$id] = $key;
276263
}
277264

278265
// Which comment type do we want to insert?
279-
if (\array_key_exists('T_CONST', $in)) {
266+
if (isset($in[T_CONST])) {
280267
$insert_type = 'const';
281-
} elseif ($is_enum && !$exists['case'] && \array_key_exists('T_CASE', $in)) {
268+
} elseif ($is_enum && !$exists['case'] && isset($in[T_CASE])) {
282269
$insert_type = 'case';
283-
} elseif (\array_key_exists('T_VARIABLE', $in)) {
284-
if (\array_key_exists('T_STATIC', $in)) {
285-
if (\array_key_exists('T_PUBLIC', $in)) {
270+
} elseif (isset($in[T_VARIABLE])) {
271+
if (isset($in[T_STATIC])) {
272+
if (isset($in[T_PUBLIC])) {
286273
$insert_type = 'public_static_property';
287-
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
274+
} elseif (isset($in[T_PROTECTED]) || isset($in[T_PRIVATE])) {
288275
$insert_type = 'internal_static_property';
289276
}
290277
} else {
291-
if (\array_key_exists('T_PUBLIC', $in)) {
278+
if (isset($in[T_PUBLIC])) {
292279
$insert_type = 'public_property';
293-
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
280+
} elseif (isset($in[T_PROTECTED]) || isset($in[T_PRIVATE])) {
294281
$insert_type = 'internal_property';
295282
}
296283
}
297-
} elseif (\array_key_exists('T_FUNCTION', $in)) {
298-
if (\array_key_exists('T_STATIC', $in)) {
299-
if (\array_key_exists('T_PUBLIC', $in)) {
284+
} elseif (isset($in[T_FUNCTION])) {
285+
if (isset($in[T_STATIC])) {
286+
if (isset($in[T_PUBLIC])) {
300287
$insert_type = 'public_static_method';
301-
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
288+
} elseif (isset($in[T_PROTECTED]) || isset($in[T_PRIVATE])) {
302289
$insert_type = 'internal_static_method';
303290
}
304291
} else {
305-
if (\array_key_exists('T_PUBLIC', $in)) {
292+
if (isset($in[T_PUBLIC])) {
306293
$insert_type = 'public_method';
307-
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
294+
} elseif (isset($in[T_PROTECTED]) || isset($in[T_PRIVATE])) {
308295
$insert_type = 'internal_method';
309296
}
310297
}
@@ -332,34 +319,61 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
332319
// Now we need to take one step forward again.
333320
$insert_at++;
334321

335-
// Create the comment to insert.
336-
$to_insert = [
337-
new Token([
338-
T_COMMENT,
339-
$this->comments[$insert_type],
340-
]),
341-
];
342-
343-
// If necessary, also insert some whitespace.
344-
if (!$tokens[$insert_at]->isWhitespace()) {
345-
$to_insert[] = new Token([
346-
T_WHITESPACE,
347-
"\n\n\t",
348-
]);
349-
}
322+
if ($tokens[$insert_at]->getContent() !== $this->comments[$insert_type]) {
323+
// Create the comment to insert.
324+
$to_insert = [
325+
new Token([
326+
T_COMMENT,
327+
$this->comments[$insert_type],
328+
]),
329+
];
350330

351-
// Insert our comment.
352-
$slices[$insert_at] = $to_insert;
331+
// If necessary, also insert some whitespace.
332+
if (!$tokens[$insert_at]->isWhitespace()) {
333+
$to_insert[] = new Token([
334+
T_WHITESPACE,
335+
"\n\n\t",
336+
]);
337+
}
338+
339+
// Insert our comment.
340+
$slices[$insert_at] = $to_insert;
341+
342+
// This comment type has now been done.
343+
$exists[$insert_type] = true;
344+
} else {
345+
// Normalize whitespace.
346+
if ($tokens[$insert_at + 1]->isWhitespace()) {
347+
$tokens[$insert_at + 1] = new Token([
348+
T_WHITESPACE,
349+
"\n\n\t",
350+
]);
351+
}
353352

354-
// This comment type has now been done.
355-
$exists[$insert_type] = true;
353+
$exists[$insert_type] = true;
354+
355+
// Do not remove this token.
356+
if (isset($existing[$insert_at])) {
357+
unset($existing[$insert_at]);
358+
}
359+
}
356360
}
357361

358362
$in = [];
359363
unset($insert_type);
360364
}
361365
}
362366

367+
// Any existing tokens to empty out?
368+
foreach ($existing as $key => $_) {
369+
$tokens->clearAt($key);
370+
371+
// If necessary, also delete whitespace.
372+
if ($tokens[$key + 1]->isWhitespace()) {
373+
$tokens->clearAt($key + 1);
374+
}
375+
}
376+
363377
// Insert comments.
364378
if ($slices !== []) {
365379
$tokens->insertSlices($slices);

0 commit comments

Comments
 (0)