Skip to content

Commit

Permalink
Review Codely's rules
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaudo committed Mar 7, 2024
1 parent 7f8eb0b commit 5bf804f
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 75 deletions.
59 changes: 57 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,62 @@ Hereldar's Easy Coding Standards configuration
[license-badge]: https://img.shields.io/badge/license-MIT-brightgreen.svg
[license-url]: LICENSE

Opinionated linting configuration considering modern PHP best
practices and providing consistency.
Opinionated linting configuration inspired on [Codely's Coding Style](https://github.com/CodelyTV/php-coding_style-codely).

How to use
----------

### Installation

Install the package via Composer:

```bash
composer require --dev hereldar/coding-style
```

### Configuration

Create a `ecs.php` file in the root of your project:

```php
use Hereldar\CodingStyle;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig
::configure()
->withSets([
CodingStyle::PROJECTS,
])
->withPaths([
__DIR__.'/src',
__DIR__.'/tests',
])
->withRootFiles();
```

### Usage

Execute the following command to see the suggested changes:

```bash
vendor/bin/ecs
```

To actually fix your code, add `--fix`:

```bash
vendor/bin/ecs --fix
```

For more information, check the [Easy Coding Standard documentation](https://github.com/easy-coding-standard/easy-coding-standard)

What it does
------------

Checks the code style of your project using:

- [PER Coding Style](https://www.php-fig.org/per/coding-style/)
- [Symfony](https://symfony.com/doc/current/contributing/code/standards.html)
- Some custom rules (you can see them [here](sets)).

**Currently under development.**
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"symplify/easy-coding-standard": "^12.0"
"symplify/easy-coding-standard": "^12.1"
},
"autoload": {
"psr-4": {
Expand Down
101 changes: 101 additions & 0 deletions sets/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

use Hereldar\CodingStyle\Rules\ClarifyingParenthesesAroundComparisonsFixer;
use Hereldar\CodingStyle\Rules\MultilineWhitespaceBeforeDoubleColonFixer;
use PhpCsFixer\Fixer\Basic\SingleLineEmptyBodyFixer;
use PhpCsFixer\Fixer\ClassNotation\SelfStaticAccessorFixer;
use PhpCsFixer\Fixer\Comment\MultilineCommentOpeningClosingFixer;
use PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer;
use PhpCsFixer\Fixer\ControlStructure\SimplifiedIfReturnFixer;
use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer;
use PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveIssetsFixer;
use PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer;
use PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocOrderByValueFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertInternalTypeFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer;
use PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer;
use PhpCsFixer\Fixer\StringNotation\StringImplicitBackslashesFixer;
use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
use Symplify\CodingStandard\Fixer\Strict\BlankLineAfterStrictTypesFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Option as ECS;

return ECSConfig
::configure()
->withPhpCsFixerSets(
php80Migration: true,
php80MigrationRisky: true,
per: true,
perRisky: true,
symfony: true,
symfonyRisky: true,
)
->withRules([
ArrayIndentationFixer::class,
BlankLineAfterStrictTypesFixer::class,
ClarifyingParenthesesAroundComparisonsFixer::class,
CombineConsecutiveIssetsFixer::class,
CombineConsecutiveUnsetsFixer::class,
ExplicitStringVariableFixer::class,
MethodChainingIndentationFixer::class,
MultilineCommentOpeningClosingFixer::class,
MultilineWhitespaceBeforeDoubleColonFixer::class,
NoUselessElseFixer::class,
PhpdocOrderByValueFixer::class,
PhpUnitDedicateAssertFixer::class,
PhpUnitDedicateAssertInternalTypeFixer::class,
PhpUnitExpectationFixer::class,
SelfStaticAccessorFixer::class,
SimplifiedIfReturnFixer::class,
SingleLineEmptyBodyFixer::class,
])
->withConfiguredRule(
GlobalNamespaceImportFixer::class,
['import_classes' => true, 'import_constants' => false, 'import_functions' => false],
)
->withConfiguredRule(
OperatorLinebreakFixer::class,
['position' => 'beginning'],
)
->withConfiguredRule(
PhpdocAlignFixer::class,
['align' => 'left'],
)
->withConfiguredRule(
PhpdocNoAliasTagFixer::class,
['replacements' => ['type' => 'var', 'link' => 'see']],
)
->withConfiguredRule(
PhpdocSeparationFixer::class,
['groups' => [
['deprecated', 'link', 'see', 'since'],
['author', 'copyright', 'license'],
['category', 'package', 'subpackage'],
['property', 'property-read', 'property-write', 'phpstan-property', 'phpstan-property-read', 'phpstan-property-write', 'psalm-property', 'psalm-property-read', 'psalm-property-write'],
['pure', 'phpstan-pure', 'psalm-pure'],
['param', 'phpstan-param', 'psalm-param'],
['return', 'phpstan-return', 'psalm-return'],
['throws', 'phpstan-throws', 'psalm-throws'],
]],
)
->withConfiguredRule(
PhpdocToCommentFixer::class,
['ignored_tags' => ['var', 'phpstan-var', 'psalm-var', 'phpstan-ignore-next-line', 'psalm-suppress']],
)
->withConfiguredRule(
StringImplicitBackslashesFixer::class,
['single_quoted' => 'ignore'],
)
->withSpacing(
indentation: ECS::INDENTATION_SPACES,
lineEnding: PHP_EOL
);
2 changes: 1 addition & 1 deletion sets/libraries.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
return ECSConfig
::configure()
->withSets([
CodingStyle::PROJECTS,
CodingStyle::BASE,
])
->withConfiguredRule(
NativeConstantInvocationFixer::class,
Expand Down
80 changes: 10 additions & 70 deletions sets/projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,29 @@

declare(strict_types=1);

use Hereldar\CodingStyle\Rules\ClarifyingParenthesesAroundComparisonsFixer;
use Hereldar\CodingStyle\Rules\MultilineWhitespaceBeforeDoubleColonFixer;
use PhpCsFixer\Fixer\Comment\MultilineCommentOpeningClosingFixer;
use Hereldar\CodingStyle;
use PhpCsFixer\Fixer\ClassNotation\FinalClassFixer;
use PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer;
use PhpCsFixer\Fixer\ClassUsage\DateTimeImmutableFixer;
use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer;
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer;
use PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveIssetsFixer;
use PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer;
use PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocOrderByValueFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer;
use PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer;
use PhpCsFixer\Fixer\StringNotation\StringImplicitBackslashesFixer;
use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig
::configure()
->withPhpCsFixerSets(
php80Migration: true,
php80MigrationRisky: true,
per: true,
perRisky: true,
symfony: true,
symfonyRisky: true,
)
->withSets([
CodingStyle::BASE,
])
->withSkip([
NativeConstantInvocationFixer::class,
NativeFunctionInvocationFixer::class,
])
->withRules([
ArrayIndentationFixer::class,
ClarifyingParenthesesAroundComparisonsFixer::class,
CombineConsecutiveIssetsFixer::class,
CombineConsecutiveUnsetsFixer::class,
ExplicitStringVariableFixer::class,
MethodChainingIndentationFixer::class,
MultilineCommentOpeningClosingFixer::class,
MultilineWhitespaceBeforeDoubleColonFixer::class,
PhpdocOrderByValueFixer::class,
DateTimeImmutableFixer::class,
FinalClassFixer::class,
ProtectedToPrivateFixer::class,
])
->withConfiguredRule(
GlobalNamespaceImportFixer::class,
['import_classes' => true, 'import_constants' => false, 'import_functions' => false],
)
->withConfiguredRule(
OperatorLinebreakFixer::class,
['position' => 'beginning'],
)
->withConfiguredRule(
PhpdocAlignFixer::class,
['align' => 'left'],
)
->withConfiguredRule(
PhpdocNoAliasTagFixer::class,
['replacements' => ['type' => 'var', 'link' => 'see']],
)
->withConfiguredRule(
PhpdocSeparationFixer::class,
['groups' => [
['deprecated', 'link', 'see', 'since'],
['author', 'copyright', 'license'],
['category', 'package', 'subpackage'],
['property', 'property-read', 'property-write', 'phpstan-property', 'phpstan-property-read', 'phpstan-property-write', 'psalm-property', 'psalm-property-read', 'psalm-property-write'],
['pure', 'phpstan-pure', 'psalm-pure'],
['param', 'phpstan-param', 'psalm-param'],
['return', 'phpstan-return', 'psalm-return'],
['throws', 'phpstan-throws', 'psalm-throws'],
]],
)
->withConfiguredRule(
PhpdocToCommentFixer::class,
['ignored_tags' => ['var', 'phpstan-var', 'psalm-var', 'phpstan-ignore-next-line', 'psalm-suppress']],
)
->withConfiguredRule(
StringImplicitBackslashesFixer::class,
['single_quoted' => 'ignore'],
)
->withConfiguredRule(
YodaStyleFixer::class,
['equal' => false, 'identical' => false, 'less_and_greater' => false],
Expand Down
1 change: 1 addition & 0 deletions src/CodingStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

final class CodingStyle
{
public const BASE = __DIR__.'/../sets/base.php';
public const PROJECTS = __DIR__.'/../sets/projects.php';
public const LIBRARIES = __DIR__.'/../sets/libraries.php';
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private function findIndentBeforeNextObjectOperators(int $index, Tokens $tokens)
$previousIndex = $index - 1;
$previous = $tokens[$previousIndex];

if (str_contains($previous->getContent(), "\n")) {
if (\str_contains($previous->getContent(), "\n")) {
return WhitespacesAnalyzer::detectIndent($tokens, $index);
}
}
Expand Down

0 comments on commit 5bf804f

Please sign in to comment.