mirror of
https://github.com/elyby/php-code-style.git
synced 2025-05-31 14:12:05 +05:30
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
2140798d0a | |||
d8eafd4505 | |||
714a38d53e | |||
08483a26e2 | |||
3d5d40b2b7 | |||
e617efc11c | |||
a6d1aa7265 | |||
3ab60f5bd5 | |||
06a37576ee | |||
95e0f0f702 | |||
0d20175b0a | |||
4b480df97d | |||
50c7deed65 | |||
31661cd142 | |||
8ee5979288 | |||
2480f7ba46 | |||
824cf6c1f1 | |||
fa40a02dfd | |||
a7bef227df | |||
59e9994662 | |||
76bd14c167 | |||
076c2f1c6c | |||
04e3690d06 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/vendor/
|
||||
vendor
|
||||
dist
|
||||
.php_cs
|
||||
.php_cs.cache
|
||||
|
35
.travis.yml
Normal file
35
.travis.yml
Normal file
@ -0,0 +1,35 @@
|
||||
language: php
|
||||
php:
|
||||
- '7.0'
|
||||
- '7.1'
|
||||
- '7.2'
|
||||
- '7.3'
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- vendor
|
||||
- $HOME/.composer
|
||||
|
||||
env:
|
||||
global:
|
||||
- DEFAULT_COMPOSER_FLAGS="--optimize-autoloader --no-interaction --no-progress"
|
||||
|
||||
before_script:
|
||||
- composer global show hirak/prestissimo -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo
|
||||
- composer install --no-interaction
|
||||
|
||||
stages:
|
||||
- Static Code Analysis
|
||||
- Test
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- stage: Static Code Analysis
|
||||
php: 7.3
|
||||
script:
|
||||
- vendor/bin/php-cs-fixer fix -v --dry-run
|
||||
allow_failures:
|
||||
- php: nightly
|
||||
|
||||
script:
|
||||
- vendor/bin/phpunit
|
89
CHANGELOG.md
Normal file
89
CHANGELOG.md
Normal file
@ -0,0 +1,89 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
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]
|
||||
|
||||
## [0.3.0] - 2019-02-23
|
||||
### Added
|
||||
- `array_indentation` fixer.
|
||||
- `combine_consecutive_issets` fixer.
|
||||
- `combine_nested_dirname` fixer.
|
||||
- `dir_constant` fixer.
|
||||
- `ereg_to_preg` fixer.
|
||||
- `explicit_string_variable` fixer.
|
||||
- `implode_call` fixer.
|
||||
- `is_null` fixer.
|
||||
- `list_syntax` fixer.
|
||||
- `logical_operators` fixer.
|
||||
- `lowercase_cast` fixer.
|
||||
- `lowercase_static_reference` fixer.
|
||||
- `magic_constant_casing` fixer.
|
||||
- `magic_method_casing` fixer.
|
||||
- `multiline_whitespace_before_semicolons` fixer.
|
||||
- `native_function_casing` fixer.
|
||||
- `no_alternative_syntax` fixer.
|
||||
- `no_homoglyph_names` fixer.
|
||||
- `no_leading_import_slash` fixer.
|
||||
- `no_leading_namespace_whitespace` fixer.
|
||||
- `no_mixed_echo_print` fixer.
|
||||
- `no_multiline_whitespace_around_double_arrow` fixer.
|
||||
- `no_php4_constructor` fixer.
|
||||
- `no_spaces_around_offset` fixer.
|
||||
- `no_superfluous_elseif` fixer.
|
||||
- `no_unneeded_control_parentheses` fixer.
|
||||
- `no_useless_return` fixer.
|
||||
- `php_unit_construct` fixer.
|
||||
- `php_unit_expectation` fixer.
|
||||
- `php_unit_method_casing` fixer.
|
||||
- `php_unit_mock` fixer.
|
||||
- `php_unit_namespaced` fixer.
|
||||
- `php_unit_set_up_tear_down_visibility` fixer.
|
||||
- `php_unit_strict` fixer.
|
||||
- `pow_to_exponentiation` fixer.
|
||||
- `psr4` fixer.
|
||||
- `return_assignment` fixer.
|
||||
- `random_api_migration` fixer.
|
||||
- `self_accessor` fixer.
|
||||
- `set_type_to_cast` fixer.
|
||||
- `short_scalar_cast` fixer.
|
||||
- `space_after_semicolon` fixer.
|
||||
- `standardize_increment` fixer.
|
||||
- `standardize_not_equals` fixer.
|
||||
- `trim_array_spaces` fixer.
|
||||
- `unary_operator_spaces` fixer.
|
||||
|
||||
### Changed
|
||||
- `friendsofphp/php-cs-fixer` version bumped to `^2.13.0`.
|
||||
|
||||
### Fixed
|
||||
- `ordered_imports` now has fixed order of import types.
|
||||
|
||||
## [0.2.1] - 2019-02-22
|
||||
### Fixed
|
||||
- Compatibility with the `friendsofphp/php-cs-fixer` version 2.13.3 and above.
|
||||
|
||||
## [0.2.0] - 2018-08-08
|
||||
### Added
|
||||
- Enh #4: `Ely\remove_class_name_method_usages` fixer.
|
||||
- `array_syntax` fixer.
|
||||
- This changelog file.
|
||||
- Travis CI building.
|
||||
|
||||
### Changed
|
||||
- `friendsofphp/php-cs-fixer` version bumped to `^2.12.2`.
|
||||
- `phpunit/phpunit` downgraded to `^6.5.1` to be compatible with PHP 7.0.
|
||||
|
||||
### Fixed
|
||||
- Bug #5: `Ely/line_break_after_statements` triggers error if statement doesn't have control brackets.
|
||||
|
||||
## 0.1.0 - 2018-04-17
|
||||
### Added
|
||||
- First release
|
||||
|
||||
[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/0.3.0...HEAD
|
||||
[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
|
||||
[0.2.0]: https://github.com/elyby/php-code-style/compare/0.1.0...0.2.0
|
35
README.md
35
README.md
@ -3,6 +3,11 @@
|
||||
Set of PHP-CS-Fixer rules used in development of Ely.by PHP projects. It's suited for PHP 7.1 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]
|
||||
[![Software License][ico-license]](LICENSE.md)
|
||||
[![Build Status][ico-build-status]][link-build-status]
|
||||
|
||||
## Installation
|
||||
|
||||
First of all install Ely.by PHP-CS-Fixer rules via composer with
|
||||
@ -12,7 +17,7 @@ First of all install Ely.by PHP-CS-Fixer rules via composer with
|
||||
composer require --dev friendsofphp/php-cs-fixer ely/php-code-style
|
||||
```
|
||||
|
||||
Then create file `.php-cs` with following contents:
|
||||
Then create file `.php_cs` with following contents:
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -189,7 +194,7 @@ Ensure that a class body contains one blank line after its definition and before
|
||||
**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**: `false`.
|
||||
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`.
|
||||
|
||||
@ -261,3 +266,29 @@ an anonymous class declaration in a case when said class constructor doesn't con
|
||||
|
||||
* `remove_for_anonymous_classes` - if its set to `true`, then braces for anonymous classes without constructor
|
||||
arguments will be removed. **Default**: `false`.
|
||||
|
||||
### `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;
|
||||
|
||||
- $className = User::className();
|
||||
+ $className = User::class;
|
||||
```
|
||||
|
||||
[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/travis/elyby/php-code-style/master.svg?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://travis-ci.org/elyby/php-code-style
|
||||
|
@ -17,10 +17,10 @@
|
||||
"type": "library",
|
||||
"require": {
|
||||
"php": "^7.0",
|
||||
"friendsofphp/php-cs-fixer": "^2.11"
|
||||
"friendsofphp/php-cs-fixer": "^2.13.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.0"
|
||||
"phpunit/phpunit": "^6.5.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@ -30,9 +30,11 @@
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Ely\\CS\\Test\\": "tests/"
|
||||
},
|
||||
"files": [
|
||||
"vendor/friendsofphp/php-cs-fixer/tests/Test/Constraint/SameStringsConstraint.php"
|
||||
]
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"platform": {
|
||||
"php": "7.0.8"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
632
composer.lock
generated
632
composer.lock
generated
File diff suppressed because it is too large
Load Diff
123
src/Fixer/LanguageConstruct/RemoveClassNameMethodUsagesFixer.php
Normal file
123
src/Fixer/LanguageConstruct/RemoveClassNameMethodUsagesFixer.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?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\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 {
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getDefinition() {
|
||||
return new FixerDefinition(
|
||||
'Converts Yii2 `BaseObject::className()` method usage into `::class` keyword.',
|
||||
[
|
||||
new CodeSample(
|
||||
'<?php
|
||||
|
||||
use Foo\Bar\Baz;
|
||||
|
||||
$className = Baz::className();
|
||||
'
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens) {
|
||||
return $tokens->isTokenKindFound(T_STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isRisky() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(SplFileInfo $file, Tokens $tokens) {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tokens $tokens
|
||||
* @param int $index
|
||||
*
|
||||
* @return null|array
|
||||
*/
|
||||
private function getReplaceCandidate(Tokens $tokens, $index) {
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tokens $tokens
|
||||
* @param int $index
|
||||
* @param int $braceOpenIndex
|
||||
* @param int $braceCloseIndex
|
||||
*/
|
||||
private function fixClassNameMethodUsage(Tokens $tokens, int $index, int $braceOpenIndex, int $braceCloseIndex) {
|
||||
$tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
|
||||
$tokens->clearTokenAndMergeSurroundingWhitespace($braceOpenIndex);
|
||||
$tokens->clearAt($index);
|
||||
$tokens->insertAt($index, new Token([CT::T_CLASS_CONSTANT, 'class']));
|
||||
}
|
||||
|
||||
}
|
@ -10,10 +10,10 @@ use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
|
||||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\Preg;
|
||||
use PhpCsFixer\Tokenizer\Token;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
use PhpCsFixer\Tokenizer\TokensAnalyzer;
|
||||
use PhpCsFixer\Utils;
|
||||
|
||||
/**
|
||||
* This is copy of the PR https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/3688
|
||||
@ -142,7 +142,8 @@ new class extends Foo {
|
||||
}
|
||||
|
||||
// The final bit of the whitespace must be the next statement's indentation
|
||||
$lines = Utils::splitLines($content);
|
||||
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)]);
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ use Ely\CS\Fixer\AbstractFixer;
|
||||
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\Preg;
|
||||
use PhpCsFixer\Tokenizer\Token;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
use PhpCsFixer\Utils;
|
||||
use SplFileInfo;
|
||||
|
||||
/**
|
||||
@ -86,6 +86,14 @@ class Foo
|
||||
return $tokens->isAnyTokenKindsFound(self::STATEMENTS);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPriority() {
|
||||
// for the best result should be run after the BracesFixer
|
||||
return -26;
|
||||
}
|
||||
|
||||
protected function applyFix(SplFileInfo $file, Tokens $tokens) {
|
||||
foreach ($tokens as $index => $token) {
|
||||
if (!$token->isGivenKind(self::STATEMENTS)) {
|
||||
@ -115,7 +123,8 @@ class Foo
|
||||
}
|
||||
|
||||
// The final bit of the whitespace must be the next statement's indentation
|
||||
$lines = Utils::splitLines($content);
|
||||
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)]);
|
||||
}
|
||||
@ -126,17 +135,23 @@ class Foo
|
||||
|
||||
if ($nextToken->equals('(')) {
|
||||
$parenthesisEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextIndex);
|
||||
$possibleStartBraceIndex = $tokens->getNextNonWhitespace($parenthesisEndIndex);
|
||||
$possibleBeginBraceIndex = $tokens->getNextNonWhitespace($parenthesisEndIndex);
|
||||
} else {
|
||||
$possibleStartBraceIndex = $nextIndex;
|
||||
$possibleBeginBraceIndex = $nextIndex;
|
||||
}
|
||||
|
||||
// `do {} while ();`
|
||||
if ($tokens[$index]->isGivenKind(T_WHILE) && $tokens[$possibleStartBraceIndex]->equals(';')) {
|
||||
return $possibleStartBraceIndex;
|
||||
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, [';']);
|
||||
}
|
||||
|
||||
$blockEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $possibleStartBraceIndex);
|
||||
$nextStatementIndex = $tokens->getNextMeaningfulToken($blockEnd);
|
||||
if ($nextStatementIndex === null) {
|
||||
return $blockEnd;
|
||||
|
@ -7,6 +7,10 @@ class Rules {
|
||||
|
||||
private static $rules = [
|
||||
'@PSR2' => true,
|
||||
'array_indentation' => true,
|
||||
'array_syntax' => [
|
||||
'syntax' => 'short',
|
||||
],
|
||||
'binary_operator_spaces' => true,
|
||||
'braces' => [
|
||||
'position_after_functions_and_oop_constructs' => 'same',
|
||||
@ -17,41 +21,98 @@ class Rules {
|
||||
'class_attributes_separation' => [
|
||||
'elements' => ['method', 'property'],
|
||||
],
|
||||
'combine_consecutive_issets' => true,
|
||||
'combine_nested_dirname' => true,
|
||||
'compact_nullable_typehint' => true,
|
||||
'concat_space' => [
|
||||
'spacing' => 'one',
|
||||
],
|
||||
'declare_equal_normalize' => true,
|
||||
'dir_constant' => true,
|
||||
'ereg_to_preg' => true,
|
||||
'explicit_string_variable' => true, // Should be configurable to choose between ${var} and {$var}
|
||||
'function_declaration' => [
|
||||
'closure_function_spacing' => 'none',
|
||||
],
|
||||
'function_to_constant' => true,
|
||||
'implode_call' => true,
|
||||
'include' => true,
|
||||
'is_null' => true,
|
||||
'linebreak_after_opening_tag' => true,
|
||||
'list_syntax' => [
|
||||
'syntax' => 'short',
|
||||
],
|
||||
'logical_operators' => true,
|
||||
'lowercase_cast' => true,
|
||||
'lowercase_static_reference' => true,
|
||||
'magic_constant_casing' => true,
|
||||
'magic_method_casing' => true,
|
||||
'method_chaining_indentation' => true,
|
||||
'modernize_types_casting' => true,
|
||||
'multiline_whitespace_before_semicolons' => [
|
||||
'strategy' => 'no_multi_line',
|
||||
],
|
||||
'native_function_casing' => true,
|
||||
'no_alternative_syntax' => true,
|
||||
'no_homoglyph_names' => true,
|
||||
'no_leading_import_slash' => true,
|
||||
'no_leading_namespace_whitespace' => true,
|
||||
'no_mixed_echo_print' => true,
|
||||
'no_multiline_whitespace_around_double_arrow' => true,
|
||||
'no_php4_constructor' => true,
|
||||
'no_short_bool_cast' => true,
|
||||
'no_spaces_around_offset' => true,
|
||||
'no_superfluous_elseif' => true,
|
||||
'no_trailing_comma_in_singleline_array' => true,
|
||||
'no_unneeded_control_parentheses' => true,
|
||||
'no_unneeded_final_method' => true,
|
||||
'no_unused_imports' => true,
|
||||
'no_useless_else' => true,
|
||||
'no_useless_return' => true,
|
||||
'no_whitespace_before_comma_in_array' => true,
|
||||
'no_whitespace_in_blank_line' => true,
|
||||
'non_printable_character' => [
|
||||
'use_escape_sequences_in_strings' => true,
|
||||
],
|
||||
'normalize_index_brace' => true,
|
||||
'object_operator_without_whitespace' => true,
|
||||
'ordered_class_elements' => true,
|
||||
'ordered_imports' => true,
|
||||
'random_api_migration' => true,
|
||||
'ordered_imports' => [
|
||||
'imports_order' => ['class', 'function', 'const'],
|
||||
],
|
||||
'php_unit_construct' => true,
|
||||
'php_unit_expectation' => true,
|
||||
'php_unit_method_casing' => true,
|
||||
'php_unit_mock' => true,
|
||||
'php_unit_namespaced' => true,
|
||||
'php_unit_set_up_tear_down_visibility' => true,
|
||||
'php_unit_strict' => true,
|
||||
'pow_to_exponentiation' => true,
|
||||
'psr4' => true,
|
||||
'return_assignment' => true,
|
||||
'random_api_migration' => [
|
||||
'replacements' => [
|
||||
'getrandmax' => 'mt_getrandmax',
|
||||
'rand' => 'random_int',
|
||||
'srand' => 'mt_srand',
|
||||
],
|
||||
],
|
||||
'return_type_declaration' => [
|
||||
'space_before' => 'none',
|
||||
],
|
||||
'self_accessor' => true,
|
||||
'set_type_to_cast' => true,
|
||||
'short_scalar_cast' => true,
|
||||
'single_quote' => true,
|
||||
'space_after_semicolon' => true,
|
||||
'standardize_increment' => true,
|
||||
'standardize_not_equals' => true,
|
||||
'strict_comparison' => true,
|
||||
'ternary_operator_spaces' => true,
|
||||
'ternary_to_null_coalescing' => true,
|
||||
'trailing_comma_in_multiline_array' => true,
|
||||
'trim_array_spaces' => true,
|
||||
'unary_operator_spaces' => true,
|
||||
'visibility_required' => [
|
||||
'elements' => ['property', 'method', 'const'],
|
||||
],
|
||||
@ -65,6 +126,7 @@ class Rules {
|
||||
'Ely/new_with_braces' => [
|
||||
'remove_for_anonymous_classes' => true,
|
||||
],
|
||||
'Ely/remove_class_name_method_usages' => true,
|
||||
];
|
||||
|
||||
public static function create(array $overwrittenRules = []): array {
|
||||
|
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ely\CS\Test\Fixer\Operator;
|
||||
|
||||
use Ely\CS\Fixer\LanguageConstruct\RemoveClassNameMethodUsagesFixer;
|
||||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
|
||||
|
||||
/**
|
||||
* @covers \Ely\CS\Fixer\LanguageConstruct\RemoveClassNameMethodUsagesFixer
|
||||
*/
|
||||
class RemoveClassNameMethodUsagesFixerTest extends AbstractFixerTestCase {
|
||||
|
||||
/**
|
||||
* @param string $expected
|
||||
* @param null|string $input
|
||||
*
|
||||
* @dataProvider provideFixCases
|
||||
*/
|
||||
public function testFix($expected, $input = null) {
|
||||
$this->doTest($expected, $input);
|
||||
}
|
||||
|
||||
public function provideFixCases() {
|
||||
return [
|
||||
[
|
||||
'<?php echo className();',
|
||||
],
|
||||
[
|
||||
'<?php
|
||||
use Foo\Bar\Baz;
|
||||
|
||||
$exceptionString = Baz::classname();
|
||||
',
|
||||
],
|
||||
[
|
||||
'<?php
|
||||
use Foo\Bar\Baz;
|
||||
|
||||
$className = Baz::class;
|
||||
',
|
||||
'<?php
|
||||
use Foo\Bar\Baz;
|
||||
|
||||
$className = Baz::className();
|
||||
',
|
||||
],
|
||||
[
|
||||
'<?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() {
|
||||
return new RemoveClassNameMethodUsagesFixer();
|
||||
}
|
||||
|
||||
}
|
@ -36,7 +36,7 @@ final class BlankLineBeforeReturnFixerTest extends AbstractFixerTestCase {
|
||||
public function provideFixCases() {
|
||||
$cases = [];
|
||||
$cases[] = [
|
||||
'$a = $a;
|
||||
'$a = $a;
|
||||
return $a;
|
||||
',
|
||||
];
|
||||
|
@ -355,10 +355,211 @@ class Foo
|
||||
'<?php
|
||||
$a = "prev statement";
|
||||
foreach ($coordinates as $coordinate) {
|
||||
[$x, $y] = explode(\',\', $coordinate);
|
||||
$points = explode(",", $coordinate);
|
||||
}
|
||||
',
|
||||
];
|
||||
// Issue 5
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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";
|
||||
}
|
||||
}',
|
||||
];
|
||||
$cases[] = [
|
||||
'<?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;
|
||||
}
|
||||
}',
|
||||
];
|
||||
|
||||
return $cases;
|
||||
}
|
||||
|
Reference in New Issue
Block a user