espocrm icon indicating copy to clipboard operation
espocrm copied to clipboard

cs fixes + rulesets

Open arkadiyasuratov opened this issue 2 years ago • 2 comments

Please treat this as a suggestion rather a merge request. Just want to show that the project could use some form of automated cs fixer.

My prefer tool is easy-coding-standard with some common rulesets from PHP_CodeSniffer and PHP-CS-Fixer. Since there's a lot going on each commit (with description ...) represents a single cs rule with all rule checkers applied all at once on last commit 226eb0716b12365c6fe92e32be2d9999632a3e50.

If you want to integrate ecs directly into the project or create a separate repo ie. espocrm/coding-standard here is the config I've used. If not you can just cherry-pick what you like or disregard this PR entirely.

<?php //ecs.php

declare(strict_types=1);

return static function (\Symplify\EasyCodingStandard\Config\ECSConfig $ecsConfig): void {
    $ecsConfig->paths([
        __DIR__ . '/application',
        __DIR__ . '/tests',

        __DIR__ . '/dev',
        __DIR__ . '/install',

        __DIR__ . '/bootstrap.php',
        __DIR__ . '/rebuild.php',
        __DIR__ . '/clear_cache.php',
        __DIR__ . '/daemon.php',
        __DIR__ . '/cron.php',
        __DIR__ . '/extension.php',
        __DIR__ . '/command.php',
        __DIR__ . '/websocket.php',
        __DIR__ . '/upgrade.php',
        __DIR__ . '/preload.php',
        __DIR__ . '/index.php',
    ]);

    /**
     * Fix SPACES.
     */
    $ecsConfig->rules([
        \Symplify\CodingStandard\Fixer\Spacing\StandaloneLinePromotedPropertyFixer::class,
        \PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer::class,
        \Symplify\CodingStandard\Fixer\Spacing\NewlineServiceDefinitionConfigFixer::class,
        \PhpCsFixer\Fixer\CastNotation\CastSpacesFixer::class,
        \PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer::class,
        \PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer::class,
        \PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer::class,
        \PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer::class,
        \PhpCsFixer\Fixer\Phpdoc\PhpdocSingleLineVarSpacingFixer::class,
        \PhpCsFixer\Fixer\NamespaceNotation\NoLeadingNamespaceWhitespaceFixer::class,
        \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer::class,
        \PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer::class,
        \PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer::class,
        \PhpCsFixer\Fixer\Operator\TernaryOperatorSpacesFixer::class,
        \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer::class,
    ]);
    $ecsConfig->ruleWithConfiguration(
        \PhpCsFixer\Fixer\Operator\ConcatSpaceFixer::class,
        [
            'spacing' => 'one'
        ]
    );
    $ecsConfig->ruleWithConfiguration(
        \PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff::class,
        [
            'ignoreBlankLines' => \false
        ]
    );
    $ecsConfig->ruleWithConfiguration(
        \PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer::class,
        [
            'operators' => [
                '=>' => 'single_space',
                '=' => 'single_space'
            ]
        ]
    );

    /**
     * Fix ARRAYS.
     */
    $ecsConfig->rules([
        \PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer::class,
        \PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer::class,
        \PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer::class,
        \PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer::class,
    ]);
    $ecsConfig->ruleWithConfiguration(
        \PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer::class,
        [
            'syntax' => 'short'
        ]
    );

    /**
     * CLEAN CODE ruleset.
     */
    $ecsConfig->rules([
        \Symplify\CodingStandard\Fixer\Commenting\ParamReturnAndVarTagMalformsFixer::class,
        \PhpCsFixer\Fixer\Semicolon\NoEmptyStatementFixer::class,
        \PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer::class,
        \PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer::class
    ]);
};

arkadiyasuratov avatar May 28 '22 11:05 arkadiyasuratov

I have an intention to incorporate a style checker/fixer in the future, just some basic stuff like PSR-12 compliance, w/o strict rules that are not in the standard.

yurikuzn avatar Jun 03 '22 08:06 yurikuzn

I have an intention to incorporate a style checker/fixer in the future

Looking forward to it. While I'm guessing this is not the most important thing right now I still hope you'll find this PR useful as a reference point.

For basic PSR-12 rules ecs might be redundant since it's already using sets from PHP-CS-Fixer, but in the end it's up to you.

arkadiyasuratov avatar Jun 03 '22 09:06 arkadiyasuratov