Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 9344e5f

Browse files
authored
Use new fixers from php-cs-fixer v2.16 (#27)
feat : Enable self_static_accessor fixer feat : Enable and configure phpdoc_line_span fixer feat : Enable nullable_type_declaration_for_default_null_value fixer feat : Enable final_static_access fixer feat : Disable final_public_method_for_abstract_class fixer feat : Bump dependencies versions * fix : fixed phpstan error
1 parent 7829a0b commit 9344e5f

File tree

3 files changed

+93
-27
lines changed

3 files changed

+93
-27
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
],
2222
"require": {
2323
"php": "^7.2",
24-
"friendsofphp/php-cs-fixer": "~2.15.1",
25-
"kubawerlos/php-cs-fixer-custom-fixers": "^1.15.1",
26-
"pedrotroller/php-cs-custom-fixer": "^2.19.0"
24+
"friendsofphp/php-cs-fixer": "~2.16.0",
25+
"kubawerlos/php-cs-fixer-custom-fixers": "~1.16.1",
26+
"pedrotroller/php-cs-custom-fixer": "~2.19.1"
2727
},
2828
"require-dev": {
2929
"narrowspark/testing-helper": "^8.0.1",

src/Config.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
use PhpCsFixerCustomFixers\Fixer\SingleLineThrowFixer;
3535
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
3636
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
37+
use const PHP_VERSION_ID;
38+
use function array_merge;
39+
use function is_string;
40+
use function trim;
3741

3842
final class Config extends CsConfig
3943
{
@@ -53,14 +57,14 @@ final class Config extends CsConfig
5357
* @param null|string $header
5458
* @param array $overwriteConfig
5559
*/
56-
public function __construct(string $header = null, array $overwriteConfig = [])
60+
public function __construct(?string $header = null, array $overwriteConfig = [])
5761
{
5862
parent::__construct('narrowspark');
5963

60-
if (\is_string($header)) {
64+
if (is_string($header)) {
6165
$this->headerRules['header_comment'] = [
6266
'comment_type' => 'PHPDoc',
63-
'header' => \trim($header),
67+
'header' => trim($header),
6468
'location' => 'after_declare_strict',
6569
'separate' => 'both',
6670
];
@@ -78,10 +82,11 @@ public function __construct(string $header = null, array $overwriteConfig = [])
7882
*/
7983
public function getRules(): array
8084
{
81-
return \array_merge(
85+
return array_merge(
86+
$this->getNoGroupRules(),
8287
$this->getContribRules(),
8388
$this->getPhp71Rules(),
84-
\PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
89+
PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
8590
$this->getSymfonyRules(),
8691
$this->getPsr12Rules(),
8792
$this->getPHPUnitRules(),
@@ -144,7 +149,7 @@ protected function getKubawerlosRules(): array
144149
DataProviderNameFixer::name() => true,
145150
NoUselessSprintfFixer::name() => true,
146151
PhpUnitNoUselessReturnFixer::name() => true,
147-
SingleLineThrowFixer::name() => true,
152+
SingleLineThrowFixer::name() => false,
148153
NoDuplicatedImportsFixer::name() => true,
149154
DataProviderReturnTypeFixer::name() => true,
150155
CommentSurroundedBySpacesFixer::name() => true,
@@ -509,6 +514,7 @@ protected function getSymfonyRules(): array
509514
'single_quote' => true,
510515
'single_trait_insert_per_statement' => true,
511516
'single_line_comment_style' => false,
517+
'single_line_throw' => true,
512518
'standardize_not_equals' => true,
513519
'ternary_operator_spaces' => true,
514520
'ternary_to_null_coalescing' => true,
@@ -518,4 +524,29 @@ protected function getSymfonyRules(): array
518524
'whitespace_after_comma_in_array' => true,
519525
];
520526
}
527+
528+
/**
529+
* @return array<string, array<string, array<int, string>|bool|string>|bool>
530+
*/
531+
public function getNoGroupRules(): array
532+
{
533+
return [
534+
'final_static_access' => true,
535+
'final_public_method_for_abstract_class' => false,
536+
'lowercase_constants' => false,
537+
'global_namespace_import' => [
538+
'import_classes' => true,
539+
'import_constants' => true,
540+
'import_functions' => true,
541+
],
542+
'nullable_type_declaration_for_default_null_value' => true,
543+
'phpdoc_line_span' => [
544+
'const' => 'multi',
545+
'method' => 'multi',
546+
'property' => 'multi',
547+
],
548+
'phpdoc_to_param_type' => false,
549+
'self_static_accessor' => true,
550+
];
551+
}
521552
}

tests/ConfigTest.php

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Narrowspark\CS\Config\Tests;
66

7+
use Generator;
78
use Narrowspark\CS\Config\Config;
89
use Narrowspark\TestingHelper\Traits\AssertArrayTrait;
910
use PhpCsFixer\ConfigInterface;
@@ -40,6 +41,16 @@
4041
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
4142
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
4243
use PHPUnit\Framework\TestCase;
44+
use ReflectionException;
45+
use const PHP_VERSION_ID;
46+
use function array_diff;
47+
use function array_keys;
48+
use function array_map;
49+
use function array_merge;
50+
use function count;
51+
use function implode;
52+
use function sprintf;
53+
use function trim;
4354

4455
/**
4556
* @internal
@@ -100,13 +111,14 @@ public function testHasContribRules(): void
100111

101112
public function testIfAllRulesAreTested(): void
102113
{
103-
$testRules = \array_merge(
114+
$testRules = array_merge(
115+
$this->getNoGroupRules(),
104116
$this->getPsr2Rules(),
105117
$this->getPsr12Rules(),
106118
$this->getContribRules(),
107119
$this->getSymfonyRules(),
108120
$this->getPhp71Rules(),
109-
\PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
121+
PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
110122
$this->getPHPUnitRules(),
111123
$this->getPedroTrollerRules(),
112124
$this->getKubawerlosRules()
@@ -117,7 +129,7 @@ public function testIfAllRulesAreTested(): void
117129
self::assertTrue(isset($testRules[$key]), '[' . $key . '] Rule is missing.');
118130
}
119131

120-
self::assertCount(\count($testRules), $rules);
132+
self::assertCount(count($testRules), $rules);
121133
}
122134

123135
public function testDoesNotHaveHeaderCommentFixerByDefault(): void
@@ -167,14 +179,14 @@ public function testAllConfiguredRulesAreBuiltIn(): void
167179
$kubawerlosRules[] = $fixer->getName();
168180
}
169181

170-
$fixersNotBuiltIn = \array_diff(
182+
$fixersNotBuiltIn = array_diff(
171183
$this->configuredFixers(),
172-
\array_merge($this->builtInFixers(), $pedroTrollerRules, $kubawerlosRules)
184+
array_merge($this->builtInFixers(), $pedroTrollerRules, $kubawerlosRules)
173185
);
174186

175-
self::assertEmpty($fixersNotBuiltIn, \sprintf(
187+
self::assertEmpty($fixersNotBuiltIn, sprintf(
176188
'Failed to assert that fixers for the rules "%s" are built in',
177-
\implode('", "', $fixersNotBuiltIn)
189+
implode('", "', $fixersNotBuiltIn)
178190
));
179191
}
180192

@@ -192,13 +204,13 @@ public function testDoesNotHaveRulesEnabled(string $fixer, $reason): void
192204
];
193205

194206
if ($fixer === 'array_syntax') {
195-
self::assertNotSame(['syntax' => 'long'], $config->getRules()['array_syntax'], \sprintf(
207+
self::assertNotSame(['syntax' => 'long'], $config->getRules()['array_syntax'], sprintf(
196208
'Fixer "%s" should not be enabled, because "%s"',
197209
$fixer,
198210
$reason['long']
199211
));
200212
} else {
201-
self::assertArraySubset($rule, $config->getRules(), true, \sprintf(
213+
self::assertArraySubset($rule, $config->getRules(), true, sprintf(
202214
'Fixer "%s" should not be enabled, because "%s"',
203215
$fixer,
204216
$reason
@@ -231,7 +243,7 @@ public function provideDoesNotHaveRulesEnabledCases(): iterable
231243
'fopen_flag_order' => 'it changes r+b to b+r and w+b and b+w',
232244
];
233245

234-
$fixers = \array_merge($contribFixers, $symfonyFixers);
246+
$fixers = array_merge($contribFixers, $symfonyFixers);
235247

236248
$data = [];
237249

@@ -265,15 +277,15 @@ public function testHeaderCommentFixerIsEnabledIfHeaderIsProvided($header): void
265277
self::assertArrayHasKey('header_comment', $rules);
266278
$expected = [
267279
'comment_type' => 'PHPDoc',
268-
'header' => \trim($header),
280+
'header' => trim($header),
269281
'location' => 'after_declare_strict',
270282
'separate' => 'both',
271283
];
272284
self::assertSame($expected, $rules['header_comment']);
273285
}
274286

275287
/**
276-
* @return \Generator
288+
* @return Generator
277289
*/
278290
public function provideHeaderCommentFixerIsEnabledIfHeaderIsProvidedCases(): iterable
279291
{
@@ -716,6 +728,7 @@ protected function getSymfonyRules(): array
716728
'single_quote' => true,
717729
'single_trait_insert_per_statement' => true,
718730
'single_line_comment_style' => false,
731+
'single_line_throw' => true,
719732
'standardize_not_equals' => true,
720733
'ternary_operator_spaces' => true,
721734
'ternary_to_null_coalescing' => true,
@@ -726,6 +739,28 @@ protected function getSymfonyRules(): array
726739
];
727740
}
728741

742+
public function getNoGroupRules(): array
743+
{
744+
return [
745+
'final_static_access' => true,
746+
'final_public_method_for_abstract_class' => false,
747+
'lowercase_constants' => false,
748+
'global_namespace_import' => [
749+
'import_classes' => true,
750+
'import_constants' => true,
751+
'import_functions' => true,
752+
],
753+
'nullable_type_declaration_for_default_null_value' => true,
754+
'phpdoc_line_span' => [
755+
'const' => 'multi',
756+
'method' => 'multi',
757+
'property' => 'multi',
758+
],
759+
'phpdoc_to_param_type' => false,
760+
'self_static_accessor' => true,
761+
];
762+
}
763+
729764
/**
730765
* @param array $expected
731766
* @param array $actual
@@ -734,12 +769,12 @@ protected function getSymfonyRules(): array
734769
private function assertHasRules(array $expected, array $actual, string $set): void
735770
{
736771
foreach ($expected as $fixer => $isEnabled) {
737-
self::assertArrayHasKey($fixer, $actual, \sprintf(
772+
self::assertArrayHasKey($fixer, $actual, sprintf(
738773
'Failed to assert that a rule for fixer "%s" (in set "%s") exists.,',
739774
$fixer,
740775
$set
741776
));
742-
self::assertSame($isEnabled, $actual[$fixer], \sprintf(
777+
self::assertSame($isEnabled, $actual[$fixer], sprintf(
743778
'Failed to assert that fixer "%s" (in set "%s") is %s.',
744779
$fixer,
745780
$set,
@@ -760,15 +795,15 @@ private function configuredFixers(): array
760795
*
761796
* @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/2361
762797
*/
763-
$rules = \array_map(static function () {
798+
$rules = array_map(static function () {
764799
return true;
765800
}, $config->getRules());
766801

767-
return \array_keys(RuleSet::create($rules)->getRules());
802+
return array_keys(RuleSet::create($rules)->getRules());
768803
}
769804

770805
/**
771-
* @throws \ReflectionException
806+
* @throws ReflectionException
772807
*
773808
* @return string[]
774809
*/
@@ -780,7 +815,7 @@ private function builtInFixers(): array
780815
$fixerFactory = FixerFactory::create();
781816
$fixerFactory->registerBuiltInFixers();
782817

783-
$builtInFixers = \array_map(static function (FixerInterface $fixer) {
818+
$builtInFixers = array_map(static function (FixerInterface $fixer) {
784819
return $fixer->getName();
785820
}, $fixerFactory->getFixers());
786821
}

0 commit comments

Comments
 (0)