Skip to content

Commit 4ace4cd

Browse files
Merge branch 'live627-fixer' into release-3.0
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
2 parents 9b2601b + f0ddb66 commit 4ace4cd

2 files changed

Lines changed: 56 additions & 62 deletions

File tree

.github/phpcs/SectionComments.php

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ final class SectionComments extends AbstractFixer
3535

3636
public array $comments;
3737

38+
public string $comment_regex;
39+
3840
/****************
3941
* Public methods
4042
****************/
@@ -95,6 +97,12 @@ public function __construct()
9597
' *************************/',
9698
]),
9799
];
100+
101+
foreach ($this->comments as $type => $string) {
102+
$regexes[$type] = preg_replace('/\s+/', '\s+', preg_quote($string, '/'));
103+
}
104+
105+
$this->comment_regex = implode('|', $regexes);
98106
}
99107

100108
public function getName(): string
@@ -190,13 +198,7 @@ public function getPriority(): int
190198

191199
public function isCandidate(Tokens $tokens): bool
192200
{
193-
foreach ($tokens as $token) {
194-
if ($token->isClassy()) {
195-
return true;
196-
}
197-
}
198-
199-
return false;
201+
return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds());
200202
}
201203

202204
/******************
@@ -206,30 +208,24 @@ public function isCandidate(Tokens $tokens): bool
206208
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
207209
{
208210
// First remove any existing section comments.
209-
foreach ($this->comments as $type => $string) {
210-
$regexes[$type] = preg_replace('/\s+/', '\s+', preg_quote($string, '/'));
211-
}
212-
213211
foreach ($tokens as $key => $token) {
214212
if ($token->getName() === 'T_COMMENT') {
215-
foreach ($regexes as $type => $regex) {
216-
if (preg_match('/^' . $regex . '$/', $token->getContent())) {
217-
$tokens->clearAt($key);
213+
if (preg_match('/^' . $this->comment_regex . '$/', $token->getContent())) {
214+
$tokens->clearAt($key);
218215

219-
if ($tokens[$key + 1]->isWhitespace()) {
220-
$tokens[$key + 1] = new Token([
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([
221225
T_WHITESPACE,
222226
"\n\n\t",
223-
]);
224-
} else {
225-
$tokens->insertAt(
226-
$key + 1,
227-
new Token([
228-
T_WHITESPACE,
229-
"\n\n\t",
230-
]),
231-
);
232-
}
227+
]),
228+
);
233229
}
234230
}
235231
}
@@ -238,16 +234,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
238234
$tokens->clearEmptyTokens();
239235

240236
// Does this file contain an enumeration?
241-
$is_enum = false;
242-
243-
foreach ($tokens as $token) {
244-
if ($token->isGivenKind(T_ENUM)) {
245-
$is_enum = true;
246-
break;
247-
}
248-
}
237+
$is_enum = $tokens->isAnyTokenKindsFound([T_ENUM]);
249238

250239
// Now insert fresh copies of the section comments.
240+
$slices = [];
251241
$exists = [
252242
'case' => false,
253243
'const' => false,
@@ -264,10 +254,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
264254
$in = [];
265255

266256
foreach ($tokens as $key => $token) {
257+
$name = $token->getName();
258+
267259
// Build up the list of token types so that we can figure out
268260
// which comment type we will want.
269-
if (in_array(
270-
$token->getName(),
261+
if (\in_array(
262+
$name,
271263
empty($in) ? [
272264
'T_PUBLIC',
273265
'T_PROTECTED',
@@ -280,39 +272,39 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
280272
'T_FUNCTION',
281273
],
282274
)) {
283-
$in[$key] = $token->getName();
275+
$in[$name] = $key;
284276
}
285277

286278
// Which comment type do we want to insert?
287-
if (in_array('T_CONST', $in)) {
279+
if (\array_key_exists('T_CONST', $in)) {
288280
$insert_type = 'const';
289-
} elseif ($is_enum && !$exists['case'] && in_array('T_CASE', $in)) {
281+
} elseif ($is_enum && !$exists['case'] && \array_key_exists('T_CASE', $in)) {
290282
$insert_type = 'case';
291-
} elseif (in_array('T_VARIABLE', $in)) {
292-
if (in_array('T_STATIC', $in)) {
293-
if (in_array('T_PUBLIC', $in)) {
283+
} elseif (\array_key_exists('T_VARIABLE', $in)) {
284+
if (\array_key_exists('T_STATIC', $in)) {
285+
if (\array_key_exists('T_PUBLIC', $in)) {
294286
$insert_type = 'public_static_property';
295-
} elseif (in_array('T_PROTECTED', $in) || in_array('T_PRIVATE', $in)) {
287+
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
296288
$insert_type = 'internal_static_property';
297289
}
298290
} else {
299-
if (in_array('T_PUBLIC', $in)) {
291+
if (\array_key_exists('T_PUBLIC', $in)) {
300292
$insert_type = 'public_property';
301-
} elseif (in_array('T_PROTECTED', $in) || in_array('T_PRIVATE', $in)) {
293+
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
302294
$insert_type = 'internal_property';
303295
}
304296
}
305-
} elseif (in_array('T_FUNCTION', $in)) {
306-
if (in_array('T_STATIC', $in)) {
307-
if (in_array('T_PUBLIC', $in)) {
297+
} elseif (\array_key_exists('T_FUNCTION', $in)) {
298+
if (\array_key_exists('T_STATIC', $in)) {
299+
if (\array_key_exists('T_PUBLIC', $in)) {
308300
$insert_type = 'public_static_method';
309-
} elseif (in_array('T_PROTECTED', $in) || in_array('T_PRIVATE', $in)) {
301+
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
310302
$insert_type = 'internal_static_method';
311303
}
312304
} else {
313-
if (in_array('T_PUBLIC', $in)) {
305+
if (\array_key_exists('T_PUBLIC', $in)) {
314306
$insert_type = 'public_method';
315-
} elseif (in_array('T_PROTECTED', $in) || in_array('T_PRIVATE', $in)) {
307+
} elseif (\array_key_exists('T_PROTECTED', $in) || \array_key_exists('T_PRIVATE', $in)) {
316308
$insert_type = 'internal_method';
317309
}
318310
}
@@ -322,7 +314,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
322314
if (!$exists[$insert_type]) {
323315
// Start by assuming we want to insert right before the
324316
// 'public', 'protected', or 'private' keyword.
325-
$insert_at = array_key_first($in);
317+
$insert_at = array_first($in);
326318

327319
// Walk back to include any preceding 'final' or 'readonly'
328320
// keywords, as well as any comments or whitespace.
@@ -357,10 +349,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
357349
}
358350

359351
// Insert our comment.
360-
$tokens->insertAt(
361-
$insert_at,
362-
$to_insert,
363-
);
352+
$slices[$insert_at] = $to_insert;
364353

365354
// This comment type has now been done.
366355
$exists[$insert_type] = true;
@@ -370,5 +359,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
370359
unset($insert_type);
371360
}
372361
}
362+
363+
// Insert comments.
364+
if ($slices !== []) {
365+
$tokens->insertSlices($slices);
366+
}
373367
}
374368
}

.php-cs-fixer.dist.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
// Skip anything being ignored in .gitignore.
2828
->ignoreVCSIgnored(true);
2929

30-
require_once('.github/phpcs/SectionComments.php');
30+
require_once '.github/phpcs/SectionComments.php';
3131

3232
return (new PhpCsFixer\Config())
33-
->registerCustomFixers([
34-
new SMF\Fixer\ClassNotation\SectionComments(),
35-
])
33+
->registerCustomFixers([
34+
new SMF\Fixer\ClassNotation\SectionComments(),
35+
])
3636
->setRules([
3737
'@PER-CS2x0' => true,
3838

@@ -82,15 +82,15 @@
8282
'method_protected',
8383
'method_private',
8484
'method_protected_static',
85-
'method_private_static'
85+
'method_private_static',
8686
],
8787
'sort_algorithm' => 'none',
8888
],
8989
'ordered_types' => [
9090
'null_adjustment' => 'always_last',
9191
'sort_algorithm' => 'none',
9292
],
93-
'SMF/section_comments' => true,
93+
'SMF/section_comments' => true,
9494

9595
// Control structure.
9696
'include' => true,

0 commit comments

Comments
 (0)