Skip to content

Commit d4bbccb

Browse files
IBX-8121: Introduced Ibexa Rule Sets (#8)
For more details see https://issues.ibexa.co/browse/IBX-8121 and #8 Key changes: * Introduced Ibexa Rule Sets * [Tests] Added test coverage for rule sets * Fixed deprecation notices * [SonarCloud][Tests] Excluded extracted rule set files from analysis --------- Co-Authored-By: Mikolaj Adamczyk <[email protected]>
1 parent 62b4a02 commit d4bbccb

File tree

17 files changed

+1265
-197
lines changed

17 files changed

+1265
-197
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@ jobs:
4040
- name: Run PHPStan analysis
4141
run: composer run-script phpstan
4242

43+
- name: Run tests suite
44+
run: composer run-script test
45+
4346
- name: Run code style check
4447
run: composer run-script check-cs -- --format=checkstyle | cs2pr

.php-cs-fixer.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
declare(strict_types=1);
88

99
use Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory;
10+
use Ibexa\CodeStyle\PhpCsFixer\Sets;
1011

1112
$finder = PhpCsFixer\Finder::create()
1213
->in(__DIR__)
1314
->files()->name('*.php');
1415

15-
return InternalConfigFactory::build()
16-
->setFinder($finder)
17-
;
16+
$configFactory = new InternalConfigFactory();
17+
$configFactory->withRuleSet(new Sets\Ibexa50RuleSet());
18+
19+
$config = $configFactory->buildConfig();
20+
$config->setFinder($finder);
21+
22+
return $config;

.sonarcloud.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sonar.exclusions=tests/lib/Sets/expected_rules/**/*

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
},
2020
"require-dev": {
2121
"roave/security-advisories": "dev-master",
22-
"phpstan/phpstan": "^1.10"
22+
"phpstan/phpstan": "^1.10",
23+
"phpunit/phpunit": "^9.6"
2324
},
2425
"autoload": {
2526
"psr-4": {
2627
"Ibexa\\CodeStyle\\": "src/lib/"
2728
}
2829
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"Ibexa\\Tests\\CodeStyle\\PhpCsFixer\\": "tests/lib/"
33+
}
34+
},
2935
"scripts": {
36+
"test": "phpunit",
3037
"check-cs": "php-cs-fixer fix -v --diff --dry-run --show-progress=dots",
3138
"fix-cs": "php-cs-fixer fix -v --show-progress=dots",
3239
"phpstan": "phpstan analyse -c phpstan.neon"

phpunit.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
beStrictAboutCoversAnnotation="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTodoAnnotatedTests="true"
9+
convertDeprecationsToExceptions="true"
10+
failOnRisky="true"
11+
failOnWarning="true"
12+
verbose="true">
13+
<testsuites>
14+
<testsuite name="default">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
20+
processUncoveredFiles="true">
21+
<include>
22+
<directory suffix=".php">src</directory>
23+
</include>
24+
</coverage>
25+
</phpunit>

src/lib/PhpCsFixer/Config.php

Lines changed: 6 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -10,181 +10,17 @@
1010

1111
use PhpCsFixer\Config as ConfigBase;
1212

13+
/**
14+
* @deprecated 1.3.0 The "Config" class has been deprecated, will be removed in 2.0. Use RuleSetInterface or InternalConfigFactory instead.
15+
*/
1316
class Config extends ConfigBase
1417
{
15-
/**
16-
* @param string $name
17-
*/
18-
public function __construct($name = 'default')
18+
public function __construct(string $name = 'default')
1919
{
2020
parent::__construct($name);
2121

2222
$this->setRiskyAllowed(true);
23-
$this->setRules([
24-
'declare_strict_types' => true,
25-
'encoding' => true,
26-
'full_opening_tag' => true,
27-
'blank_line_after_namespace' => true,
28-
'elseif' => true,
29-
'function_declaration' => true,
30-
'indentation_type' => true,
31-
'line_ending' => true,
32-
'constant_case' => ['case' => 'lower'],
33-
'lowercase_keywords' => true,
34-
'no_closing_tag' => true,
35-
'no_spaces_after_function_name' => true,
36-
'no_spaces_inside_parenthesis' => true,
37-
'no_trailing_whitespace' => true,
38-
'no_trailing_whitespace_in_comment' => true,
39-
'single_blank_line_at_eof' => true,
40-
'single_import_per_statement' => true,
41-
'single_line_after_imports' => true,
42-
'switch_case_semicolon_to_colon' => true,
43-
'switch_case_space' => true,
44-
'visibility_required' => true,
45-
'binary_operator_spaces' => true,
46-
'blank_line_before_statement' => [
47-
'statements' => ['return'],
48-
],
49-
'braces' => [
50-
'allow_single_line_closure' => true,
51-
],
52-
'class_attributes_separation' => [
53-
'elements' => [
54-
'method' => 'one',
55-
'property' => 'one',
56-
],
57-
],
58-
'declare_equal_normalize' => true,
59-
'function_typehint_space' => true,
60-
'include' => true,
61-
'increment_style' => true,
62-
'lowercase_cast' => true,
63-
'lowercase_static_reference' => true,
64-
'magic_constant_casing' => true,
65-
'magic_method_casing' => true,
66-
'method_argument_space' => true,
67-
'native_function_casing' => true,
68-
'native_function_type_declaration_casing' => true,
69-
'new_with_braces' => true,
70-
'no_blank_lines_after_class_opening' => true,
71-
'no_blank_lines_after_phpdoc' => true,
72-
'no_empty_comment' => true,
73-
'no_empty_phpdoc' => true,
74-
'no_empty_statement' => true,
75-
'no_extra_blank_lines' => ['tokens' => [
76-
'curly_brace_block',
77-
'extra',
78-
'parenthesis_brace_block',
79-
'square_brace_block',
80-
'throw',
81-
'use',
82-
]],
83-
'no_leading_import_slash' => true,
84-
'no_leading_namespace_whitespace' => true,
85-
'no_mixed_echo_print' => true,
86-
'no_multiline_whitespace_around_double_arrow' => true,
87-
'no_short_bool_cast' => true,
88-
'no_singleline_whitespace_before_semicolons' => true,
89-
'no_spaces_around_offset' => true,
90-
'no_trailing_comma_in_list_call' => true,
91-
'no_trailing_comma_in_singleline_array' => true,
92-
'no_unneeded_control_parentheses' => true,
93-
'no_unneeded_curly_braces' => true,
94-
'no_unneeded_final_method' => true,
95-
'no_unused_imports' => true,
96-
'no_whitespace_before_comma_in_array' => true,
97-
'no_whitespace_in_blank_line' => true,
98-
'normalize_index_brace' => true,
99-
'object_operator_without_whitespace' => true,
100-
'php_unit_fqcn_annotation' => true,
101-
'phpdoc_annotation_without_dot' => false,
102-
'phpdoc_indent' => true,
103-
'phpdoc_inline_tag_normalizer' => true,
104-
'phpdoc_tag_type' => [
105-
'tags' => [
106-
'inheritdoc' => 'inline',
107-
],
108-
],
109-
'general_phpdoc_tag_rename' => true,
110-
'phpdoc_no_access' => true,
111-
'phpdoc_no_alias_tag' => [
112-
'replacements' => [
113-
'type' => 'var',
114-
],
115-
],
116-
'phpdoc_no_empty_return' => true,
117-
'phpdoc_no_package' => true,
118-
'phpdoc_no_useless_inheritdoc' => true,
119-
'phpdoc_return_self_reference' => true,
120-
'phpdoc_scalar' => true,
121-
'phpdoc_separation' => true,
122-
'phpdoc_single_line_var_spacing' => true,
123-
'phpdoc_summary' => true,
124-
'phpdoc_trim' => true,
125-
'phpdoc_types' => true,
126-
'phpdoc_var_without_name' => true,
127-
'protected_to_private' => true,
128-
'return_type_declaration' => true,
129-
'semicolon_after_instruction' => true,
130-
'short_scalar_cast' => true,
131-
'single_class_element_per_statement' => true,
132-
'single_line_comment_style' => [
133-
'comment_types' => ['hash'],
134-
],
135-
'single_quote' => true,
136-
'single_trait_insert_per_statement' => true,
137-
'standardize_increment' => true,
138-
'standardize_not_equals' => true,
139-
'ternary_operator_spaces' => true,
140-
'trailing_comma_in_multiline' => true,
141-
'trim_array_spaces' => true,
142-
'unary_operator_spaces' => true,
143-
'whitespace_after_comma_in_array' => true,
144-
'dir_constant' => true,
145-
'ereg_to_preg' => true,
146-
'error_suppression' => true,
147-
'fopen_flag_order' => true,
148-
'fopen_flags' => false,
149-
'function_to_constant' => true,
150-
'implode_call' => true,
151-
'is_null' => true,
152-
'modernize_types_casting' => true,
153-
'native_constant_invocation' => [
154-
'fix_built_in' => false,
155-
'include' => [
156-
'DIRECTORY_SEPARATOR',
157-
'PHP_SAPI',
158-
'PHP_VERSION_ID',
159-
],
160-
'scope' => 'namespaced',
161-
],
162-
'no_alias_functions' => true,
163-
'no_homoglyph_names' => true,
164-
'non_printable_character' => true,
165-
'php_unit_construct' => true,
166-
'psr_autoloading' => true,
167-
'set_type_to_cast' => true,
168-
'concat_space' => ['spacing' => 'one'],
169-
'array_syntax' => ['syntax' => 'short'],
170-
'simplified_null_return' => false,
171-
'phpdoc_align' => false,
172-
'phpdoc_to_comment' => false,
173-
'cast_spaces' => false,
174-
'blank_line_after_opening_tag' => false,
175-
'single_blank_line_before_namespace' => true,
176-
'space_after_semicolon' => false,
177-
'native_function_invocation' => false,
178-
'phpdoc_types_order' => [
179-
'null_adjustment' => 'always_last',
180-
'sort_algorithm' => 'none',
181-
],
182-
'php_unit_mock_short_will_return' => false,
183-
'yoda_style' => false,
184-
'no_break_comment' => false,
185-
'self_accessor' => false,
186-
'static_lambda' => true,
187-
'ordered_imports' => true,
188-
]);
23+
$ruleSet = new Sets\Ibexa46RuleSet();
24+
$this->setRules($ruleSet->getRules());
18925
}
19026
}

src/lib/PhpCsFixer/InternalConfigFactory.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Ibexa\CodeStyle\PhpCsFixer;
1010

11-
use AdamWojs\PhpCsFixerPhpdocForceFQCN\Fixer\Phpdoc\ForceFQCNFixer;
11+
use Ibexa\CodeStyle\PhpCsFixer\Sets\RuleSetInterface;
1212
use PhpCsFixer\ConfigInterface;
1313

1414
/**
@@ -18,16 +18,16 @@
1818
*/
1919
final class InternalConfigFactory
2020
{
21-
/** @deprecated Use IBEXA_PHP_HEADER constant instead. */
21+
/** @deprecated 1.3.0 The "InternalConfigFactory::EZPLATFORM_PHP_HEADER" constant has been deprecated, will be removed in 2.0. Use AbstractIbexaRuleSet::IBEXA_PHP_HEADER constant instead. */
2222
public const EZPLATFORM_PHP_HEADER = self::IBEXA_PHP_HEADER;
2323

24-
public const IBEXA_PHP_HEADER = <<<'EOF'
25-
@copyright Copyright (C) Ibexa AS. All rights reserved.
26-
@license For full copyright and license information view LICENSE file distributed with this source code.
27-
EOF;
24+
/** @deprecated 1.3.0 The "InternalConfigFactory::IBEXA_PHP_HEADER" constant has been deprecated, will be removed in 2.0. Use AbstractIbexaRuleSet::IBEXA_PHP_HEADER constant instead. */
25+
public const IBEXA_PHP_HEADER = Sets\AbstractIbexaRuleSet::IBEXA_PHP_HEADER;
2826

2927
/** @var array<string, mixed> */
30-
private $customRules = [];
28+
private array $customRules = [];
29+
30+
private RuleSetInterface $ruleSet;
3131

3232
/**
3333
* @param array<string, mixed> $rules
@@ -39,26 +39,24 @@ public function withRules(array $rules): self
3939
return $this;
4040
}
4141

42+
public function withRuleSet(RuleSetInterface $ruleSet): self
43+
{
44+
$this->ruleSet = $ruleSet;
45+
46+
return $this;
47+
}
48+
49+
public function getRuleSet(): RuleSetInterface
50+
{
51+
return $this->ruleSet ??= new Sets\Ibexa46RuleSet();
52+
}
53+
4254
public function buildConfig(): ConfigInterface
4355
{
44-
$config = new Config();
45-
$config->registerCustomFixers([
46-
new ForceFQCNFixer(),
47-
]);
48-
49-
$specificRules = [
50-
'header_comment' => [
51-
'comment_type' => 'PHPDoc',
52-
'header' => static::IBEXA_PHP_HEADER,
53-
'location' => 'after_open',
54-
'separate' => 'top',
55-
],
56-
'AdamWojs/phpdoc_force_fqcn_fixer' => true,
57-
];
56+
$config = $this->getRuleSet()->buildConfig();
5857
$config->setRules(array_merge(
5958
$config->getRules(),
60-
$specificRules,
61-
$this->customRules
59+
$this->customRules,
6260
));
6361

6462
return $config;

0 commit comments

Comments
 (0)