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 few another fixers #105

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5a5d6fd
Feat: Re-add fixers for property/parameter/return types and construct…
OndraM May 13, 2024
e7ff98d
Feat: Add PhpdocToCommentFixer (part of #94)
OndraM Apr 27, 2024
90a6a30
Feat: Enable all aliases in NoAliasFunctionsFixer (part of #94)
OndraM Apr 27, 2024
9450224
Feat: Replace UnionTypeHintFormatSniff with TypesSpacesFixer
OndraM Apr 28, 2024
2367d78
Feat: Add AssignNullCoalescingToCoalesceEqualFixer (part of #94)
OndraM Apr 28, 2024
5d7d2e6
Feat: Add NoSpaceAroundDoubleColonFixer (part of #94)
OndraM May 6, 2024
e2fc584
Feat: Add ClassReferenceNameCasingFixer (part of #94)
OndraM May 6, 2024
29be507
Feat: Add NoUnneededImportAliasFixer (part of #94)
OndraM May 6, 2024
bb42d03
Feat: Add SingleLineCommentSpacingFixer (part of #94)
OndraM May 6, 2024
2d47c2f
Feat: Add OrderedClassElementsFixer with specified order of elements …
OndraM May 6, 2024
f8a713a
Test: Add tests for NoTrailingCommaInSinglelineFixer and array declar…
OndraM May 6, 2024
5c3498c
Feat: Add PhpdocAlignFixer (part of #94)
OndraM May 6, 2024
158ccbd
Feat: Change phpdoc order so that `@throws` is after `@return`
OndraM May 6, 2024
6f6f49e
Feat: Add ArrayIndentationFixer (part of #94)
OndraM May 6, 2024
7369bfa
Feat: Remove space after `fn` declaration as defined in PER2.0
OndraM May 6, 2024
9f9bb18
Feat: Empty body of function should be abberviated `{}` as defined in…
OndraM May 6, 2024
dda2b59
Feat: Reconfigure TrailingCommaInMultilineFixer in accordance with PE…
OndraM May 7, 2024
5efeb00
Docs: Add comments to various fixers
OndraM May 7, 2024
601faed
Feat: Add FullyQualifiedStrictTypesFixer to import all FQCN (part of …
OndraM May 7, 2024
899ca55
Feat: Add MultilineStringToHeredocFixer (part of #94)
OndraM May 7, 2024
a13a176
Feat: Add LongToShorthandOperatorFixer (part of #94)
OndraM May 7, 2024
cf72e48
Feat: Add rules for attributes handling (AttributeEmptyParenthesesFix…
OndraM May 7, 2024
d6bbd2c
Feat: Add NoUnneededControlParenthesesFixer fixer (part of #94)
OndraM May 7, 2024
723d452
Feat: Add NullableTypeDeclarationFixer to standardize nullable declar…
OndraM May 9, 2024
ab8cd81
Feat: Add NullableTypeDeclarationForDefaultNullValueFixer (part of #94)
OndraM May 9, 2024
e55a518
Test: Refactor integration tests to make them more versatile
OndraM May 10, 2024
2a3ccec
Feat: Add OctalNotationFixer for explicit octal notation using 0o in …
OndraM May 10, 2024
18d1dcb
Feat: Add SimpleToComplexStringVariableFixer for PHP 8.2+ (part of #94)
OndraM May 10, 2024
b640a68
Test: Add test for PhpDoc property fixer/sniff
OndraM May 13, 2024
cbfeadd
Test: Add test for PhpDoc param and return type hints fixers/sniffs
OndraM May 13, 2024
4be6d5c
Test: Add test for current phpunit fixers
OndraM May 13, 2024
1f5454b
Docs: Mention PhpUnitAttributesFixer in README
OndraM May 13, 2024
3f2a451
Feat: Add PhpUnitFqcnAnnotationFixer
OndraM May 13, 2024
815901d
Feat: Add PhpUnitMethodCasingFixer
OndraM May 13, 2024
b660c08
Docs: Update comments - add missing and remove irrelevant
OndraM May 16, 2024
0f8e541
Add MagicMethodCasingFixer to check correct magic method casing
OndraM May 21, 2024
f83f191
Add PhpdocTrimConsecutiveBlankLineSeparationFixer to removes extra bl…
OndraM May 21, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ return ECSConfig::configure()
__DIR__ . '/vendor/lmc/coding-standard/ecs.php',
]
)
->withRules(
[
// PHPUnit attributes must be used over their respective PHPDoc-based annotations. (Use with PHPUnit 10+.)
PhpUnitAttributesFixer::class,
]
)
// Enforce line-length to 120 characters
->withConfiguredRule(LineLengthSniff::class, ['absoluteLineLimit' => 120])
// Tests must have @test annotation
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.13.2",
Expand Down
2 changes: 1 addition & 1 deletion ecs-internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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
206 changes: 167 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;
}
}
149 changes: 146 additions & 3 deletions tests/Integration/Fixtures/Basic.correct.php.inc
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
<?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
$lambdaWithUnusedImport = function () { return 'foo'; };
// NoUselessSprintfFixer
$uselessSprintf = 'bar';
// NoSpaceAroundDoubleColonFixer
$className = DateTime::class;
// ClassReferenceNameCasingFixer
$date = new \DateTime();

$aliasedClass = new Foo();

// SingleSpaceAfterConstructFixer
if ($a == $b) {
return true;
Expand All @@ -22,9 +48,126 @@ 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;
// MultilineStringToHeredocFixer
$multilineString = <<<'EOD'
This string
spans multiple lines
but should be heredoc instead
EOD;

// SingleLineCommentSpacingFixer
// This comment should have space on the beginning
/* So should this one, also with space on the end */

// 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