Skip to content

Commit 2181e77

Browse files
authored
Merge pull request #44 from viktorkulahin/3.0.1
3.0.1
2 parents f1b6167 + f9f72da commit 2181e77

File tree

9 files changed

+153
-76
lines changed

9 files changed

+153
-76
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ Semantic Versioning is maintained only for the following:
1111
The fixers themselves can change their behavior on any update.
1212
New fixers could be added with minor releases, this would require changes in configuration if migration mode is used.
1313

14+
## 3.0.1
15+
16+
### Changed
17+
- friendsofphp/php-cs-fixer` minor update to version 3.64.0
18+
- Dependencies update (phpunit)
19+
- Modified the `download-phar.sh` script to download the latest PHAR file.
20+
- VisibilityPropertiesFixer has been changed to properly detect variables declared in the __constructor.Some other refactoring of fixer.
21+
- DefaultValuesInConstructorFixer priority changed to work wright with VisibilityPropertiesFixer.
22+
- SplittingInSeveralLinesFixer was refactored to handle situations with splitting lines in closures and functions parameters, including parameters in __constructor.
23+
1424
## 3.0.0
1525

1626
### Changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ You can look at `.travis.yml` file in this repository for integration with travi
5252

5353
Run in project directory by command: `{your-bin-dir}/php-cs-fixer fix /path/to/code --verbose --dry-run --diff`
5454

55-
Use `--config=.php-cs-fixer.php` flag for custom configuration.
55+
Use `--config=php-cs-fixer.php` flag for custom configuration.
5656

5757
If `/path/to/code` is not defined `php-cs-fixer` will run files from default `src` directory excluding `Test` folders.
5858

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"description": "PHP CS Fixer config for Paysera conventions",
44
"type": "library",
55
"require": {
6-
"php": ">=7.4",
6+
"php": ">=7.4 <8.0 || >=8.1",
77
"doctrine/inflector": "^1.0 || ^2.0"
88
},
99
"require-dev": {
10-
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.0",
11-
"friendsofphp/php-cs-fixer": "3.60.0",
10+
"phpunit/phpunit": "^9.3.0",
11+
"friendsofphp/php-cs-fixer": "3.64.0",
1212
"sanmai/phpunit-legacy-adapter": "^6.4 || ^8.2"
1313
},
1414
"autoload": {

download-phar.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22

3-
curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.60.0/php-cs-fixer.phar -o paysera-php-cs-fixer
3+
curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.64.0/php-cs-fixer.phar -o paysera-php-cs-fixer
44
chmod +x paysera-php-cs-fixer

paysera-php-cs-fixer

31.2 KB
Binary file not shown.

src/Fixer/PhpBasic/CodeStyle/DefaultValuesInConstructorFixer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public function getName(): string
5454
return 'Paysera/php_basic_code_style_default_values_in_constructor';
5555
}
5656

57+
public function getPriority(): int
58+
{
59+
// Should run after `VisibilityPropertiesFixer`
60+
return 61;
61+
}
62+
5763
public function isCandidate(Tokens $tokens): bool
5864
{
5965
return $tokens->isAnyTokenKindsFound([T_CLASS]);
@@ -84,10 +90,16 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
8490
$parentConstructNeeded = true;
8591
}
8692
$subsequentDeclarativeToken = $tokens->getNextMeaningfulToken($key);
93+
94+
// @TODO: PHP 7.4 support, drop condition when there will be no PHP 7.4 support.
95+
$tokenKinds = [T_STATIC, T_FUNCTION];
96+
if (defined('T_READONLY')) {
97+
$tokenKinds[] = T_READONLY;
98+
}
99+
87100
if (
88101
$token->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])
89-
&& !$tokens[$subsequentDeclarativeToken]->isGivenKind(T_STATIC)
90-
&& !$tokens[$subsequentDeclarativeToken]->isGivenKind(T_FUNCTION)
102+
&& !$tokens[$subsequentDeclarativeToken]->isGivenKind($tokenKinds)
91103
) {
92104
$propertyNameIndex = $tokens->getNextNonWhitespace($key);
93105
$endOfPropertyDeclarationSemicolon = $tokens->getNextTokenOfKind($key, [';']);

src/Fixer/PhpBasic/CodeStyle/MethodNamingFixer.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,43 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
9494
$functionTokenIndex = $tokens->getPrevNonWhitespace($key);
9595
$visibilityTokenIndex = $functionTokenIndex ? $tokens->getPrevNonWhitespace($functionTokenIndex) : null;
9696

97-
if ($functionTokenIndex && $visibilityTokenIndex) {
98-
if (
99-
$token->isGivenKind(T_STRING)
100-
&& $tokens[$key + 1]->equals('(')
101-
&& $tokens[$functionTokenIndex]->isGivenKind(T_FUNCTION)
102-
&& $tokens[$visibilityTokenIndex]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])
103-
) {
104-
$functionName = $tokens[$key]->getContent();
105-
$parenthesesEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $key + 1);
106-
$nextIndex = $tokens->getNextMeaningfulToken($parenthesesEndIndex);
107-
108-
$returnType = null;
109-
if ($tokens[$nextIndex]->isGivenKind(CT::T_TYPE_COLON)) {
110-
$typeIndex = $tokens->getNextMeaningfulToken($nextIndex);
111-
$returnType = $tokens[$typeIndex]->getContent();
112-
$nextIndex = $tokens->getNextMeaningfulToken($typeIndex);
113-
}
114-
115-
if (!$tokens[$nextIndex]->equals('{')) {
116-
continue;
117-
}
118-
119-
$this->fixMethod($tokens, $functionName, $visibilityTokenIndex, $nextIndex, $returnType);
97+
if ($functionTokenIndex === null || $visibilityTokenIndex === null) {
98+
continue;
99+
}
100+
101+
if (
102+
$token->isGivenKind(T_STRING)
103+
&& $tokens[$key + 1]->equals('(')
104+
&& $tokens[$functionTokenIndex]->isGivenKind(T_FUNCTION)
105+
&& $tokens[$visibilityTokenIndex]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])
106+
) {
107+
$functionName = $tokens[$key]->getContent();
108+
$parenthesesEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $key + 1);
109+
$nextIndex = $tokens->getNextMeaningfulToken($parenthesesEndIndex);
110+
111+
$returnType = null;
112+
if ($tokens[$nextIndex]->isGivenKind(CT::T_TYPE_COLON)) {
113+
$typeIndex = $tokens->getNextMeaningfulToken($nextIndex);
114+
$returnType = $tokens[$typeIndex]->getContent();
115+
$nextIndex = $tokens->getNextMeaningfulToken($typeIndex);
116+
}
117+
118+
if (!$tokens[$nextIndex]->equals('{')) {
119+
continue;
120120
}
121+
122+
$this->fixMethod($tokens, $functionName, $visibilityTokenIndex, $nextIndex, $returnType);
121123
}
122124
}
123125
}
124126

125-
private function fixMethod(Tokens $tokens, $functionName, $visibilityTokenIndex, $curlyBraceStartIndex, $returnType)
126-
{
127+
private function fixMethod(
128+
Tokens $tokens,
129+
$functionName,
130+
$visibilityTokenIndex,
131+
$curlyBraceStartIndex,
132+
$returnType
133+
): void {
127134
$index = $tokens->getPrevNonWhitespace($visibilityTokenIndex);
128135
$docBlockIndex = null;
129136
if ($tokens[$index]->isGivenKind(T_DOC_COMMENT)) {
@@ -133,7 +140,7 @@ private function fixMethod(Tokens $tokens, $functionName, $visibilityTokenIndex,
133140
}
134141

135142
$shouldReturnBool = preg_match(
136-
'#^(?:' . implode('|', $this->boolFunctionPrefixes) . ')[A-Z]#',
143+
'#^(' . implode('|', $this->boolFunctionPrefixes) . ')[A-Z]#',
137144
$functionName,
138145
);
139146

@@ -167,7 +174,7 @@ private function hasFunctionReturnClause(Tokens $tokens, int $curlyBraceStartInd
167174
return false;
168175
}
169176

170-
private function insertComment(Tokens $tokens, int $insertIndex)
177+
private function insertComment(Tokens $tokens, int $insertIndex): void
171178
{
172179
$comment = '// TODO: ' . self::BOOL_FUNCTION_COMMENT;
173180
if (!$tokens[$tokens->getNextNonWhitespace($insertIndex)]->isGivenKind(T_COMMENT)) {

src/Fixer/PhpBasic/CodeStyle/SplittingInSeveralLinesFixer.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,15 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
110110
do {
111111
foreach ($startEndTokens as $startValue => $endValue) {
112112
if ($token->getContent() === $startValue) {
113-
$groupedItem = $this->parser->parseUntil($token, $endValue);
114-
$this->fixWhitespaceForItem($groupedItem);
113+
$processResult = $this->processForClosureAndFunctionParameters($token, $endValue);
114+
$token = $processResult ?? $token;
115115

116-
$token = $groupedItem->lastToken();
116+
if ($processResult === null) {
117+
$groupedItem = $this->parser->parseUntil($token, $endValue);
118+
$this->fixWhitespaceForItem($groupedItem);
119+
120+
$token = $groupedItem->lastToken();
121+
}
117122
}
118123
}
119124

@@ -123,7 +128,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
123128
$this->contextualTokenBuilder->overrideTokens($tokens, $firstToken);
124129
}
125130

126-
private function fixWhitespaceForItem(ItemInterface $groupedItem)
131+
private function fixWhitespaceForItem(ItemInterface $groupedItem): void
127132
{
128133
$standardIndent = $this->whitespacesConfig->getIndent();
129134

@@ -159,7 +164,7 @@ private function fixWhitespaceForItem(ItemInterface $groupedItem)
159164
}
160165
}
161166

162-
private function ensureContentForPrefixWhitespace(ComplexItemList $itemList, string $content)
167+
private function ensureContentForPrefixWhitespace(ComplexItemList $itemList, string $content): void
163168
{
164169
$prefixWhitespaceItem = $itemList->getFirstPrefixWhitespaceItem();
165170

@@ -202,7 +207,7 @@ private function ensureContentForPrefixWhitespace(ComplexItemList $itemList, str
202207
}
203208
}
204209

205-
private function ensureContentForPostfixWhitespace(ComplexItemList $itemList, string $content)
210+
private function ensureContentForPostfixWhitespace(ComplexItemList $itemList, string $content): void
206211
{
207212
$postfixWhitespaceItem = $itemList->getFirstPostfixWhitespaceItem();
208213

@@ -238,7 +243,7 @@ private function ensureContentForPostfixWhitespace(ComplexItemList $itemList, st
238243
}
239244
}
240245

241-
private function fixSeparators(SeparatedItemList $itemList, string $indent)
246+
private function fixSeparators(SeparatedItemList $itemList, string $indent): void
242247
{
243248
$separator = $itemList->getSeparator();
244249

@@ -275,7 +280,7 @@ private function isItemListUnsupported(ComplexItemList $itemList): bool
275280
);
276281
}
277282

278-
private function fixWhitespaceBefore(ItemInterface $item, ?string $whitespaceBefore, bool $forceWhitespace)
283+
private function fixWhitespaceBefore(ItemInterface $item, ?string $whitespaceBefore, bool $forceWhitespace): void
279284
{
280285
$firstToken = $item->firstToken();
281286
if ($firstToken->isWhitespace()) {
@@ -295,7 +300,7 @@ private function fixWhitespaceAfter(ItemInterface $item, ?string $whitespaceAfte
295300
}
296301
}
297302

298-
private function replaceWithIfNeeded(ContextualToken $token, string $replacement = null)
303+
private function replaceWithIfNeeded(ContextualToken $token, string $replacement = null): void
299304
{
300305
if ($replacement === null) {
301306
$token->previousToken()->setNextContextualToken($token->getNextToken());
@@ -318,4 +323,24 @@ private function hasExtraLinesWithCorrectEnding(string $current, string $replace
318323
&& substr($current, -strlen($replacement)) === $replacement
319324
);
320325
}
326+
327+
private function processForClosureAndFunctionParameters(ContextualToken $token, string $endValue): ?ContextualToken
328+
{
329+
if ($token->getContent() === '(') {
330+
if ($token->nextNonWhitespaceToken()->getContent() === 'function') {
331+
return $token;
332+
}
333+
334+
if (
335+
$token
336+
->previousNonWhitespaceToken()
337+
->previousNonWhitespaceToken()
338+
->getContent() === 'function'
339+
) {
340+
return $this->parser->parseUntil($token, $endValue)->lastToken();
341+
}
342+
}
343+
344+
return null;
345+
}
321346
}

0 commit comments

Comments
 (0)