Skip to content

Commit f4dd542

Browse files
authored
tools: use easy-coding-standard for code-style checks (#5489)
* tools: use 'easy-coding-standard' for code-style checks todo: - add php-code-sniffer rules - add ECG/php-compa checks (already tested ... next pr) * workflow * PER 3 CS * cache keys
1 parent f30028b commit f4dd542

10 files changed

Lines changed: 206 additions & 95 deletions

File tree

.ddev/commands/web/php-cs-fixer

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
## Usage: php-cs-fixer
55
## Example: ddev php-cs-fixer <path-to-files>
66

7-
php vendor/bin/php-cs-fixer fix "$@"
7+
php vendor/bin/ecs --config=.php-cs-fixer.dist.php --fix "$@"

.github/workflows/php-cs-fixer.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ jobs:
3636
- name: Restore result cache
3737
uses: actions/cache/restore@v5
3838
with:
39-
path: .cache/.php-cs-fixer.cache
40-
key: php-cs-fixer-result-cache-${{ github.run_id }}
39+
path: .cache/.ecs.cache
40+
key: ecs-cache-${{ github.run_id }}
4141
restore-keys: |
42-
php-cs-fixer-result-cache-
42+
ecs-cache-
4343
4444
- name: PHP-CS-Fixer
45-
run: php vendor/bin/php-cs-fixer fix --diff --dry-run
45+
run: php vendor/bin/ecs --config=.php-cs-fixer.dist.php
4646

4747
- name: Save result cache
4848
uses: actions/cache/save@v5
4949
if: always()
5050
with:
51-
path: .cache/.php-cs-fixer.cache
52-
key: php-cs-fixer-result-cache-${{ github.run_id }}
51+
path: .cache/.ecs.cache
52+
key: ecs-cache-${{ github.run_id }}

.php-cs-fixer.dist.php

Lines changed: 120 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,124 @@
22

33
declare(strict_types=1);
44

5-
$config = new PhpCsFixer\Config();
6-
return $config
7-
->setCacheFile(__DIR__ . '/.cache/.php-cs-fixer.cache')
8-
->setFinder(
9-
PhpCsFixer\Finder::create()
10-
->in([
11-
__DIR__,
12-
])
13-
->exclude([
14-
__DIR__ . '/shell/translations.php',
15-
])
16-
->name('*.php')
17-
->ignoreDotFiles(true)
18-
->ignoreVCS(true)
5+
/**
6+
* @copyright For copyright and license information, read the COPYING.txt file.
7+
* @link /COPYING.txt
8+
* @license Open Software License (OSL 3.0)
9+
*/
10+
11+
use PhpCsFixer\Fixer as PhpCsFixer;
12+
use Symplify\EasyCodingStandard\Config\ECSConfig;
13+
14+
return ECSConfig::configure()
15+
->withPaths([
16+
__DIR__ . '/app/Mage.php',
17+
__DIR__ . '/app/code/core',
18+
__DIR__ . '/dev',
19+
__DIR__ . '/errors',
20+
__DIR__ . '/lib/Mage',
21+
__DIR__ . '/lib/Magento',
22+
__DIR__ . '/lib/Varien',
23+
__DIR__ . '/shell',
24+
__DIR__ . '/tests',
25+
])
26+
->withFileExtensions(['php'])
27+
->withRootFiles()
28+
->withCache(directory: __DIR__ . '/.cache/.ecs.cache')
29+
->withPhpCsFixerSets(perCS30: true)
30+
->withRules([
31+
// RISKY: Replaces intval, floatval, doubleval, strval and boolval function calls with according type casting operators
32+
PhpCsFixer\CastNotation\ModernizeTypesCastingFixer::class,
33+
// RISKY: Use && and || logical operators instead of and and or
34+
PhpCsFixer\Operator\LogicalOperatorsFixer::class,
35+
// There should not be empty PHPDoc blocks
36+
PhpCsFixer\Phpdoc\NoEmptyPhpdocFixer::class,
37+
// PHPDoc annotation descriptions should not be a sentence
38+
PhpCsFixer\Phpdoc\PhpdocAnnotationWithoutDotFixer::class,
39+
// Docblocks should have the same indentation as the documented subject
40+
PhpCsFixer\Phpdoc\PhpdocIndentFixer::class,
41+
// Orders all @param annotations in DocBlocks according to method signature
42+
PhpCsFixer\Phpdoc\PhpdocParamOrderFixer::class,
43+
// Single line @var PHPDoc should have proper spacing
44+
PhpCsFixer\Phpdoc\PhpdocSingleLineVarSpacingFixer::class,
45+
// PHPDoc should start and end with content, excluding the very first and last line of the docblocks
46+
PhpCsFixer\Phpdoc\PhpdocTrimFixer::class,
47+
// Removes extra blank lines after summary and after description in PHPDoc
48+
PhpCsFixer\Phpdoc\PhpdocTrimConsecutiveBlankLineSeparationFixer::class,
49+
// @var and @type annotations must have type and name in the correct order
50+
PhpCsFixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer::class,
51+
// @var and @type annotations of classy properties should not contain the name
52+
PhpCsFixer\Phpdoc\PhpdocVarWithoutNameFixer::class,
53+
])
54+
->withConfiguredRule(
55+
// PHP84: Adds or removes ? before single type declarations or |null at the end of union types when parameters have a default null value.
56+
PhpCsFixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer::class,
57+
['use_nullable_type_declaration' => true],
58+
)
59+
->withConfiguredRule(
60+
// Sort union types and intersection types using configured order.
61+
PhpCsFixer\Operator\OperatorLinebreakFixer::class,
62+
['only_booleans' => false, 'position' => 'beginning'],
63+
)
64+
->withConfiguredRule(
65+
// Operators - when multiline - must always be at the beginning or at the end of the line.
66+
PhpCsFixer\ClassNotation\OrderedTypesFixer::class,
67+
['sort_algorithm' => 'alpha'],
68+
)
69+
->withConfiguredRule(
70+
// All items of the given PHPDoc tags must be either left-aligned or (by default) aligned vertically
71+
PhpCsFixer\Phpdoc\PhpdocAlignFixer::class,
72+
['align' => 'vertical'],
73+
)
74+
->withConfiguredRule(
75+
// Annotations in PHPDoc should be ordered in defined sequence
76+
PhpCsFixer\Phpdoc\PhpdocOrderFixer::class,
77+
['order' => ['param', 'return', 'throws', 'deprecated', 'see', 'SuppressWarnings', 'phpstan-ignore']],
78+
)
79+
->withConfiguredRule(
80+
// Annotations in PHPDoc should be ordered in defined sequence
81+
PhpCsFixer\Phpdoc\PhpdocOrderByValueFixer::class,
82+
['annotations' => ['author', 'covers', 'group', 'method', 'throws', 'uses']],
83+
)
84+
->withConfiguredRule(
85+
// Fixes casing of PHPDoc tags
86+
PhpCsFixer\Phpdoc\PhpdocScalarFixer::class,
87+
['types' => ['boolean', 'callback', 'double', 'integer', 'real', 'str']],
88+
)
89+
->withConfiguredRule(
90+
// Fixes casing of PHPDoc tags
91+
PhpCsFixer\Phpdoc\PhpdocTagCasingFixer::class,
92+
['tags' => ['inheritDoc']],
93+
)
94+
->withConfiguredRule(
95+
// Sorts PHPDoc types
96+
PhpCsFixer\Phpdoc\PhpdocTypesOrderFixer::class,
97+
['sort_algorithm' => 'alpha'],
98+
)
99+
->withConfiguredRule(
100+
// Calls to PHPUnit\Framework\TestCase static methods must all be of the same type, either $this->, self:: or static::
101+
PhpCsFixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer::class,
102+
['call_type' => 'self'],
103+
)
104+
->withConfiguredRule(
105+
// There MUST NOT be more than one property or constant declared per statement.
106+
PhpCsFixer\ClassNotation\SingleClassElementPerStatementFixer::class,
107+
['elements' => ['const', 'property']],
108+
)
109+
->withConfiguredRule(
110+
// Convert double quotes to single quotes for simple strings.
111+
PhpCsFixer\StringNotation\SingleQuoteFixer::class,
112+
['strings_containing_single_quote_chars' => false],
113+
)
114+
->withConfiguredRule(
115+
// Arguments lists, array destructuring lists, arrays that are multi-line, match-lines and parameters lists must have a trailing comma.
116+
// removed "match" and "parameters" for PHP7
117+
// see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/8308
118+
PhpCsFixer\ControlStructure\TrailingCommaInMultilineFixer::class,
119+
['after_heredoc' => true, 'elements' => ['arguments', 'array_destructuring', 'arrays']],
19120
)
20-
->setRiskyAllowed(true)
21-
->setRules([
22-
// see https://cs.symfony.com/doc/ruleSets/PER-CS2.0.html
23-
'@PER-CS2x0' => true,
24-
// RISKY: Use && and || logical operators instead of and and or.
25-
'logical_operators' => true,
26-
// RISKY: Replaces intval, floatval, doubleval, strval and boolval function calls with according type casting operator.
27-
'modernize_types_casting' => true,
28-
// There should not be empty PHPDoc blocks.
29-
'no_empty_phpdoc' => true,
30-
// PHP84: Adds or removes ? before single type declarations or |null at the end of union types when parameters have a default null value.
31-
'nullable_type_declaration_for_default_null_value' => true,
32-
// Operators - when multiline - must always be at the beginning or at the end of the line.
33-
'operator_linebreak' => true,
34-
// Sort union types and intersection types using configured order.
35-
'ordered_types' => true,
36-
// Calls to PHPUnit\Framework\TestCase static methods must all be of the same type, either $this->, self:: or static::
37-
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
38-
// PHPDoc annotation descriptions should not be a sentence.
39-
'phpdoc_annotation_without_dot' => true,
40-
// All items of the given PHPDoc tags must be either left-aligned or (by default) aligned vertically.
41-
'phpdoc_align' => true,
42-
// Docblocks should have the same indentation as the documented subject.
43-
'phpdoc_indent' => true,
44-
// Annotations in PHPDoc should be ordered in defined sequence.
45-
'phpdoc_order' => ['order' => ['param', 'return', 'throws', 'deprecated', 'see', 'SuppressWarnings', 'phpstan-ignore']],
46-
// Order PHPDoc tags by value.
47-
'phpdoc_order_by_value' => ['annotations' => ['author', 'covers', 'group', 'method', 'throws', 'uses']],
48-
// Orders all @param annotations in DocBlocks according to method signature.
49-
'phpdoc_param_order' => true,
50-
// PHPDoc should start and end with content, excluding the very first and last line of the docblocks.
51-
'phpdoc_trim' => true,
52-
// Removes extra blank lines after summary and after description in PHPDoc.
53-
'phpdoc_trim_consecutive_blank_line_separation' => true,
54-
// Scalar types should always be written in the same form. int not integer, bool not boolean, float not real or double.
55-
'phpdoc_scalar' => true,
56-
// Single line @var PHPDoc should have proper spacing.
57-
'phpdoc_single_line_var_spacing' => true,
58-
// Fixes casing of PHPDoc tags.
59-
'phpdoc_tag_casing' => true,
60-
// Sorts PHPDoc types.
61-
'phpdoc_types_order' => true,
62-
// @var and @type annotations must have type and name in the correct order.
63-
'phpdoc_var_annotation_correct_order' => true,
64-
// @var and @type annotations of classy properties should not contain the name.
65-
'phpdoc_var_without_name' => true,
66-
// There MUST NOT be more than one property or constant declared per statement.
67-
'single_class_element_per_statement' => true,
68-
// Convert double quotes to single quotes for simple strings.
69-
'single_quote' => true,
70-
// Arguments lists, array destructuring lists, arrays that are multi-line, match-lines and parameters lists must have a trailing comma.
71-
// removed "match" and "parameters" for PHP7
72-
// see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/8308
73-
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arguments', 'array_destructuring', 'arrays']],
74-
// A single space or none should be around union type and intersection type operators.
75-
'types_spaces' => true,
76-
]);
121+
->withConfiguredRule(
122+
// A single space or none should be around union type and intersection type operators
123+
PhpCsFixer\Whitespace\TypesSpacesFixer::class,
124+
['space' => 'none'],
125+
);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![https://packagist.org/packages/openmage/magento-lts](https://poser.pugx.org/openmage/magento-lts/d/total.svg)
44
![https://github.com/openmage/magento-lts/actions/workflows/security-php.yml](https://github.com/openmage/magento-lts/actions/workflows/security-php.yml/badge.svg)
55
![https://github.com/OpenMage/magento-lts/actions/workflows/workflow.yml](https://github.com/OpenMage/magento-lts/actions/workflows/workflow.yml/badge.svg)
6-
![https://www.php-fig.org/per/coding-style/](https://img.shields.io/badge/Code-PER2.0-white.svg)
6+
![https://www.php-fig.org/per/coding-style/](https://img.shields.io/badge/Code-PER3.0-white.svg)
77

88
# Magento - Long Term Support
99

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"phpunit/phpunit": "^9.6",
7878
"rector/rector": "^2.1.12",
7979
"squizlabs/php_codesniffer": "^3.7",
80+
"symplify/easy-coding-standard": "^13.0",
8081
"symplify/vendor-patches": "^12.0, !=12.0.5, !=12.0.6"
8182
},
8283
"replace": {
@@ -171,8 +172,8 @@
171172
"n98/magerun": "The n98 magerun cli tools provides some handy tools to work with Magento from command line."
172173
},
173174
"scripts": {
174-
"php-cs-fixer:test": "vendor/bin/php-cs-fixer fix --dry-run --diff",
175-
"php-cs-fixer:fix": "vendor/bin/php-cs-fixer fix",
175+
"php-cs-fixer:test": "vendor/bin/ecs --config=.php-cs-fixer.dist.php",
176+
"php-cs-fixer:fix": "vendor/bin/ecs --config=.php-cs-fixer.dist.php --fix",
176177
"phpmd:test": "vendor/bin/phpmd app/code/core/Mage html .phpmd.dist.xml --color --cache --cache-file .cache/.phpmd.result-cache.php --baseline-file .phpmd.dist.baseline.xml --report-file build/phpmd/report.html",
177178
"phpmd:baseline": "vendor/bin/phpmd app/code/core/Mage html .phpmd.dist.xml --color --cache --cache-file .cache/.phpmd.result-cache.php --baseline-file .phpmd.dist.baseline.xml --report-file build/phpmd/report.html --update-baseline --generate-baseline",
178179
"phpstan:test": "XDEBUG_MODE=off php vendor/bin/phpstan analyze",

composer.lock

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/content/developers/coding-style/per-2.0.md

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: PER-3.0
3+
tags:
4+
- Coding style
5+
---
6+
7+
# PER-3.0 style
8+
9+
OpenMage follows modern PER-3.0 [^1] coding style.
10+
11+
[^1]: [PER-3.0](https://www.php-fig.org/per/coding-style/) code standard

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ nav:
207207
- developers/tools/ddev.md
208208
- developers/tools/oneline.md
209209
- 'Coding Style':
210-
- developers/coding-style/per-2.0.md
210+
- developers/coding-style/per-version.md
211211
- 'Events':
212212
- developers/events/list.md
213213
- 'Tools':

tests/unit/Varien/ObjectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function setUpBeforeClass(): void
3737
*
3838
* @group Varien_Object
3939
*/
40-
public function testGetData($expectedResult, $setKey, $setValue, null|string $key, $index = null): void
40+
public function testGetData($expectedResult, $setKey, $setValue, ?string $key, $index = null): void
4141
{
4242
self::$subject->setData($setKey, $setValue);
4343
self::assertSame($expectedResult, self::$subject->getData($key, $index));

0 commit comments

Comments
 (0)