Compare commits

...

25 Commits

Author SHA1 Message Date
ErickSkrauch
390cfbd1f2
Fix composer.lock 2024-01-24 03:51:14 +01:00
ErickSkrauch
b80c930d01
Update year in the LICENSE 2024-01-23 06:07:47 +01:00
ErickSkrauch
30b1946616
Upgrade ruleset up to PHP-CS-Fixer v3.48 2024-01-23 06:07:12 +01:00
ErickSkrauch
259b5cc89c
Fix an error in the code style sample 😅 2024-01-23 05:20:06 +01:00
ErickSkrauch
ca58c15e3f
Prepare 1.0.1 release 2023-07-21 06:12:46 +02:00
ErickSkrauch
e6bac9d44b
Adjust comment_to_phpdoc fixer to exclude PHPStan error supression tags 2023-07-21 05:37:25 +02:00
ErickSkrauch
7c4ed0f859
Prepare 1.0.0 release 2023-05-17 23:35:51 +02:00
ErickSkrauch
ee30eaf4ad
Update missing CHANGELOG entry 2023-05-17 23:21:40 +02:00
ErickSkrauch
e86f8ed2cc
Remove class_definition::space_before_parenthesis param to keep the same behavior as before 2023-05-17 23:21:05 +02:00
ErickSkrauch
c794507936
Update rules 2023-05-17 23:01:23 +02:00
ErickSkrauch
5373d05c91
Depend on a stable version of the erickskrauch/php-cs-fixer-custom-fixers 2023-05-17 20:08:37 +02:00
ErickSkrauch
62932ec0f7
Resolves #16. Split repository and move all custom fixers to separate repository 2023-05-17 19:54:10 +02:00
ErickSkrauch
84dd1184b8
Prepare 0.5.0 release 2023-04-08 21:37:10 +02:00
ErickSkrauch
7d36ce912c
Fixes #13. Implemented Ely\multiline_if_statement_braces fixer 2023-04-08 21:07:39 +02:00
ErickSkrauch
e9ad4a94ab
Fix Unreleased changelog link 2023-03-28 22:15:20 +02:00
ErickSkrauch
6d572ae421
Add fixer to place promoted properties on a new line when there is more than 1 parameter 2023-03-24 01:13:24 +01:00
ErickSkrauch
050dfe6399
Revisit BlackLineAroundClassBodyFixer 2023-03-24 00:40:48 +01:00
ErickSkrauch
b85d0d5c01
Cover properties promotion cases for Ely\align_multiple_parameters fixer 2023-03-23 17:09:01 +01:00
ErickSkrauch
5ae374f6cd
Fix build shield 2023-03-23 03:44:14 +01:00
ErickSkrauch
6956e0271e
Fixes #12. Implemented Ely\align_multiline_parameters 2023-03-23 03:36:47 +01:00
ErickSkrauch
4a4f556d7b
Update LICENSE year 2023-03-22 20:41:26 +01:00
ErickSkrauch
6e9d815a1f
Fixes #9. Add space before next meaningful line of code and skip comments 2023-03-22 20:41:04 +01:00
ErickSkrauch
22dcb418fb
Update CHANGELOG 2023-03-22 20:39:19 +01:00
ErickSkrauch
2d215dc761
Fixes #10. Don't count string interpolation as a scope beginning 2023-03-22 18:42:39 +01:00
ErickSkrauch
6f14beec28
Fix PHPUnit configuration 2023-03-22 14:03:19 +01:00
20 changed files with 359 additions and 4464 deletions

View File

@ -48,11 +48,4 @@ jobs:
- name: PHP-CS-Fixer
if: matrix.php-versions == '8.1'
run: vendor/bin/php-cs-fixer fix --dry-run --format=checkstyle | cs2pr
# https://github.com/marketplace/actions/setup-php-action#phpunit
- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: PHPUnit
run: vendor/bin/phpunit
run: vendor/bin/php-cs-fixer check --format=checkstyle | cs2pr

1
.gitignore vendored
View File

@ -1,5 +1,4 @@
vendor
dist
.phpunit.result.cache
.php-cs-fixer
.php-cs-fixer.cache

View File

@ -5,6 +5,49 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- `align_multiline_comment` fixer.
- `nullable_type_declaration` fixer.
- `php_unit_data_provider_return_type` fixer.
- `attribute_empty_parentheses` fixer.
- `numeric_literal_separator` fixer.
### Fixed
- Replace `compact_nullable_typehint` with `compact_nullable_type_declaration` (deprecated).
- Replace `curly_braces_position` with `braces_position` (deprecated).
- Replace `function_typehint_space` with `type_declaration_spaces` (deprecated).
- Replace `native_function_type_declaration_casing` with `native_type_declaration_casing` (deprecated).
- Replace `new_with_braces` with `new_with_parentheses` (deprecated).
## [1.0.1] - 2023-07-21
### Changed
- `comment_to_phpdoc` no longer fixes PHPStan error suppression and the `todo` tag.
## [1.0.0] - 2023-05-17
### Added
- `single_space_around_construct` fixer.
- `no_extra_blank_lines` fixer.
### Changed
- `friendsofphp/php-cs-fixer` version bumped to `^3.16`.
### Removed
- Iss #16: All custom fixers have been moved to the [separate repository](https://github.com/erickskrauch/php-cs-fixer-custom-fixers).
- Usage of the `braces_fixer` since it's deprecated.
## [0.5.0] - 2023-04-08
### Added
- Enh #12: Implemented `Ely\align_multiline_parameters` fixer.
- Enh #13: Implemented `Ely\multiline_if_statement_braces` fixer.
- Enabled `Ely\align_multiline_parameters` for Ely.by codestyle in `['types' => false, 'defaults' => false]` mode.
- Enabled `Ely\multiline_if_statement_braces` for Ely.by codestyle in `['keep_on_own_line' => true]` mode.
- Enabled
[`PhpCsFixerCustomFixers/multiline_promoted_properties`](https://github.com/kubawerlos/php-cs-fixer-custom-fixers#multilinepromotedpropertiesfixer)
fixer for Ely.by codestyle in 2+ parameters mode.
### Fixed
- Bug #10: `Ely/blank_line_before_return` don't treat interpolation curly bracket as beginning of the scope.
- Bug #9: `Ely/line_break_after_statements` add space before next meaningful line of code and skip comments.
## [0.4.0] - 2022-12-06
### Added
@ -135,7 +178,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- First release
[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/0.4.0...HEAD
[Unreleased]: https://github.com/elyby/php-code-style/compare/1.0.1...HEAD
[1.0.1]: https://github.com/elyby/php-code-style/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/elyby/php-code-style/compare/0.5.0...1.0.0
[0.5.0]: https://github.com/elyby/php-code-style/compare/0.4.0...0.5.0
[0.4.0]: https://github.com/elyby/php-code-style/compare/0.3.0...0.4.0
[0.3.0]: https://github.com/elyby/php-code-style/compare/0.2.1...0.3.0
[0.2.1]: https://github.com/elyby/php-code-style/compare/0.2.0...0.2.1

View File

@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2018 Ely.by (http://ely.by)
Copyright 2024 Ely.by (https://ely.by)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

135
README.md
View File

@ -1,7 +1,6 @@
# Ely.by PHP-CS-Fixer rules
Set of PHP-CS-Fixer rules used in development of Ely.by PHP projects. It's suited for PHP 7.4 and above.
You can use it as a ready-made set of rules or [just some of them](#using-our-fixers).
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
@ -43,12 +42,14 @@ vendor/bin/php-cs-fixer fix
### Configuration
You can pass a custom set of rules to the `\Ely\CS\Config::create()` call. For example, it can be used to validate a
project with PHP 7.0 compatibility:
project with PHP 7.4 compatibility:
```php
<?php
return \Ely\CS\Config::create([
'visibility_required' => ['property', 'method'],
'trailing_comma_in_multiline' => [
'elements' => ['arrays', 'arguments'],
],
])->setFinder($finder);
```
@ -78,10 +79,13 @@ class Foo extends Bar implements FooInterface {
public $field1;
public $field2;
public Typed $field2;
public function sampleFunction(int $a, int $b = null): array {
if ($a === $b) {
public function sampleFunction(
int $a,
private readonly int $b = null,
): array {
if ($a === $this->b) {
$result = bar();
} else {
$result = BazClass::bar($this->field1, $this->field2);
@ -89,7 +93,7 @@ class Foo extends Bar implements FooInterface {
return $result;
}
public function setToNull(): self {
$this->field1 = null;
return $this;
@ -154,120 +158,21 @@ class Foo extends Bar implements FooInterface {
echo 'the next statement is here';
```
## Using our fixers
First of all, you must install Ely.by PHP-CS-Fixer package as described in the [installation chapter](#installation).
After that you can enable our custom fixers with `registerCustomFixers` method:
```php
<?php
// Your Finder configuration
return \PhpCsFixer\Config::create()
->registerCustomFixers(new \Ely\CS\Fixers());
```
And then you'll be able to use our custom rules.
### `Ely/blank_line_around_class_body`
Ensure that a class body contains one blank line after its definition and before its end:
```diff
--- Original
+++ New
@@ @@
<?php
class Test {
+
public function func() {
$obj = new class extends Foo {
+
public $prop;
+
}
}
+
}
```
**Configuration:**
* `apply_to_anonymous_classes` - should this fixer be applied to anonymous classes? If it is set to `false`, than
anonymous classes will be fixed to don't have empty lines around body. **Default**: `true`.
* `blank_lines_count` - adjusts an amount of the blank lines. **Default**: `1`.
### `Ely/blank_line_before_return`
This is extended version of the original `blank_line_before_statement` fixer. It applies only to `return` statements
and only in cases, when on the current nesting level more than one statements.
```diff
--- Original
+++ New
@@ @@
<?php
public function foo() {
$a = 'this';
$b = 'is';
+
return "$a $b awesome";
}
public function bar() {
$this->foo();
return 'okay';
}
```
### `Ely/line_break_after_statements`
Ensures that there is one blank line above the next statements: `if`, `switch`, `for`, `foreach`, `while`
and `do-while`.
```diff
--- Original
+++ New
@@ @@
<?php
$a = 123;
if ($a === 123) {
// Do something here
}
+
$b = [1, 2, 3];
foreach ($b as $number) {
if ($number === 3) {
echo 'it is three!';
}
}
+
$c = 'next statement';
```
### `Ely/remove_class_name_method_usages` (Yii2)
Replaces Yii2 [`BaseObject::className()`](https://github.com/yiisoft/yii2/blob/e53fc0ded1/framework/base/BaseObject.php#L84)
usages with native `::class` keyword, introduced in PHP 5.5.
```diff
--- Original
+++ New
@@ @@
<?php
use common\models\User;
* There MUST be no alignment around multiline function parameters.
- $className = User::className();
+ $className = User::class;
```
```php
<?php
function foo(
string $input,
int $key = 0,
): void {}
```
[ico-version]: https://img.shields.io/packagist/v/ely/php-code-style.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-Apache-green.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/ely/php-code-style.svg?style=flat-square
[ico-build-status]: https://img.shields.io/github/workflow/status/elyby/php-code-style/CI.svg?style=flat-square
[ico-build-status]: https://img.shields.io/github/actions/workflow/status/elyby/php-code-style/ci.yml?branch=master&style=flat-square
[link-packagist]: https://packagist.org/packages/ely/php-code-style
[link-contributors]: ../../contributors
[link-downloads]: https://packagist.org/packages/ely/php-code-style/stats
[link-build-status]: https://github.com/elyby/php-code-style/actions

View File

@ -20,35 +20,25 @@
"homepage": "https://github.com/elyby/php-code-style",
"require": {
"php": "^7.4 || ^8.0",
"friendsofphp/php-cs-fixer": "^3.13"
"erickskrauch/php-cs-fixer-custom-fixers": "^1.0.1",
"friendsofphp/php-cs-fixer": "^3.47",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.13"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.28",
"php-cs-fixer/phpunit-constraint-isidenticalstring": "^1.2",
"phpspec/prophecy": "^1.15",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.5",
"phpunitgoodpractices/polyfill": "^1.5",
"phpunitgoodpractices/traits": "^1.9.1",
"symfony/phpunit-bridge": "^6.0"
"ergebnis/composer-normalize": "^2.28"
},
"autoload": {
"psr-4": {
"Ely\\CS\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Ely\\CS\\Test\\": "tests/",
"PhpCsFixer\\Tests\\": "vendor/friendsofphp/php-cs-fixer/tests/"
}
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
},
"preferred-install": {
"friendsofphp/php-cs-fixer": "source"
}
},
"sort-packages": true
}
}

2735
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,30 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="./vendor/autoload.php"
colors="true"
columns="max"
verbose="true"
>
<testsuites>
<testsuite name="all">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<php>
<ini name="zend.enable_gc" value="0"/>
<ini name="memory_limit" value="1G"/>
</php>
</phpunit>

View File

@ -3,14 +3,18 @@ declare(strict_types=1);
namespace Ely\CS;
use ErickSkrauch\PhpCsFixer\Fixers as ErickSkrauchFixers;
use PhpCsFixer\Config as PhpCsFixerConfig;
use PhpCsFixer\ConfigInterface as PhpCsFixerConfigInterface;
use PhpCsFixerCustomFixers\Fixers as KubawerlosFixers;
class Config {
final class Config {
public static function create(array $overwrittenRules = []): PhpCsFixerConfig {
public static function create(array $overwrittenRules = []): PhpCsFixerConfigInterface {
return (new PhpCsFixerConfig())
->setRiskyAllowed(true)
->registerCustomFixers(new Fixers())
->registerCustomFixers(new ErickSkrauchFixers())
->registerCustomFixers(new KubawerlosFixers())
->setRules(Rules::create($overwrittenRules));
}

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Fixer;
abstract class AbstractFixer extends \PhpCsFixer\AbstractFixer {
/**
* {@inheritdoc}
*/
public function getName(): string {
return sprintf('Ely/%s', parent::getName());
}
}

View File

@ -1,107 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Fixer\LanguageConstruct;
use Ely\CS\Fixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
/**
* Replaces Yii2 BaseObject::className() usages with native ::class keyword, introduced in PHP 5.5.
*
* @author ErickSkrauch <erickskrauch@ely.by>
*/
final class RemoveClassNameMethodUsagesFixer extends AbstractFixer {
public function getDefinition(): FixerDefinitionInterface {
return new FixerDefinition(
'Converts Yii2 `BaseObject::className()` method usage into `::class` keyword.',
[
new CodeSample(
'<?php
use Foo\Bar\Baz;
$className = Baz::className();
',
),
],
null,
'Risky when the method `className()` is overridden.',
);
}
public function isCandidate(Tokens $tokens): bool {
return $tokens->isTokenKindFound(T_STRING);
}
public function isRisky(): bool {
return true;
}
protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
for ($index = $tokens->count() - 4; $index > 0; --$index) {
$candidate = $this->getReplaceCandidate($tokens, $index);
if ($candidate === null) {
continue;
}
$this->fixClassNameMethodUsage(
$tokens,
$index,
$candidate[0], // brace open
$candidate[1], // brace close
);
}
}
private function getReplaceCandidate(Tokens $tokens, int $index): ?array {
if (!$tokens[$index]->isGivenKind(T_STRING)) {
return null;
}
$braceOpenIndex = $tokens->getNextMeaningfulToken($index);
if (!$tokens[$braceOpenIndex]->equals('(')) {
return null;
}
$braceCloseIndex = $tokens->getNextMeaningfulToken($braceOpenIndex);
if (!$tokens[$braceCloseIndex]->equals(')')) {
return null;
}
$doubleColon = $tokens->getPrevMeaningfulToken($index);
if (!$tokens[$doubleColon]->isGivenKind([T_DOUBLE_COLON])) {
return null;
}
$methodName = $tokens[$index]->getContent();
if ($methodName !== 'className') {
return null;
}
return [
$braceOpenIndex,
$braceCloseIndex,
];
}
private function fixClassNameMethodUsage(
Tokens $tokens,
int $index,
int $braceOpenIndex,
int $braceCloseIndex
): void {
$tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
$tokens->clearTokenAndMergeSurroundingWhitespace($braceOpenIndex);
$tokens->clearAt($index);
$tokens->insertAt($index, new Token([CT::T_CLASS_CONSTANT, 'class']));
}
}

View File

@ -1,131 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Fixer\Whitespace;
use Ely\CS\Fixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;
/**
* This is copy of the PR https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/3688
*
* @author ErickSkrauch <erickskrauch@ely.by>
*/
final class BlankLineAroundClassBodyFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface {
public function getDefinition(): FixerDefinitionInterface {
return new FixerDefinition(
'Ensure that class body contains one blank line after class definition and before its end.',
[
new CodeSample(
'<?php
class Sample
{
protected function foo()
{
}
}
',
),
new CodeSample(
'<?php
new class extends Foo {
protected function foo()
{
}
};
',
['apply_to_anonymous_classes' => false],
),
new CodeSample(
'<?php
new class extends Foo {
protected function foo()
{
}
};
',
['apply_to_anonymous_classes' => true],
),
],
);
}
public function getPriority(): int {
// should be run after the BracesFixer
return -26;
}
public function isCandidate(Tokens $tokens): bool {
return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds());
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void {
$analyzer = new TokensAnalyzer($tokens);
foreach ($tokens as $index => $token) {
if (!$token->isClassy()) {
continue;
}
$countLines = $this->configuration['blank_lines_count'];
if (!$this->configuration['apply_to_anonymous_classes'] && $analyzer->isAnonymousClass($index)) {
$countLines = 0;
}
$startBraceIndex = $tokens->getNextTokenOfKind($index, ['{']);
if ($tokens[$startBraceIndex + 1]->isWhitespace()) {
$nextStatementIndex = $tokens->getNextMeaningfulToken($startBraceIndex);
// Traits should be placed right after a class opening brace,
if ($tokens[$nextStatementIndex]->getContent() !== 'use') {
$this->fixBlankLines($tokens, $startBraceIndex + 1, $countLines);
}
}
$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startBraceIndex);
if ($tokens[$endBraceIndex - 1]->isWhitespace()) {
$this->fixBlankLines($tokens, $endBraceIndex - 1, $countLines);
}
}
}
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface {
return new FixerConfigurationResolver([
(new FixerOptionBuilder('blank_lines_count', 'adjusts an amount of the blank lines.'))
->setAllowedTypes(['int'])
->setDefault(1)
->getOption(),
(new FixerOptionBuilder('apply_to_anonymous_classes', 'whether this fixer should be applied to anonymous classes.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
]);
}
private function fixBlankLines(Tokens $tokens, int $index, int $countLines): void {
$content = $tokens[$index]->getContent();
// Apply fix only in the case when the count lines do not equals to expected
if (substr_count($content, "\n") === $countLines + 1) {
return;
}
// The final bit of the whitespace must be the next statement's indentation
Preg::matchAll('/[^\n\r]+[\r\n]*/', $content, $matches);
$lines = $matches[0];
$eol = $this->whitespacesConfig->getLineEnding();
$tokens[$index] = new Token([T_WHITESPACE, str_repeat($eol, $countLines + 1) . end($lines)]);
}
}

View File

@ -1,92 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Fixer\Whitespace;
use Ely\CS\Fixer\AbstractFixer;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
/**
* This is extended version of the original `blank_line_before_statement` fixer.
* It applies only to `return` statements and only in cases, when on the current nesting level more than one statements.
*
* @url https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/5c5de791ab/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Andreas Möller <am@localheinz.com>
* @author SpacePossum
*/
final class BlankLineBeforeReturnFixer extends AbstractFixer implements WhitespacesAwareFixerInterface {
public function getDefinition(): FixerDefinitionInterface {
return new FixerDefinition(
'An empty line feed should precede a return statement.',
[new CodeSample("<?php\nfunction A()\n{\n echo 1;\n echo 2;\n return 1;\n}\n")],
);
}
public function isCandidate(Tokens $tokens): bool {
return $tokens->isTokenKindFound(T_RETURN);
}
public function getPriority(): int {
// should be run after NoUselessReturnFixer, ClassDefinitionFixer and BracesFixer
return -26;
}
protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_RETURN)) {
continue;
}
$eol = $this->whitespacesConfig->getLineEnding();
$prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
if (!$prevNonWhitespaceToken->equalsAny([';', '}'])) {
continue;
}
$prevIndex = $index - 1;
$prevToken = $tokens[$prevIndex];
if ($prevToken->isWhitespace()) {
$countParts = substr_count($prevToken->getContent(), "\n");
if ($countParts === 0) {
$tokens[$prevIndex] = new Token([T_WHITESPACE, rtrim($prevToken->getContent(), " \t") . $eol . $eol]);
} elseif ($countParts === 1) {
$backwardIndex = $prevIndex;
do {
if (--$backwardIndex < 0) {
break;
}
$backwardToken = $tokens[$backwardIndex];
if ($backwardToken->getContent() === '{') {
break;
}
if ($backwardToken->isWhitespace()) {
$countParts += substr_count($backwardToken->getContent(), "\n");
}
} while ($countParts < 3);
if ($countParts !== 2) {
$tokens[$prevIndex] = new Token([T_WHITESPACE, $eol . $prevToken->getContent()]);
}
}
} else {
$tokens->insertSlices([$index => new Token([T_WHITESPACE, $eol . $eol])]);
++$index;
++$limit;
}
}
}
}

View File

@ -1,170 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Fixer\Whitespace;
use Ely\CS\Fixer\AbstractFixer;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
/**
* This is rewritten version of the original fixer created by @PedroTroller with improved cases validation and
* targeted to the PHP-CS-Fixer 2.11 version.
*
* @url https://github.com/PedroTroller/PhpCSFixer-Custom-Fixers/blob/affdf99f51/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenStatementsFixer.php
*
* @author ErickSkrauch <erickskrauch@ely.by>
*/
final class LineBreakAfterStatementsFixer extends AbstractFixer implements WhitespacesAwareFixerInterface {
/**
* There is no 'do', 'cause the processing of the 'while' also includes do {} while (); construction
*/
public const STATEMENTS = [
T_IF,
T_SWITCH,
T_FOR,
T_FOREACH,
T_WHILE,
];
public function getDefinition(): FixerDefinitionInterface {
return new FixerDefinition(
'Ensures that there is one blank line above the control statements.',
[
new CodeSample(
'<?php
class Foo
{
/**
* @return null
*/
public function foo() {
do {
// ...
} while (true);
foreach (["foo", "bar"] as $str) {
// ...
}
if (true === false) {
// ...
}
foreach (["foo", "bar"] as $str) {
if ($str === "foo") {
// smth
}
}
while (true) {
// ...
}
switch("123") {
case "123":
break;
}
$a = "next statement";
}
}
',
),
],
);
}
public function isCandidate(Tokens $tokens): bool {
return $tokens->isAnyTokenKindsFound(self::STATEMENTS);
}
public function getPriority(): int {
// for the best result should be run after the BracesFixer
return -26;
}
protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(self::STATEMENTS)) {
continue;
}
$endStatementIndex = $this->findStatementEnd($tokens, $index);
$nextStatementIndex = $tokens->getNextNonWhitespace($endStatementIndex);
if ($nextStatementIndex === null) {
continue;
}
if ($tokens[$nextStatementIndex]->equals('}')) {
$this->fixBlankLines($tokens, $endStatementIndex + 1, 0);
continue;
}
$this->fixBlankLines($tokens, $endStatementIndex + 1, 1);
}
}
private function fixBlankLines(Tokens $tokens, int $index, int $countLines): void {
$content = $tokens[$index]->getContent();
// Apply fix only in the case when the count lines do not equals to expected
if (substr_count($content, "\n") === $countLines + 1) {
return;
}
// The final bit of the whitespace must be the next statement's indentation
Preg::matchAll('/[^\n\r]+[\r\n]*/', $content, $matches);
$lines = $matches[0];
$eol = $this->whitespacesConfig->getLineEnding();
$tokens[$index] = new Token([T_WHITESPACE, str_repeat($eol, $countLines + 1) . end($lines)]);
}
private function findStatementEnd(Tokens $tokens, int $index): int {
$nextIndex = $tokens->getNextMeaningfulToken($index);
$nextToken = $tokens[$nextIndex];
if ($nextToken->equals('(')) {
$parenthesisEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextIndex);
$possibleBeginBraceIndex = $tokens->getNextNonWhitespace($parenthesisEndIndex);
} else {
$possibleBeginBraceIndex = $nextIndex;
}
// `do {} while ();`
if ($tokens[$index]->isGivenKind(T_WHILE) && $tokens[$possibleBeginBraceIndex]->equals(';')) {
return $possibleBeginBraceIndex;
}
$possibleBeginBrace = $tokens[$possibleBeginBraceIndex];
if ($possibleBeginBrace->equals('{')) {
$blockEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $possibleBeginBraceIndex);
} else {
$blockEnd = $tokens->getNextTokenOfKind($possibleBeginBraceIndex, [';']);
}
$nextStatementIndex = $tokens->getNextMeaningfulToken($blockEnd);
if ($nextStatementIndex === null) {
return $blockEnd;
}
// `if () {} elseif {}`
if ($tokens[$nextStatementIndex]->isGivenKind(T_ELSEIF)) {
return $this->findStatementEnd($tokens, $nextStatementIndex);
}
// `if () {} else if {}` or simple `if () {} else {}`
if ($tokens[$nextStatementIndex]->isGivenKind(T_ELSE)) {
$nextNextStatementIndex = $tokens->getNextMeaningfulToken($nextStatementIndex);
if ($tokens[$nextNextStatementIndex]->isGivenKind(T_IF)) {
return $this->findStatementEnd($tokens, $nextNextStatementIndex);
}
return $this->findStatementEnd($tokens, $nextStatementIndex);
}
return $blockEnd;
}
}

View File

@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS;
use ArrayIterator;
use IteratorAggregate;
use PhpCsFixer\Finder;
use PhpCsFixer\Fixer\FixerInterface;
use ReflectionClass;
use Traversable;
class Fixers implements IteratorAggregate {
public function getIterator(): Traversable {
$finder = new Finder();
$finder->in(__DIR__ . '/Fixer')->name('*.php');
$classes = [];
foreach ($finder as $file) {
$class = '\\Ely\\CS' . str_replace('/', '\\', mb_substr($file->getPathname(), mb_strlen(__DIR__), -4));
if (!class_exists($class)) {
continue;
}
$rfl = new ReflectionClass($class);
if (!$rfl->implementsInterface(FixerInterface::class) || $rfl->isAbstract()) {
continue;
}
$classes[] = $class;
}
return new ArrayIterator(array_map(fn($class) => new $class(), $classes));
}
}

View File

@ -3,8 +3,9 @@ declare(strict_types=1);
namespace Ely\CS;
class Rules {
final class Rules {
// Last fixers review has been done on PHP-CS-Fixer v3.48
public static function create(array $overwrittenRules = []): array {
return array_merge([
'@PSR2' => true,
@ -33,16 +34,19 @@ class Rules {
'trim_array_spaces' => true,
'whitespace_after_comma_in_array' => true,
// Attribute notation
'attribute_empty_parentheses' => true,
// Basic
'braces' => [
'allow_single_line_anonymous_class_with_empty_body' => true,
'position_after_functions_and_oop_constructs' => 'same',
'braces_position' => [
'functions_opening_brace' => 'same_line',
'classes_opening_brace' => 'same_line',
],
'no_multiple_statements_per_line' => true,
'no_trailing_comma_in_singleline' => true,
'non_printable_character' => [
'use_escape_sequences_in_strings' => true,
],
'numeric_literal_separator' => true,
'octal_notation' => PHP_MAJOR_VERSION >= 8 && PHP_MINOR_VERSION >= 1,
'psr_autoloading' => true,
@ -53,7 +57,7 @@ class Rules {
'magic_constant_casing' => true,
'magic_method_casing' => true,
'native_function_casing' => true,
'native_function_type_declaration_casing' => true,
'native_type_declaration_casing' => true,
// Cast Notation
'cast_spaces' => [
@ -72,6 +76,10 @@ class Rules {
'property' => 'one',
],
],
'class_definition' => [
'single_item_single_line' => true,
'inline_constructor_arguments' => false,
],
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_unneeded_final_method' => true,
@ -80,7 +88,15 @@ class Rules {
'visibility_required' => true,
// Comment
'comment_to_phpdoc' => true,
'comment_to_phpdoc' => [
'ignored_tags' => [
'todo',
// https://phpstan.org/user-guide/ignoring-errors
'phpstan-ignore',
'phpstan-ignore-line',
'phpstan-ignore-next-line',
],
],
'multiline_comment_opening_closing' => true,
'no_empty_comment' => true,
'single_line_comment_spacing' => true,
@ -112,7 +128,6 @@ class Rules {
'closure_function_spacing' => 'none',
'closure_fn_spacing' => 'none',
],
'function_typehint_space' => true,
'implode_call' => true,
'lambda_not_used_import' => true,
'return_type_declaration' => true,
@ -133,6 +148,8 @@ class Rules {
'dir_constant' => true,
'function_to_constant' => true,
'is_null' => true,
'nullable_type_declaration' => true,
'single_space_around_construct' => true,
// List Notation
'list_syntax' => true,
@ -151,7 +168,7 @@ class Rules {
'spacing' => 'one',
],
'logical_operators' => true,
'new_with_braces' => [
'new_with_parentheses' => [
'anonymous_class' => false,
],
'no_useless_concat_operator' => true,
@ -169,6 +186,7 @@ class Rules {
// PHPUnit
'php_unit_construct' => true,
'php_unit_data_provider_return_type' => true,
'php_unit_dedicate_assert_internal_type' => true,
'php_unit_expectation' => true,
'php_unit_fqcn_annotation' => true,
@ -183,6 +201,9 @@ class Rules {
'call_type' => 'this',
],
// PHPDoc
'align_multiline_comment' => true,
// Return Notation
'no_useless_return' => true,
'return_assignment' => true,
@ -205,21 +226,49 @@ class Rules {
// Whitespace
'array_indentation' => true,
'compact_nullable_typehint' => true,
'compact_nullable_type_declaration' => true,
'method_chaining_indentation' => true,
'no_extra_blank_lines' => [
'tokens' => [
'attribute',
'break',
'case',
'continue',
'curly_brace_block',
'default',
'extra',
'parenthesis_brace_block',
'return',
'square_brace_block',
'switch',
'throw',
'use',
],
],
'no_spaces_around_offset' => true,
'no_whitespace_in_blank_line' => true,
'type_declaration_spaces' => true,
'types_spaces' => [
'space_multiple_catch' => 'none',
],
// kubawerlos fixers
'PhpCsFixerCustomFixers/multiline_promoted_properties' => [
'minimum_number_of_parameters' => 2,
],
// Our custom or extended fixers
'Ely/blank_line_around_class_body' => [
'ErickSkrauch/align_multiline_parameters' => [
'variables' => null,
'defaults' => false,
],
'ErickSkrauch/blank_line_around_class_body' => [
'apply_to_anonymous_classes' => false,
],
'Ely/blank_line_before_return' => true,
'Ely/line_break_after_statements' => true,
'Ely/remove_class_name_method_usages' => true,
'ErickSkrauch/blank_line_before_return' => true,
'ErickSkrauch/line_break_after_statements' => true,
'ErickSkrauch/multiline_if_statement_braces' => true,
'ErickSkrauch/remove_class_name_method_usages' => true,
], $overwrittenRules);
}

View File

@ -1,66 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Test\Fixer\LanguageConstruct;
use Ely\CS\Fixer\LanguageConstruct\RemoveClassNameMethodUsagesFixer;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @covers \Ely\CS\Fixer\LanguageConstruct\RemoveClassNameMethodUsagesFixer
*/
class RemoveClassNameMethodUsagesFixerTest extends AbstractFixerTestCase {
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void {
$this->doTest($expected, $input);
}
public function provideFixCases(): iterable {
yield [
'<?php echo className();',
];
yield [
'<?php
use Foo\Bar\Baz;
$exceptionString = Baz::classname();
',
];
yield [
'<?php
use Foo\Bar\Baz;
$className = Baz::class;
',
'<?php
use Foo\Bar\Baz;
$className = Baz::className();
',
];
yield [
'<?php
use Foo\Bar\Baz;
$exceptionString = "The class should be instance of " . Baz::class . " and nothing else";
',
'<?php
use Foo\Bar\Baz;
$exceptionString = "The class should be instance of " . Baz::className() . " and nothing else";
',
];
}
protected function createFixer(): AbstractFixer {
return new RemoveClassNameMethodUsagesFixer();
}
}

View File

@ -1,340 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Test\Fixer\Whitespace;
use Ely\CS\Fixer\Whitespace\BlankLineAroundClassBodyFixer;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @author ErickSkrauch <erickskrauch@ely.by>
*
* @covers \Ely\CS\Fixer\Whitespace\BlankLineAroundClassBodyFixer
*/
final class BlankLineAroundClassBodyFixerTest extends AbstractFixerTestCase {
private static $configurationDoNotApplyForAnonymousClasses = ['apply_to_anonymous_classes' => false];
private static $configurationTwoEmptyLines = ['blank_lines_count' => 2];
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = null): void {
if ($configuration !== null) {
$this->fixer->configure($configuration);
}
$this->doTest($expected, $input);
}
public function provideFixCases(): iterable {
yield [
'<?php
class Good
{
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
public function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
class Good
{
/**
* Also blank line before DocBlock
*/
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
/**
* Also blank line before DocBlock
*/
public function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
class Good
{
/**
* Too many whitespaces
*/
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
/**
* Too many whitespaces
*/
public function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
interface Good
{
/**
* Also blank line before DocBlock
*/
public function firstMethod();
}',
'<?php
interface Good
{
/**
* Also blank line before DocBlock
*/
public function firstMethod();
}',
];
yield [
'<?php
trait Good
{
/**
* Also no blank line before DocBlock
*/
public function firstMethod() {}
}',
'<?php
trait Good
{
/**
* Also no blank line before DocBlock
*/
public function firstMethod() {}
}',
];
yield [
'<?php
class Good
{
use Foo\bar;
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
use Foo\bar;
public function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
class Good
{
use Foo\bar;
use Foo\baz;
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
use Foo\bar;
use Foo\baz;
public function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
class Good
{
use Foo, Bar {
Bar::smallTalk insteadof A;
Foo::bigTalk insteadof B;
}
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
use Foo, Bar {
Bar::smallTalk insteadof A;
Foo::bigTalk insteadof B;
}
public function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
class Good
{
public function firstMethod()
{
//code here
}
}',
'<?php
class Good
{
public function firstMethod()
{
//code here
}
}',
self::$configurationTwoEmptyLines,
];
// check if some fancy whitespaces aren't modified
yield [
'<?php
class Good
{public
function firstMethod()
{
//code here
}
}',
];
yield [
'<?php
$class = new class extends \Foo {
public $field;
public function firstMethod() {}
};',
'<?php
$class = new class extends \Foo {
public $field;
public function firstMethod() {}
};',
];
yield [
'<?php
$class = new class extends \Foo {
public $field;
public function firstMethod() {}
};',
'<?php
$class = new class extends \Foo {
public $field;
public function firstMethod() {}
};',
self::$configurationDoNotApplyForAnonymousClasses,
];
}
/**
* @dataProvider provideMessyWhitespacesCases
*/
public function testMessyWhitespaces(string $expected, ?string $input = null): void {
/** @var \PhpCsFixer\Fixer\WhitespacesAwareFixerInterface $fixer */
$fixer = $this->fixer;
$fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest($expected, $input);
}
public function provideMessyWhitespacesCases(): iterable {
yield [
"<?php\nclass Foo\n{\r\n\r\n public function bar() {}\r\n\r\n}",
"<?php\nclass Foo\n{\n public function bar() {}\n}",
];
yield [
"<?php\nclass Foo\n{\r\n\r\n public function bar() {}\r\n\r\n}",
"<?php\nclass Foo\n{\r\n\r\n\n\n public function bar() {}\n\n\n\n}",
];
}
protected function createFixer(): AbstractFixer {
return new BlankLineAroundClassBodyFixer();
}
}

View File

@ -1,213 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Test\Fixer\Whitespace;
use Ely\CS\Fixer\Whitespace\BlankLineBeforeReturnFixer;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* Original file copied from:
* @url https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/5c5de791ab/tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Andreas Möller <am@localheinz.com>
* @author SpacePossum
*
* @internal
*
* @property BlankLineBeforeReturnFixer $fixer
*
* @covers \Ely\CS\Fixer\Whitespace\BlankLineBeforeReturnFixer
*/
final class BlankLineBeforeReturnFixerTest extends AbstractFixerTestCase {
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void {
$this->doTest($expected, $input);
}
public function provideFixCases(): iterable {
yield [
'$a = $a;
return $a;
',
];
yield [
'<?php
$a = $a;
return $a;',
'<?php
$a = $a; return $a;',
];
yield [
'<?php
$b = $b;
return $b;',
'<?php
$b = $b;return $b;',
];
yield [
'<?php
$c = $c;
return $c;',
'<?php
$c = $c;
return $c;',
];
yield [
'<?php
$d = $d;
return $d;',
'<?php
$d = $d;
return $d;',
];
yield [
'<?php
if (true) {
return 1;
}',
];
yield [
'<?php
if (true)
return 1;
',
];
yield [
'<?php
if (true) {
return 1;
} else {
return 2;
}',
];
yield [
'<?php
if (true)
return 1;
else
return 2;
',
];
yield [
'<?php
if (true) {
return 1;
} elseif (false) {
return 2;
}',
];
yield [
'<?php
if (true)
return 1;
elseif (false)
return 2;
',
];
yield [
'<?php
throw new Exception("return true;");',
];
yield [
'<?php
function foo()
{
// comment
return "foo";
}',
];
yield [
'<?php
function foo()
{
// comment
return "bar";
}',
];
yield [
'<?php
function foo()
{
// comment
return "bar";
}',
];
yield [
'<?php
function foo() {
$a = "a";
$b = "b";
return $a . $b;
}',
'<?php
function foo() {
$a = "a";
$b = "b";
return $a . $b;
}',
];
yield [
'<?php
function foo() {
$b = "b";
return $a . $b;
}',
];
yield [
'<?php
function foo() {
$a = "a";
return $a . "hello";
}
function bar() {
$b = "b";
return $b . "hello";
}
',
];
}
/**
* @dataProvider provideMessyWhitespacesCases
*/
public function testMessyWhitespaces(string $expected, ?string $input = null): void {
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest($expected, $input);
}
public function provideMessyWhitespacesCases(): iterable {
yield [
"<?php\r\n\$a = \$a;\r\n\r\nreturn \$a;",
"<?php\r\n\$a = \$a; return \$a;",
];
yield [
"<?php\r\n\$b = \$b;\r\n\r\nreturn \$b;",
"<?php\r\n\$b = \$b;return \$b;",
];
yield [
"<?php\r\n\$c = \$c;\r\n\r\nreturn \$c;",
"<?php\r\n\$c = \$c;\r\nreturn \$c;",
];
}
protected function createFixer(): AbstractFixer {
return new BlankLineBeforeReturnFixer();
}
}

View File

@ -1,584 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\CS\Test\Fixer\Whitespace;
use Ely\CS\Fixer\Whitespace\LineBreakAfterStatementsFixer;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @covers \Ely\CS\Fixer\Whitespace\LineBreakAfterStatementsFixer
*
* @author ErickSkrauch <erickskrauch@ely.by>
*/
class LineBreakAfterStatementsFixerTest extends AbstractFixerTestCase {
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void {
$this->doTest($expected, $input);
}
public function provideFixCases(): iterable {
// Simple cases
yield [
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
} else {
// another code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
} else {
// another code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++) {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++) {
// code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str) {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str) {
// code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
while ($i < 10) {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
while ($i < 10) {
// code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
do {
// code
} while ($i < 10);
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
do {
// code
} while ($i < 10);
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a":
break;
case "b":
break;
default:
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a":
break;
case "b":
break;
default:
// code
}
$a = "next statement";
}
}',
];
// Extended cases
yield [
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} else if ("a" === "c") {
// code
} else if ("a" === "d") {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} else if ("a" === "c") {
// code
} else if ("a" === "d") {
// code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} elseif ("a" === "c") {
// code
} elseif ("a" === "d") {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} elseif ("a" === "c") {
// code
} elseif ("a" === "d") {
// code
}
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str) {
if ($str === "foo") {
// code
}
}
}
}',
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str) {
if ($str === "foo") {
// code
}
}
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a": {
break;
}
case "b": {
break;
}
default: {
// code
}
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a": {
break;
}
case "b": {
break;
}
default: {
// code
}
}
$a = "next statement";
}
}',
];
yield [
'<?php
$a = "prev statement";
foreach ($coordinates as $coordinate) {
$points = explode(",", $coordinate);
}
',
];
// Issue 5
yield [
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
else
$this->baz();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
else
$this->baz();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++)
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++)
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str)
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str)
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
while ($i < 10)
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
while ($i < 10)
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
do
$this->bar();
while ($i < 10);
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
do
$this->bar();
while ($i < 10);
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
if ("a" === "b")
$this->foo();
else if ("a" === "c")
$this->bar();
else if ("a" === "d")
$this->baz();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function bar()
{
if ("a" === "b")
$this->foo();
else if ("a" === "c")
$this->bar();
else if ("a" === "d")
$this->baz();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str)
if ($str === "foo")
$this->bar();
return 3;
}
}',
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str)
if ($str === "foo")
$this->bar();
return 3;
}
}',
];
}
protected function createFixer(): AbstractFixer {
return new LineBreakAfterStatementsFixer();
}
}