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

Commit

Permalink
Use new fixers from php-cs-fixer v2.16 (#27)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
prisis authored Nov 3, 2019
1 parent 7829a0b commit 9344e5f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
],
"require": {
"php": "^7.2",
"friendsofphp/php-cs-fixer": "~2.15.1",
"kubawerlos/php-cs-fixer-custom-fixers": "^1.15.1",
"pedrotroller/php-cs-custom-fixer": "^2.19.0"
"friendsofphp/php-cs-fixer": "~2.16.0",
"kubawerlos/php-cs-fixer-custom-fixers": "~1.16.1",
"pedrotroller/php-cs-custom-fixer": "~2.19.1"
},
"require-dev": {
"narrowspark/testing-helper": "^8.0.1",
Expand Down
43 changes: 37 additions & 6 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
use PhpCsFixerCustomFixers\Fixer\SingleLineThrowFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
use const PHP_VERSION_ID;
use function array_merge;
use function is_string;
use function trim;

final class Config extends CsConfig
{
Expand All @@ -53,14 +57,14 @@ final class Config extends CsConfig
* @param null|string $header
* @param array $overwriteConfig
*/
public function __construct(string $header = null, array $overwriteConfig = [])
public function __construct(?string $header = null, array $overwriteConfig = [])
{
parent::__construct('narrowspark');

if (\is_string($header)) {
if (is_string($header)) {
$this->headerRules['header_comment'] = [
'comment_type' => 'PHPDoc',
'header' => \trim($header),
'header' => trim($header),
'location' => 'after_declare_strict',
'separate' => 'both',
];
Expand All @@ -78,10 +82,11 @@ public function __construct(string $header = null, array $overwriteConfig = [])
*/
public function getRules(): array
{
return \array_merge(
return array_merge(
$this->getNoGroupRules(),
$this->getContribRules(),
$this->getPhp71Rules(),
\PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
$this->getSymfonyRules(),
$this->getPsr12Rules(),
$this->getPHPUnitRules(),
Expand Down Expand Up @@ -144,7 +149,7 @@ protected function getKubawerlosRules(): array
DataProviderNameFixer::name() => true,
NoUselessSprintfFixer::name() => true,
PhpUnitNoUselessReturnFixer::name() => true,
SingleLineThrowFixer::name() => true,
SingleLineThrowFixer::name() => false,
NoDuplicatedImportsFixer::name() => true,
DataProviderReturnTypeFixer::name() => true,
CommentSurroundedBySpacesFixer::name() => true,
Expand Down Expand Up @@ -509,6 +514,7 @@ protected function getSymfonyRules(): array
'single_quote' => true,
'single_trait_insert_per_statement' => true,
'single_line_comment_style' => false,
'single_line_throw' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
Expand All @@ -518,4 +524,29 @@ protected function getSymfonyRules(): array
'whitespace_after_comma_in_array' => true,
];
}

/**
* @return array<string, array<string, array<int, string>|bool|string>|bool>
*/
public function getNoGroupRules(): array
{
return [
'final_static_access' => true,
'final_public_method_for_abstract_class' => false,
'lowercase_constants' => false,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'nullable_type_declaration_for_default_null_value' => true,
'phpdoc_line_span' => [
'const' => 'multi',
'method' => 'multi',
'property' => 'multi',
],
'phpdoc_to_param_type' => false,
'self_static_accessor' => true,
];
}
}
71 changes: 53 additions & 18 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Narrowspark\CS\Config\Tests;

use Generator;
use Narrowspark\CS\Config\Config;
use Narrowspark\TestingHelper\Traits\AssertArrayTrait;
use PhpCsFixer\ConfigInterface;
Expand Down Expand Up @@ -40,6 +41,16 @@
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use const PHP_VERSION_ID;
use function array_diff;
use function array_keys;
use function array_map;
use function array_merge;
use function count;
use function implode;
use function sprintf;
use function trim;

/**
* @internal
Expand Down Expand Up @@ -100,13 +111,14 @@ public function testHasContribRules(): void

public function testIfAllRulesAreTested(): void
{
$testRules = \array_merge(
$testRules = array_merge(
$this->getNoGroupRules(),
$this->getPsr2Rules(),
$this->getPsr12Rules(),
$this->getContribRules(),
$this->getSymfonyRules(),
$this->getPhp71Rules(),
\PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
PHP_VERSION_ID >= 70300 ? $this->getPhp73Rules() : [],
$this->getPHPUnitRules(),
$this->getPedroTrollerRules(),
$this->getKubawerlosRules()
Expand All @@ -117,7 +129,7 @@ public function testIfAllRulesAreTested(): void
self::assertTrue(isset($testRules[$key]), '[' . $key . '] Rule is missing.');
}

self::assertCount(\count($testRules), $rules);
self::assertCount(count($testRules), $rules);
}

public function testDoesNotHaveHeaderCommentFixerByDefault(): void
Expand Down Expand Up @@ -167,14 +179,14 @@ public function testAllConfiguredRulesAreBuiltIn(): void
$kubawerlosRules[] = $fixer->getName();
}

$fixersNotBuiltIn = \array_diff(
$fixersNotBuiltIn = array_diff(
$this->configuredFixers(),
\array_merge($this->builtInFixers(), $pedroTrollerRules, $kubawerlosRules)
array_merge($this->builtInFixers(), $pedroTrollerRules, $kubawerlosRules)
);

self::assertEmpty($fixersNotBuiltIn, \sprintf(
self::assertEmpty($fixersNotBuiltIn, sprintf(
'Failed to assert that fixers for the rules "%s" are built in',
\implode('", "', $fixersNotBuiltIn)
implode('", "', $fixersNotBuiltIn)
));
}

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

if ($fixer === 'array_syntax') {
self::assertNotSame(['syntax' => 'long'], $config->getRules()['array_syntax'], \sprintf(
self::assertNotSame(['syntax' => 'long'], $config->getRules()['array_syntax'], sprintf(
'Fixer "%s" should not be enabled, because "%s"',
$fixer,
$reason['long']
));
} else {
self::assertArraySubset($rule, $config->getRules(), true, \sprintf(
self::assertArraySubset($rule, $config->getRules(), true, sprintf(
'Fixer "%s" should not be enabled, because "%s"',
$fixer,
$reason
Expand Down Expand Up @@ -231,7 +243,7 @@ public function provideDoesNotHaveRulesEnabledCases(): iterable
'fopen_flag_order' => 'it changes r+b to b+r and w+b and b+w',
];

$fixers = \array_merge($contribFixers, $symfonyFixers);
$fixers = array_merge($contribFixers, $symfonyFixers);

$data = [];

Expand Down Expand Up @@ -265,15 +277,15 @@ public function testHeaderCommentFixerIsEnabledIfHeaderIsProvided($header): void
self::assertArrayHasKey('header_comment', $rules);
$expected = [
'comment_type' => 'PHPDoc',
'header' => \trim($header),
'header' => trim($header),
'location' => 'after_declare_strict',
'separate' => 'both',
];
self::assertSame($expected, $rules['header_comment']);
}

/**
* @return \Generator
* @return Generator
*/
public function provideHeaderCommentFixerIsEnabledIfHeaderIsProvidedCases(): iterable
{
Expand Down Expand Up @@ -716,6 +728,7 @@ protected function getSymfonyRules(): array
'single_quote' => true,
'single_trait_insert_per_statement' => true,
'single_line_comment_style' => false,
'single_line_throw' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
Expand All @@ -726,6 +739,28 @@ protected function getSymfonyRules(): array
];
}

public function getNoGroupRules(): array
{
return [
'final_static_access' => true,
'final_public_method_for_abstract_class' => false,
'lowercase_constants' => false,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'nullable_type_declaration_for_default_null_value' => true,
'phpdoc_line_span' => [
'const' => 'multi',
'method' => 'multi',
'property' => 'multi',
],
'phpdoc_to_param_type' => false,
'self_static_accessor' => true,
];
}

/**
* @param array $expected
* @param array $actual
Expand All @@ -734,12 +769,12 @@ protected function getSymfonyRules(): array
private function assertHasRules(array $expected, array $actual, string $set): void
{
foreach ($expected as $fixer => $isEnabled) {
self::assertArrayHasKey($fixer, $actual, \sprintf(
self::assertArrayHasKey($fixer, $actual, sprintf(
'Failed to assert that a rule for fixer "%s" (in set "%s") exists.,',
$fixer,
$set
));
self::assertSame($isEnabled, $actual[$fixer], \sprintf(
self::assertSame($isEnabled, $actual[$fixer], sprintf(
'Failed to assert that fixer "%s" (in set "%s") is %s.',
$fixer,
$set,
Expand All @@ -760,15 +795,15 @@ private function configuredFixers(): array
*
* @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/2361
*/
$rules = \array_map(static function () {
$rules = array_map(static function () {
return true;
}, $config->getRules());

return \array_keys(RuleSet::create($rules)->getRules());
return array_keys(RuleSet::create($rules)->getRules());
}

/**
* @throws \ReflectionException
* @throws ReflectionException
*
* @return string[]
*/
Expand All @@ -780,7 +815,7 @@ private function builtInFixers(): array
$fixerFactory = FixerFactory::create();
$fixerFactory->registerBuiltInFixers();

$builtInFixers = \array_map(static function (FixerInterface $fixer) {
$builtInFixers = array_map(static function (FixerInterface $fixer) {
return $fixer->getName();
}, $fixerFactory->getFixers());
}
Expand Down

0 comments on commit 9344e5f

Please sign in to comment.