Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: PhpUnitMethodCasingFixer - do not touch anonymous class#8463

Merged
kubawerlos merged 5 commits into
PHP-CS-Fixer:masterfrom
6b7562617765726c6f73:fix_PhpUnitMethodCasingFixer
Mar 21, 2025
Merged

fix: PhpUnitMethodCasingFixer - do not touch anonymous class#8463
kubawerlos merged 5 commits into
PHP-CS-Fixer:masterfrom
6b7562617765726c6f73:fix_PhpUnitMethodCasingFixer

Conversation

@kubawerlos
Copy link
Copy Markdown
Member

Fixes #8462

@kubawerlos kubawerlos enabled auto-merge (squash) February 24, 2025 18:22
@coveralls
Copy link
Copy Markdown

coveralls commented Feb 24, 2025

Coverage Status

coverage: 94.876% (+0.02%) from 94.853%
when pulling 6d221c7 on 6b7562617765726c6f73:fix_PhpUnitMethodCasingFixer
into fe85a5d on PHP-CS-Fixer:master.

@obresoft
Copy link
Copy Markdown
Contributor

obresoft commented Feb 24, 2025

I'm not sure if this is helpful, but I was thinking about checking boundaries within the class. Thank you for fixing!

final protected function isTestAttributePresent(Tokens $tokens, int $index): bool
    {
        $prevMethodEndIndex = $tokens->getPrevTokenOfKind($index, ['}']);
        $classIndex = $tokens->getPrevTokenOfKind($index, [[T_CLASS]]);
        $currentIndex = $index - 1;

        while (true) {
            $attributeIndex = $tokens->getPrevTokenOfKind($currentIndex, [[T_ATTRIBUTE]]);
            if (null === $attributeIndex || $attributeIndex <= $prevMethodEndIndex || $attributeIndex <= $classIndex) {
                break;
            }

            foreach (AttributeAnalyzer::collect($tokens, $attributeIndex) as $attributeAnalysis) {
                foreach ($attributeAnalysis->getAttributes() as $attribute) {
                    $attributeName = ltrim(self::getFullyQualifiedName($tokens, $attribute['name']), '\\');
                    if ('phpunit\framework\attributes\test' === $attributeName) {
                        return true;
                    }
                }
            }

            $currentIndex = $attributeIndex - 1;
        }

        return false;
    }

@kubawerlos
Copy link
Copy Markdown
Member Author

We can actually go the other way and simplify more - we can remove $prevMethodEndIndex as checking $tokens->getPrevTokenOfKind($index, ['{', [T_ATTRIBUTE]]) will never reach previous method's attribute.

@kubawerlos kubawerlos force-pushed the fix_PhpUnitMethodCasingFixer branch from c126fda to ad82059 Compare February 24, 2025 19:30
@ruudk
Copy link
Copy Markdown
Contributor

ruudk commented Mar 14, 2025

What's needed to merge this? Can I help?

@kubawerlos kubawerlos merged commit c3d07b5 into PHP-CS-Fixer:master Mar 21, 2025
@kubawerlos kubawerlos deleted the fix_PhpUnitMethodCasingFixer branch March 21, 2025 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

php_unit_method_casing renames methods declared inside the test (e.g. anonymous class)

5 participants