Skip to content

Commit 117338e

Browse files
authored
Modernize coding standard: PHP 8.4, PER-CS 3, Rector, dedup rules (#42)
* Improve PHP-CS-Fixer config sharing, require PHP 8.4, and slim PHP_CodeSniffer (#38) * feat: improve PHP-CS-Fixer config sharing and require PHP 8.4 - Add @PHP8x4Migration, @PHP8x4Migration:risky and @PHPUnit10x0Migration:risky rulesets for automatic syntax/test modernization, plus numeric_literal_separator. - Mark Config and Rules as @api to declare the supported BC surface. - Add a smoke test covering Rules::get() and Config::create(). - Fix the stale raw-require path example in the rules file docblock. - Bump required PHP to ^8.4 and drop 8.3 from the CI matrix. - Restructure README: PHP-CS-Fixer first, then PHP_CodeSniffer, for clarity. * refactor: drop phpcs sniffs already covered by PHP-CS-Fixer Now that the shared PHP-CS-Fixer config owns formatting, stop checking the same issues with PHP_CodeSniffer (PHP-CS-Fixer is faster and auto-fixes). Removed 30 sniffs whose concern is fully handled by an enabled fixer rule (verified against the expanded 151-rule set), e.g. array syntax/indentation, cast and operator spacing, trailing commas, yoda style, null coalesce, short list, import ordering, scope indentation, superfluous whitespace. Kept sniffs with no fixer equivalent in our config: single_quote, native_function_casing, AND/OR operators, == vs ===, member var scope, annotation-aware unused imports, and const/property spacing. Generic.CodeAnalysis.EmptyStatement is kept: it detects empty control-structure bodies, not stray semicolons (that is Slevomat UselessSemicolon, which is removed). * refactor: drop PSR12 phpcs ref, keep only PSR1 structural sniffs PHP-CS-Fixer's @PER-CS3x0 (PER-CS supersedes PSR-12) already enforces every PSR12 formatting concern, verified against the expanded fixer ruleset. Replace the whole PSR12 ref with the three PSR1 sniffs PHP-CS-Fixer cannot cover: side-effect separation, one-symbol-per-file, and method camelCase naming. Const visibility stays enforced via SlevomatCodingStandard.Classes.ClassConstantVisibility. README now states PHP-CS-Fixer is primary (owns formatting) and PHP_CodeSniffer is supplementary, not a complete standard on its own. * fix: stop forcing arrow functions and numeric literal separators Both came from the newly added rules and produced unwanted rewrites: - use_arrow_functions (via @PHP8x4Migration:risky) converted readable long closures to fn(); disable it to match the existing phpcs intent (SlevomatCodingStandard.Functions.RequireArrowFunction is kept disabled). - numeric_literal_separator rewrote literals like 2020 to 2_020; drop it. * Inline shared PHP-CS-Fixer rules into the Rules class (#39) * refactor: inline shared rules into Rules class, drop root rules file Move the rule array from the root .php-cs-fixer-rules.php into Rules::get() so the ruleset is a single PSR-4 autoloaded source with no file-path indirection (dirname(__DIR__, 2) require). The raw-require fallback the root file provided was never documented as public API, so this is safe on the unreleased 1.x line. Also keep '@api' out of general_phpdoc_annotation_remove: the standard was stripping the @api tags that mark the supported public surface (found by running the shared config on this package's own code). * chore: suppress FunctionLength on the inlined rule list * ci: dogfood the shared PHP-CS-Fixer config on this package (#40) Add a .php-cs-fixer.dist.php that lints this package with its own shared config, plus a strict PHP-CS-Fixer CI gate so a config regression (e.g. a rule renamed or removed by a dependency bump) fails loudly instead of shipping unnoticed. - .php-cs-fixer.dist.php: uses Config::create(); excludes vendor/.cache and the intentionally-malformed sniff test fixtures (tests/Sniffs/**); disables mb_str_functions (this package processes ASCII PHP tokens, not user-facing strings, so it should not pull in an ext-mbstring runtime dependency). - composer.json: add `php-cs-fixer` and `php-cs-fixer:check` scripts. - .github/workflows/php-cs-fixer.yml: strict dry-run gate (mirrors psalm.yml). - Apply the resulting formatting to the package source. - BladeTemplateExtractor: turn the `phpcs:disable` doc block into a line comment so PHP-CS-Fixer no longer expands it into a multi-line doc block that then conflicts with SlevomatCodingStandard.Commenting.RequireOneLineDocComment. * feat: add `general_phpdoc_tag_rename` rule to standardize `inheritdoc` as `inheritDoc` * feat: expand `phpdoc_line_span` rule configuration for improved control over docblock formatting * chore: remove redundant `setCacheFile` call from PHP-CS-Fixer config removed as too opinionated * feat: enable `global_namespace_import` and restore `ordered_class_elements` for improved control over imports and declaration order * chore: fix cache path and typo in README examples * chore: install rector, apply cs to this package * docs: reorganize for clarity * docs: add CI workflow example with auto-fix * ci: consolidate style workflows into one auto-fix workflow Replace php-cs-fixer.yml (hard gate) and format_php.yml (both referenced removed composer scripts) with a single coding-standard.yml that runs Rector, PHP-CS-Fixer and phpcbf, then commits fixes back. `composer cs` now chains both tools. README links to the workflow as a usage example. * cleanup * cleanup * cleanup toolchain * docs: cleanup * style: update line length rules in cs config * style: update line length rules in cs config * style: adjust cognitive complexity rules in cs config * style: adjust line length warning to 180 chars * style: increase max function length to 70 lines in cs config * style: exclude test files from unused variable rule * feat: move more rules from phpcs to php-cs-fixer * style: add report-only rules for abstract/final classes and static closures, clarify risky rules in CS config * style: allow mixed phpdoc tags for magic methods in CS config * style: clarify exclusion of `no_superfluous_phpdoc_tags` * docs: rearrange README headings for clarity
1 parent 6179b0f commit 117338e

35 files changed

Lines changed: 567 additions & 614 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Coding standard
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.php'
7+
- '**.xml'
8+
- 'composer.*'
9+
workflow_dispatch:
10+
11+
permissions:
12+
contents: write
13+
14+
concurrency:
15+
group: coding-standard-${{ github.ref }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
coding-standard:
20+
name: Coding standard
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 7
23+
steps:
24+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
28+
with:
29+
php-version: 8.4
30+
coverage: none
31+
32+
- name: Install Composer dependencies
33+
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
34+
with:
35+
composer-options: '--no-scripts'
36+
37+
- name: Apply Rector
38+
run: composer rector
39+
40+
- name: Apply Coding Standard
41+
run: composer cs
42+
43+
- name: Commit Fixes (if any)
44+
uses: stefanzweifel/git-auto-commit-action@b863ae1933cb653a53c021fe36dbb774e1fb9403 # v5.2.0
45+
with:
46+
commit_message: 'style: apply coding standard'

.github/workflows/format_php.yml

Lines changed: 0 additions & 78 deletions
This file was deleted.
File renamed without changes.

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525
- "lowest"
2626
- "highest"
2727
php-version:
28-
- "8.3"
2928
- "8.4"
3029
- "8.5"
3130
operating-system:

.php-cs-fixer-rules.php

Lines changed: 0 additions & 122 deletions
This file was deleted.

.php-cs-fixer.dist.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
use IxDFCodingStandard\PhpCsFixer\Config;
4+
use PhpCsFixer\Finder;
5+
6+
$finder = Finder::create()
7+
->in(__DIR__)
8+
// ->exclude(['vendor', '.cache']) // gitignored paths are already excluded, as well as other typiucal dits like vendor, node_modules, storage, cache, etc.
9+
// Sniff test fixtures are intentionally malformed; reformatting them would shift line numbers and break the sniff tests.
10+
->notPath('#^tests/Sniffs/#');
11+
12+
// mb_str_functions targets user-facing application strings. This package only processes ASCII PHP tokens,
13+
// so keep the plain byte-string functions and avoid pulling in an ext-mbstring runtime dependency.
14+
return Config::create(__DIR__, finder: $finder, ruleOverrides: ['mb_str_functions' => false])
15+
->setCacheFile('./.cache/.php-cs-fixer.cache');

IxDFCodingStandard/Helpers/ClassHelper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
namespace IxDFCodingStandard\Helpers;
44

55
/** Created based on \SlevomatCodingStandard\Helpers to have a SSoT for this internal API */
6-
final class ClassHelper extends \SlevomatCodingStandard\Helpers\ClassHelper
7-
{
8-
}
6+
final class ClassHelper extends \SlevomatCodingStandard\Helpers\ClassHelper {}

IxDFCodingStandard/PhpCsFixer/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/**
1010
* Pre-configured PHP-CS-Fixer config factory for IxDF projects.
1111
* @see README.md for usage examples
12+
* @api
1213
*/
1314
final class Config
1415
{
@@ -22,7 +23,6 @@ public static function create(string $projectDir, array $ruleOverrides = [], ?Fi
2223
$config
2324
->setParallelConfig(ParallelConfigFactory::detect())
2425
->setUsingCache(true)
25-
->setCacheFile($projectDir.'/.cache/.php-cs-fixer.cache')
2626
->setRiskyAllowed(true)
2727
->setIndent(' ')
2828
->setLineEnding("\n")

0 commit comments

Comments
 (0)