Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: function_to_constant get_class() replacement #7770

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/rules/language_construct/function_to_constant.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Example #1
{
- echo get_class();
- echo get_called_class();
+ echo __CLASS__;
+ echo self::class;
+ echo static::class;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Output/Progress/DotsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(OutputContext $context)
*/
public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
throw new \BadMethodCallException('Cannot serialize '.self::class);
}

/**
Expand All @@ -76,7 +76,7 @@ public function __sleep(): array
*/
public function __wakeup(): void
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
throw new \BadMethodCallException('Cannot unserialize '.self::class);
}

public function onFixerFileProcessed(FixerFileProcessedEvent $event): void
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Output/Progress/PercentageBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(OutputContext $context)
*/
public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
throw new \BadMethodCallException('Cannot serialize '.self::class);
}

/**
Expand All @@ -60,7 +60,7 @@ public function __sleep(): array
*/
public function __wakeup(): void
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
throw new \BadMethodCallException('Cannot unserialize '.self::class);
}

public function onFixerFileProcessed(FixerFileProcessedEvent $event): void
Expand Down
4 changes: 2 additions & 2 deletions src/FileRemoval.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __destruct()
*/
public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
throw new \BadMethodCallException('Cannot serialize '.self::class);
}

/**
Expand All @@ -58,7 +58,7 @@ public function __sleep(): array
*/
public function __wakeup(): void
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
throw new \BadMethodCallException('Cannot unserialize '.self::class);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Fixer/LanguageConstruct/FunctionToConstantFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function __construct()
new Token([T_DOUBLE_COLON, '::']),
new Token([CT::T_CLASS_CONSTANT, 'class']),
],
'get_class' => [new Token([T_CLASS_C, '__CLASS__'])],
'get_class' => [
new Token([T_STRING, 'self']),
new Token([T_DOUBLE_COLON, '::']),
new Token([CT::T_CLASS_CONSTANT, 'class']),
],
'get_class_this' => [
new Token([T_STATIC, 'static']),
new Token([T_DOUBLE_COLON, '::']),
Expand Down Expand Up @@ -167,7 +171,10 @@ private function fixFunctionCallToConstant(Tokens $tokens, int $index, int $brac
$tokens->clearTokenAndMergeSurroundingWhitespace($i);
}

if ($replacements[0]->isGivenKind([T_CLASS_C, T_STATIC])) {
if (
$replacements[0]->isGivenKind([T_CLASS_C, T_STATIC])
|| ($replacements[0]->isGivenKind(T_STRING) && 'self' === $replacements[0]->getContent())
) {
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
if ($prevToken->isGivenKind(T_NS_SEPARATOR)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Linter/ProcessLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __destruct()
*/
public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
throw new \BadMethodCallException('Cannot serialize '.self::class);
}

/**
Expand All @@ -95,7 +95,7 @@ public function __sleep(): array
*/
public function __wakeup(): void
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
throw new \BadMethodCallException('Cannot unserialize '.self::class);
}

public function isAsync(): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer/CT.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static function getMapById(): array
static $constants;

if (null === $constants) {
$reflection = new \ReflectionClass(__CLASS__);
$reflection = new \ReflectionClass(self::class);
$constants = array_flip($reflection->getConstants());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testThatSrcClassHaveTestClass(string $className): void
$testClassName = 'PhpCsFixer\\Tests'.substr($className, 10).'Test';

if (\in_array($className, self::$classesWithoutTests, true)) {
self::assertFalse(class_exists($testClassName), sprintf('Class "%s" already has tests, so it should be removed from "%s::$classesWithoutTests".', $className, __CLASS__));
self::assertFalse(class_exists($testClassName), sprintf('Class "%s" already has tests, so it should be removed from "%s::$classesWithoutTests".', $className, self::class));
self::markTestIncomplete(sprintf('Class "%s" has no tests yet, please help and add it.', $className));
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Fixer/LanguageConstruct/FunctionToConstantFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class A
public function echoClassName($notMe)
{
echo get_class($notMe);
echo __CLASS__/** 1 *//* 2 */;
echo __CLASS__;
echo self::class/** 1 *//* 2 */;
echo self::class;
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ class B
];

yield 'get_class with leading backslash' => [
'<?php __CLASS__;',
'<?php self::class;',
'<?php \get_class();',
];

Expand Down Expand Up @@ -251,7 +251,7 @@ class B
];

yield [
'<?php class Foo{ public function Bar(){ echo static::class; echo __CLASS__; }}',
'<?php class Foo{ public function Bar(){ echo static::class; echo self::class; }}',
'<?php class Foo{ public function Bar(){ echo \get_class((($this))); echo get_class(); }}',
['functions' => ['get_class_this', 'get_class']],
];
Expand Down