Skip to content

Commit

Permalink
Merge pull request #44 from viktorkulahin/3.0.1
Browse files Browse the repository at this point in the history
3.0.1
  • Loading branch information
mSprunskas authored Oct 3, 2024
2 parents f1b6167 + f9f72da commit 2181e77
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 76 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Semantic Versioning is maintained only for the following:
The fixers themselves can change their behavior on any update.
New fixers could be added with minor releases, this would require changes in configuration if migration mode is used.

## 3.0.1

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

## 3.0.0

### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ You can look at `.travis.yml` file in this repository for integration with travi

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"description": "PHP CS Fixer config for Paysera conventions",
"type": "library",
"require": {
"php": ">=7.4",
"php": ">=7.4 <8.0 || >=8.1",
"doctrine/inflector": "^1.0 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"friendsofphp/php-cs-fixer": "3.60.0",
"phpunit/phpunit": "^9.3.0",
"friendsofphp/php-cs-fixer": "3.64.0",
"sanmai/phpunit-legacy-adapter": "^6.4 || ^8.2"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion download-phar.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.60.0/php-cs-fixer.phar -o paysera-php-cs-fixer
curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.64.0/php-cs-fixer.phar -o paysera-php-cs-fixer
chmod +x paysera-php-cs-fixer
Binary file modified paysera-php-cs-fixer
Binary file not shown.
16 changes: 14 additions & 2 deletions src/Fixer/PhpBasic/CodeStyle/DefaultValuesInConstructorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function getName(): string
return 'Paysera/php_basic_code_style_default_values_in_constructor';
}

public function getPriority(): int
{
// Should run after `VisibilityPropertiesFixer`
return 61;
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_CLASS]);
Expand Down Expand Up @@ -84,10 +90,16 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
$parentConstructNeeded = true;
}
$subsequentDeclarativeToken = $tokens->getNextMeaningfulToken($key);

// @TODO: PHP 7.4 support, drop condition when there will be no PHP 7.4 support.
$tokenKinds = [T_STATIC, T_FUNCTION];
if (defined('T_READONLY')) {
$tokenKinds[] = T_READONLY;
}

if (
$token->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])
&& !$tokens[$subsequentDeclarativeToken]->isGivenKind(T_STATIC)
&& !$tokens[$subsequentDeclarativeToken]->isGivenKind(T_FUNCTION)
&& !$tokens[$subsequentDeclarativeToken]->isGivenKind($tokenKinds)
) {
$propertyNameIndex = $tokens->getNextNonWhitespace($key);
$endOfPropertyDeclarationSemicolon = $tokens->getNextTokenOfKind($key, [';']);
Expand Down
61 changes: 34 additions & 27 deletions src/Fixer/PhpBasic/CodeStyle/MethodNamingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,43 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
$functionTokenIndex = $tokens->getPrevNonWhitespace($key);
$visibilityTokenIndex = $functionTokenIndex ? $tokens->getPrevNonWhitespace($functionTokenIndex) : null;

if ($functionTokenIndex && $visibilityTokenIndex) {
if (
$token->isGivenKind(T_STRING)
&& $tokens[$key + 1]->equals('(')
&& $tokens[$functionTokenIndex]->isGivenKind(T_FUNCTION)
&& $tokens[$visibilityTokenIndex]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])
) {
$functionName = $tokens[$key]->getContent();
$parenthesesEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $key + 1);
$nextIndex = $tokens->getNextMeaningfulToken($parenthesesEndIndex);

$returnType = null;
if ($tokens[$nextIndex]->isGivenKind(CT::T_TYPE_COLON)) {
$typeIndex = $tokens->getNextMeaningfulToken($nextIndex);
$returnType = $tokens[$typeIndex]->getContent();
$nextIndex = $tokens->getNextMeaningfulToken($typeIndex);
}

if (!$tokens[$nextIndex]->equals('{')) {
continue;
}

$this->fixMethod($tokens, $functionName, $visibilityTokenIndex, $nextIndex, $returnType);
if ($functionTokenIndex === null || $visibilityTokenIndex === null) {
continue;
}

if (
$token->isGivenKind(T_STRING)
&& $tokens[$key + 1]->equals('(')
&& $tokens[$functionTokenIndex]->isGivenKind(T_FUNCTION)
&& $tokens[$visibilityTokenIndex]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])
) {
$functionName = $tokens[$key]->getContent();
$parenthesesEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $key + 1);
$nextIndex = $tokens->getNextMeaningfulToken($parenthesesEndIndex);

$returnType = null;
if ($tokens[$nextIndex]->isGivenKind(CT::T_TYPE_COLON)) {
$typeIndex = $tokens->getNextMeaningfulToken($nextIndex);
$returnType = $tokens[$typeIndex]->getContent();
$nextIndex = $tokens->getNextMeaningfulToken($typeIndex);
}

if (!$tokens[$nextIndex]->equals('{')) {
continue;
}

$this->fixMethod($tokens, $functionName, $visibilityTokenIndex, $nextIndex, $returnType);
}
}
}

private function fixMethod(Tokens $tokens, $functionName, $visibilityTokenIndex, $curlyBraceStartIndex, $returnType)
{
private function fixMethod(
Tokens $tokens,
$functionName,
$visibilityTokenIndex,
$curlyBraceStartIndex,
$returnType
): void {
$index = $tokens->getPrevNonWhitespace($visibilityTokenIndex);
$docBlockIndex = null;
if ($tokens[$index]->isGivenKind(T_DOC_COMMENT)) {
Expand All @@ -133,7 +140,7 @@ private function fixMethod(Tokens $tokens, $functionName, $visibilityTokenIndex,
}

$shouldReturnBool = preg_match(
'#^(?:' . implode('|', $this->boolFunctionPrefixes) . ')[A-Z]#',
'#^(' . implode('|', $this->boolFunctionPrefixes) . ')[A-Z]#',
$functionName,
);

Expand Down Expand Up @@ -167,7 +174,7 @@ private function hasFunctionReturnClause(Tokens $tokens, int $curlyBraceStartInd
return false;
}

private function insertComment(Tokens $tokens, int $insertIndex)
private function insertComment(Tokens $tokens, int $insertIndex): void
{
$comment = '// TODO: ' . self::BOOL_FUNCTION_COMMENT;
if (!$tokens[$tokens->getNextNonWhitespace($insertIndex)]->isGivenKind(T_COMMENT)) {
Expand Down
43 changes: 34 additions & 9 deletions src/Fixer/PhpBasic/CodeStyle/SplittingInSeveralLinesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
do {
foreach ($startEndTokens as $startValue => $endValue) {
if ($token->getContent() === $startValue) {
$groupedItem = $this->parser->parseUntil($token, $endValue);
$this->fixWhitespaceForItem($groupedItem);
$processResult = $this->processForClosureAndFunctionParameters($token, $endValue);
$token = $processResult ?? $token;

$token = $groupedItem->lastToken();
if ($processResult === null) {
$groupedItem = $this->parser->parseUntil($token, $endValue);
$this->fixWhitespaceForItem($groupedItem);

$token = $groupedItem->lastToken();
}
}
}

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

private function fixWhitespaceForItem(ItemInterface $groupedItem)
private function fixWhitespaceForItem(ItemInterface $groupedItem): void
{
$standardIndent = $this->whitespacesConfig->getIndent();

Expand Down Expand Up @@ -159,7 +164,7 @@ private function fixWhitespaceForItem(ItemInterface $groupedItem)
}
}

private function ensureContentForPrefixWhitespace(ComplexItemList $itemList, string $content)
private function ensureContentForPrefixWhitespace(ComplexItemList $itemList, string $content): void
{
$prefixWhitespaceItem = $itemList->getFirstPrefixWhitespaceItem();

Expand Down Expand Up @@ -202,7 +207,7 @@ private function ensureContentForPrefixWhitespace(ComplexItemList $itemList, str
}
}

private function ensureContentForPostfixWhitespace(ComplexItemList $itemList, string $content)
private function ensureContentForPostfixWhitespace(ComplexItemList $itemList, string $content): void
{
$postfixWhitespaceItem = $itemList->getFirstPostfixWhitespaceItem();

Expand Down Expand Up @@ -238,7 +243,7 @@ private function ensureContentForPostfixWhitespace(ComplexItemList $itemList, st
}
}

private function fixSeparators(SeparatedItemList $itemList, string $indent)
private function fixSeparators(SeparatedItemList $itemList, string $indent): void
{
$separator = $itemList->getSeparator();

Expand Down Expand Up @@ -275,7 +280,7 @@ private function isItemListUnsupported(ComplexItemList $itemList): bool
);
}

private function fixWhitespaceBefore(ItemInterface $item, ?string $whitespaceBefore, bool $forceWhitespace)
private function fixWhitespaceBefore(ItemInterface $item, ?string $whitespaceBefore, bool $forceWhitespace): void
{
$firstToken = $item->firstToken();
if ($firstToken->isWhitespace()) {
Expand All @@ -295,7 +300,7 @@ private function fixWhitespaceAfter(ItemInterface $item, ?string $whitespaceAfte
}
}

private function replaceWithIfNeeded(ContextualToken $token, string $replacement = null)
private function replaceWithIfNeeded(ContextualToken $token, string $replacement = null): void
{
if ($replacement === null) {
$token->previousToken()->setNextContextualToken($token->getNextToken());
Expand All @@ -318,4 +323,24 @@ private function hasExtraLinesWithCorrectEnding(string $current, string $replace
&& substr($current, -strlen($replacement)) === $replacement
);
}

private function processForClosureAndFunctionParameters(ContextualToken $token, string $endValue): ?ContextualToken
{
if ($token->getContent() === '(') {
if ($token->nextNonWhitespaceToken()->getContent() === 'function') {
return $token;
}

if (
$token
->previousNonWhitespaceToken()
->previousNonWhitespaceToken()
->getContent() === 'function'
) {
return $this->parser->parseUntil($token, $endValue)->lastToken();
}
}

return null;
}
}
Loading

0 comments on commit 2181e77

Please sign in to comment.