Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new fixers #95

Merged
merged 36 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6d5d3b9
Feat: Add PhpdocToCommentFixer (part of #94)
OndraM Apr 27, 2024
04e0a49
Feat: Enable all aliases in NoAliasFunctionsFixer (part of #94)
OndraM Apr 27, 2024
b78d8e6
Feat: Replace UnionTypeHintFormatSniff with TypesSpacesFixer
OndraM Apr 28, 2024
2eeb811
Feat: Add AssignNullCoalescingToCoalesceEqualFixer (part of #94)
OndraM Apr 28, 2024
fa1ce8b
Feat: Add NoSpaceAroundDoubleColonFixer (part of #94)
OndraM May 6, 2024
42257c6
Feat: Add ClassReferenceNameCasingFixer (part of #94)
OndraM May 6, 2024
7e52ba6
Feat: Add NoUnneededImportAliasFixer (part of #94)
OndraM May 6, 2024
8fe4222
Feat: Add SingleLineCommentSpacingFixer (part of #94)
OndraM May 6, 2024
80b71e2
Feat: Add OrderedClassElementsFixer with specified order of elements …
OndraM May 6, 2024
c7dc404
Test: Add tests for NoTrailingCommaInSinglelineFixer and array declar…
OndraM May 6, 2024
c04b715
Feat: Add PhpdocAlignFixer (part of #94)
OndraM May 6, 2024
46ebf2e
Feat: Change phpdoc order so that `@throws` is after `@return`
OndraM May 6, 2024
b16a398
Feat: Add ArrayIndentationFixer (part of #94)
OndraM May 6, 2024
ce973e3
Feat: Remove space after `fn` declaration as defined in PER2.0
OndraM May 6, 2024
8caa26a
Feat: Empty body of function should be abberviated `{}` as defined in…
OndraM May 6, 2024
aa2853b
Feat: Reconfigure TrailingCommaInMultilineFixer in accordance with PE…
OndraM May 7, 2024
1b89496
Docs: Add comments to various fixers
OndraM May 7, 2024
6399ba0
Feat: Add FullyQualifiedStrictTypesFixer to import all FQCN (part of …
OndraM May 7, 2024
a65d4db
Feat: Add MultilineStringToHeredocFixer (part of #94)
OndraM May 7, 2024
0709431
Feat: Add LongToShorthandOperatorFixer (part of #94)
OndraM May 7, 2024
e94dc5d
Feat: Add rules for attributes handling (AttributeEmptyParenthesesFix…
OndraM May 7, 2024
eb1479e
Feat: Add NoUnneededControlParenthesesFixer fixer (part of #94)
OndraM May 7, 2024
ad7f959
Feat: Add NullableTypeDeclarationFixer to standardize nullable declar…
OndraM May 9, 2024
d08cfc8
Feat: Add NullableTypeDeclarationForDefaultNullValueFixer (part of #94)
OndraM May 9, 2024
84c9c7e
Test: Refactor integration tests to make them more versatile
OndraM May 10, 2024
5748e6c
Feat: Add OctalNotationFixer for explicit octal notation using 0o in …
OndraM May 10, 2024
0f34419
Feat: Add SimpleToComplexStringVariableFixer for PHP 8.2+ (part of #94)
OndraM May 10, 2024
24fc9ea
Test: Add test for PhpDoc property fixer/sniff
OndraM May 13, 2024
5ec11d9
Test: Add test for PhpDoc param and return type hints fixers/sniffs
OndraM May 13, 2024
16c3cfc
Test: Add test for current phpunit fixers
OndraM May 13, 2024
0887cf3
Feat: Add PhpUnitFqcnAnnotationFixer
OndraM May 13, 2024
b1bfeb4
Feat: Add PhpUnitMethodCasingFixer
OndraM May 13, 2024
409d729
Docs: Update comments - add missing and remove irrelevant
OndraM May 16, 2024
8a35296
Add MagicMethodCasingFixer to check correct magic method casing
OndraM May 21, 2024
117d879
Add PhpdocTrimConsecutiveBlankLineSeparationFixer to removes extra bl…
OndraM May 21, 2024
a2dfe95
Feat: Remove SingleLineCommentSpacingFixer and MultilineStringToHered…
OndraM May 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ return ECSConfig::configure()
[
// PHPUnit attributes must be used over their respective PHPDoc-based annotations. (Use with PHPUnit 10+.)
PhpUnitAttributesFixer::class,
// Single-line comments must have proper spacing.
// Single-line comments must have proper spacing (one space after `//`).
SingleLineCommentSpacingFixer::class,
// Convert multiline string to heredoc or nowdoc.
MultilineStringToHeredocFixer::class,
]
)
// Enforce line-length to 120 characters.
Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ $ vendor/bin/ecs check --ansi src/ tests/ # old
$ vendor/bin/ecs check --ansi # new
```

### 6. Add some optional rules
On top of default rules included in ecs.php, there are some more opinionated ones you may want to add.

These suggested rules are listed in [README.md](https://github.com/lmc-eu/php-coding-standard?tab=readme-ov-file#add-custom-checks-or-override-default-settings).

### 5. BE CAREFUL WITH SUGGESTED CHANGES! ⚠️

Some of the new default fixers introduced in php-coding-standard 4.0 and 4.1 suggest changes, which - if not
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
],
"require": {
"php": "^8.0",
"friendsofphp/php-cs-fixer": "^3.0",
"slevomat/coding-standard": "^8.0",
"friendsofphp/php-cs-fixer": "^3.47.1",
"slevomat/coding-standard": "^8.6",
"squizlabs/php_codesniffer": "^3.9",
"symplify/easy-coding-standard": "^12.1.5"
"symplify/easy-coding-standard": "^12.1.9"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42.0",
Expand Down
2 changes: 1 addition & 1 deletion ecs-internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

/**
/*
* Internal rules configuration for the lmc/coding-standard project itself
*/
return ECSConfig::configure()
Expand Down
169 changes: 130 additions & 39 deletions ecs.php

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/Helper/SniffClassWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

final class SniffClassWrapper
{
public function __construct(private File $file, private int $position, private Naming $naming)
{
}
public function __construct(private File $file, private int $position, private Naming $naming) {}

public function getClassName(): ?string
{
Expand Down
82 changes: 70 additions & 12 deletions tests/Integration/CodingStandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
*/
class CodingStandardTest extends TestCase
{
private string $tempFixtureFile;

/** @after */
protected function cleanUpTempFixtureFile(): void
{
unlink($this->tempFixtureFile);
}

/**
* @test
* @dataProvider provideFilesToFix
*/
public function shouldFixFile(string $wrongFile, string $correctFile): void
{
// copy wrongFile to a new temporary file
$fixtureFile = tempnam(sys_get_temp_dir(), 'ecs-test');
if ($fixtureFile === false) {
$this->fail('Could not create temporary file');
}
copy($wrongFile, $fixtureFile);

shell_exec(
__DIR__ . '/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix ' . $fixtureFile,
);
$fixedFile = $this->runEcsCheckOnFile($wrongFile);

$this->assertStringEqualsFile($correctFile, file_get_contents($fixtureFile));
unlink($fixtureFile);
$this->assertStringEqualsFile($correctFile, file_get_contents($fixedFile));
}

public function provideFilesToFix(): array
Expand All @@ -38,6 +36,66 @@ public function provideFilesToFix(): array
__DIR__ . '/Fixtures/NewPhpFeatures.wrong.php.inc',
__DIR__ . '/Fixtures/NewPhpFeatures.correct.php.inc',
],
'PhpDoc' => [__DIR__ . '/Fixtures/PhpDoc.wrong.php.inc', __DIR__ . '/Fixtures/PhpDoc.correct.php.inc'],
'PhpUnit' => [__DIR__ . '/Fixtures/PhpUnit.wrong.php.inc', __DIR__ . '/Fixtures/PhpUnit.correct.php.inc'],
];
}

/**
* @test
* @requires PHP >= 8.1
*/
public function shouldFixPhp81(): void
{
$fixedFile = $this->runEcsCheckOnFile(__DIR__ . '/Fixtures/Php81.wrong.php.inc');

$this->assertStringEqualsFile(
__DIR__ . '/Fixtures/Php81.correct.php.inc',
file_get_contents($fixedFile),
);
}

/**
* @test
* @requires PHP >= 8.2
*/
public function shouldFixPhp82(): void
{
$fixedFile = $this->runEcsCheckOnFile(__DIR__ . '/Fixtures/Php82.wrong.php.inc');

$this->assertStringEqualsFile(
__DIR__ . '/Fixtures/Php82.correct.php.inc',
file_get_contents($fixedFile),
);
}

private function runEcsCheckOnFile(string $file): string
{
$fixtureFile = $this->initTempFixtureFile();

// copy source of wrongFile to a temporary file which will be modified by ECS
copy($file, $fixtureFile);

shell_exec(
sprintf(
'%s/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix %s',
__DIR__,
$fixtureFile,
),
);

return $fixtureFile;
}

private function initTempFixtureFile(): string
{
// Create new file in temporary directory
$fixtureFile = tempnam(sys_get_temp_dir(), 'ecs-test');
if ($fixtureFile === false) {
$this->fail('Could not create temporary file');
}
$this->tempFixtureFile = $fixtureFile; // store to be able to remove it later

return $fixtureFile;
}
}
139 changes: 136 additions & 3 deletions tests/Integration/Fixtures/Basic.correct.php.inc
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
<?php declare(strict_types=1);

class Basic
namespace Lmc\CodingStandard\Integration\Fixtures;

// NoUnneededImportAliasFixer
use Bar\Foo;
use Some\Other\Namespace\AbstractBasic;

class Basic extends AbstractBasic implements InterfaceFromThisNamespace // FullyQualifiedStrictTypesFixer
{
use SomeUsefulTrait; // OrderedClassElementsFixer
public const FOO = 'foo'; // ClassAttributesSeparationFixer

public function isEqual($a, $b) // VisibilityRequiredFixer
public const MY_PUBLIC_CONST = 333; // OrderedClassElementsFixer

protected int $myProperty = 666; // OrderedClassElementsFixer

// MagicMethodCasingFixer, OrderedClassElementsFixer
public function __toString(): string
{
return '';
}

public function isEqual($a, ?string $b): ?bool // VisibilityRequiredFixer, CompactNullableTypeDeclarationFixer
{
// TrimArraySpacesFixer
$fooBar = ['a', 'b'];
// NoTrailingCommaInSinglelineFixer
mb_strlen('foobar');
// MbStrFunctionsFixer
$bazLength = mb_strlen('baz');
// LambdaNotUsedImportFixer
Expand All @@ -16,6 +35,13 @@ class Basic
$uselessSprintf = 'bar';
// StrictParamFixer
$useStrictParam = in_array(1337, $fooBar, true);
// NoSpaceAroundDoubleColonFixer
$className = DateTime::class;
// ClassReferenceNameCasingFixer
$date = new \DateTime();

$aliasedClass = new Foo();

// SingleSpaceAfterConstructFixer, StrictComparisonFixer
if ($a === $b || $bazLength !== 3) {
return true;
Expand All @@ -24,9 +50,116 @@ class Basic
return false; // BlankLineBeforeStatementFixer
}

public function fooBar(mixed $foo): mixed
public function fooBar(mixed $foo): mixed // FunctionDeclarationFixer
{
// MagicConstantCasingFixer
$magicConstant = __DIR__;
$value = 5;
// FunctionDeclarationFixer
$function = function ($foo) use ($value) {
return $foo + $value;
};
// FunctionDeclarationFixer
$fn = fn($foo) => $foo + $value;

// PhpdocToCommentFixer
/*
* Phpdoc used instead of plain comment
*/
if ($foo === 'bar') {
// NoAliasFunctionsFixer
$baz = implode(',', ['foo', 'bar']);
}

$i = 3;
$i += 6; // LongToShorthandOperatorFixer
$i *= 2; // LongToShorthandOperatorFixer
$text = 'foo';
$text .= 'bar'; // LongToShorthandOperatorFixer

switch ($i) {
case 1: // NoUnneededControlParenthesesFixer
$i++;
break;
default:
break;
}

// HeredocIndentationFixer
$heredoc = <<<HEREDOC
This is a
multiline heredoc string. It contains $foo.
It should be indented, though.
HEREDOC;
// HeredocIndentationFixer
$newdoc = <<<'NEWDOC'
This is a $newdoc, where variables are not expanded.
NEWDOC;

// TernaryToElvisOperatorFixer
return ($foo ?: 'not true');
}

public function arrayDeclarations(): void
{
$empty = [];

$singleLineArray = ['foo', 'bar', 'baz'];
$singleLineArray2 = [1, 2, 3];

$multiLineAssociative1 = [
'foo' => 'bar',
'baz' => 'bat',
];

$multiLineAssociative2 = [
'foo' => 'bar',
'baz' => 'bat',
'bak' => 'baz',
];

$multiLineAssociative3 = [
'firstKey' => 'bar',
'thisIsSecondKey' => 'bat',
'third' => 'bat',
];
}

public function emptyFunction1(): void {} // SingleLineEmptyBodyFixer

public function emptyFunction2(
$arg,
): void {} // SingleLineEmptyBodyFixer

// TrailingCommaInMultilineFixer
public function multiline(
$arg1,
$arg2,
$arg3,
): void {
// TrailingCommaInMultilineFixer
$isSet = isset(
$arg1,
$arg2,
$arg3,
);

$sprintf = sprintf(
'%s%s',
'bar',
'baz',
);

foo(
1,
2,
);
}

public function withParameters(
// NullableTypeDeclarationFixer
?string $nullableValue,
// NullableTypeDeclarationForDefaultNullValueFixer
?string $anotherNullableValue = null,
): void {}
}
Loading