Skip to content

Commit 3d4af3e

Browse files
authored
Deprecate InternalClassCasingFixer (#742)
1 parent 421dda4 commit 3d4af3e

File tree

6 files changed

+30
-88
lines changed

6 files changed

+30
-88
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## v3.8.0
44
- Update minimum PHP version to 7.4
5+
- Update minimum PHP CS Fixer version to 3.6.0
56
- DataProviderStaticFixer - add option "force"
7+
- Deprecate InternalClassCasingFixer
68

79
## v3.7.0
810
- Add NoTrailingCommaInSinglelineFixer

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3373-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3374-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
@@ -132,6 +132,7 @@ Declare statement for strict types must be placed in the same line, after openin
132132

133133
#### InternalClassCasingFixer
134134
Classes defined internally by extension or core must be referenced with the correct case.
135+
DEPRECATED: use `class_reference_name_casing` instead.
135136
```diff
136137
<?php
137138
-$foo = new STDClass();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"php": "^7.4 || ^8.0",
1414
"ext-filter": "*",
1515
"ext-tokenizer": "*",
16-
"friendsofphp/php-cs-fixer": "^3.1.0",
16+
"friendsofphp/php-cs-fixer": "^3.6.0",
1717
"symfony/finder": "^4.0 || ^5.0 || ^6.0"
1818
},
1919
"require-dev": {

src/Fixer/InternalClassCasingFixer.php

Lines changed: 20 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,26 @@
1111

1212
namespace PhpCsFixerCustomFixers\Fixer;
1313

14+
use PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer;
15+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1416
use PhpCsFixer\FixerDefinition\CodeSample;
1517
use PhpCsFixer\FixerDefinition\FixerDefinition;
1618
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
17-
use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer;
18-
use PhpCsFixer\Tokenizer\CT;
19-
use PhpCsFixer\Tokenizer\Token;
2019
use PhpCsFixer\Tokenizer\Tokens;
2120

22-
final class InternalClassCasingFixer extends AbstractFixer
21+
/**
22+
* @deprecated
23+
*/
24+
final class InternalClassCasingFixer extends AbstractFixer implements DeprecatedFixerInterface
2325
{
26+
/** @var ClassReferenceNameCasingFixer */
27+
private $classReferenceNameCasingFixer;
28+
29+
public function __construct()
30+
{
31+
$this->classReferenceNameCasingFixer = new ClassReferenceNameCasingFixer();
32+
}
33+
2434
public function getDefinition(): FixerDefinitionInterface
2535
{
2636
return new FixerDefinition(
@@ -31,99 +41,26 @@ public function getDefinition(): FixerDefinitionInterface
3141

3242
public function getPriority(): int
3343
{
34-
return 0;
44+
return $this->classReferenceNameCasingFixer->getPriority();
3545
}
3646

3747
public function isCandidate(Tokens $tokens): bool
3848
{
39-
return $tokens->isTokenKindFound(\T_STRING);
49+
return $this->classReferenceNameCasingFixer->isCandidate($tokens);
4050
}
4151

4252
public function isRisky(): bool
4353
{
44-
return false;
54+
return $this->classReferenceNameCasingFixer->isRisky();
4555
}
4656

4757
public function fix(\SplFileInfo $file, Tokens $tokens): void
4858
{
49-
$namespaces = (new NamespacesAnalyzer())->getDeclarations($tokens);
50-
51-
foreach ($namespaces as $namespace) {
52-
$this->fixCasing($tokens, $namespace->getScopeStartIndex(), $namespace->getScopeEndIndex(), $namespace->getFullName() === '');
53-
}
54-
}
55-
56-
private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool $isInGlobalNamespace): void
57-
{
58-
for ($index = $startIndex; $index < $endIndex; $index++) {
59-
if (!$tokens[$index]->isGivenKind(\T_STRING)) {
60-
continue;
61-
}
62-
63-
if (!$this->isGlobalClassUsage($tokens, $index, $isInGlobalNamespace)) {
64-
continue;
65-
}
66-
67-
$correctCase = $this->getCorrectCase($tokens[$index]->getContent());
68-
69-
if ($correctCase === $tokens[$index]->getContent()) {
70-
continue;
71-
}
72-
73-
$tokens[$index] = new Token([\T_STRING, $correctCase]);
74-
}
75-
}
76-
77-
private function isGlobalClassUsage(Tokens $tokens, int $index, bool $isInGlobalNamespace): bool
78-
{
79-
$prevIndex = $tokens->getPrevMeaningfulToken($index);
80-
\assert(\is_int($prevIndex));
81-
82-
if ($tokens[$prevIndex]->isGivenKind(\T_NS_SEPARATOR)) {
83-
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
84-
\assert(\is_int($prevIndex));
85-
86-
if ($tokens[$prevIndex]->isGivenKind(\T_STRING)) {
87-
return false;
88-
}
89-
} elseif (!$isInGlobalNamespace) {
90-
return false;
91-
}
92-
93-
if ($tokens[$prevIndex]->isGivenKind([\T_AS, \T_CLASS, \T_CONST, \T_DOUBLE_COLON, \T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
94-
return false;
95-
}
96-
97-
$nextIndex = $tokens->getNextMeaningfulToken($index);
98-
\assert(\is_int($nextIndex));
99-
100-
if ($tokens[$nextIndex]->isGivenKind(\T_NS_SEPARATOR)) {
101-
return false;
102-
}
103-
104-
return $tokens[$prevIndex]->isGivenKind([\T_NEW]) || !$tokens[$nextIndex]->equals('(');
59+
$this->classReferenceNameCasingFixer->fix($file, $tokens);
10560
}
10661

107-
private function getCorrectCase(string $className): string
62+
public function getSuccessorsNames(): array
10863
{
109-
/** @var null|array<string, string> $classes */
110-
static $classes;
111-
112-
if ($classes === null) {
113-
$classes = [];
114-
foreach (\get_declared_classes() as $class) {
115-
if ((new \ReflectionClass($class))->isInternal()) {
116-
$classes[\strtolower($class)] = $class;
117-
}
118-
}
119-
}
120-
121-
$lowercaseClassName = \strtolower($className);
122-
123-
if (!\array_key_exists($lowercaseClassName, $classes)) {
124-
return $className;
125-
}
126-
127-
return $classes[$lowercaseClassName];
64+
return [$this->classReferenceNameCasingFixer->getName()];
12865
}
12966
}

src/Fixer/PhpdocVarAnnotationToAssertFixer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ private function getAssertTokens(Tokens $tokens, int $docCommentIndex, string $v
120120
}
121121

122122
$types = $annotation->getTypeExpression()->getTypes();
123-
if ($types === []) {
124-
return null;
125-
}
126123

127124
$assertCode = '<?php assert(';
128125

tests/Fixer/InternalClassCasingFixerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function testIsRisky(): void
2323
self::assertFalse($this->fixer->isRisky());
2424
}
2525

26+
public function testSuccessorName(): void
27+
{
28+
self::assertContains('class_reference_name_casing', $this->fixer->getSuccessorsNames());
29+
}
30+
2631
/**
2732
* @dataProvider provideFixCases
2833
*/

0 commit comments

Comments
 (0)