Skip to content

Commit

Permalink
bug #5384 PsrAutoloadingFixer - do not remove directory structure fro…
Browse files Browse the repository at this point in the history
…m the Class name (kubawerlos, keradus)

This PR was squashed before being merged into the 2.17 branch.

Discussion
----------

PsrAutoloadingFixer - do not remove directory structure from the Class name

Fixes #5377

Pinging @jaydiablo for review (as a reporter of the bug) - the issue you have reported is the one in "class with wrong casing (2 levels namespace)" case.

Commits
-------

5d1856f PsrAutoloadingFixer - do not remove directory structure from the Class name
  • Loading branch information
keradus committed Dec 24, 2020
2 parents 9d14829 + 5d1856f commit 5d4d1ec
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 27 deletions.
73 changes: 68 additions & 5 deletions src/Fixer/Basic/PsrAutoloadingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
return;
}

if (null === $namespace && null !== $this->configuration['dir']) {
$expectedClassyName = substr(str_replace('/', '_', $file->getRealPath()), -\strlen($classyName) - 4, -4);
} else {
$expectedClassyName = $file->getBasename('.php');
}
$expectedClassyName = $this->calculateClassyName($file, $namespace, $classyName);

if ($classyName !== $expectedClassyName) {
$tokens[$classyIndex] = new Token([T_STRING, $expectedClassyName]);
Expand Down Expand Up @@ -210,4 +206,71 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
$tokens->insertAt($namespaceStartIndex, $newNamespace);
}
}

/**
* @param null|string $namespace
* @param string $currentName
*
* @return string
*/
private function calculateClassyName(\SplFileInfo $file, $namespace, $currentName)
{
$name = $file->getBasename('.php');

$maxNamespace = $this->calculateMaxNamespace($file, $namespace);

if (null !== $this->configuration['dir']) {
return ('' !== $maxNamespace ? (str_replace('\\', '_', $maxNamespace).'_') : '').$name;
}

$namespaceParts = array_reverse(explode('\\', $maxNamespace));

foreach ($namespaceParts as $namespacePart) {
$nameCandidate = sprintf('%s_%s', $namespacePart, $name);
if (strtolower($nameCandidate) !== strtolower(substr($currentName, -\strlen($nameCandidate)))) {
break;
}
$name = $nameCandidate;
}

return $name;
}

/**
* @param null|string $namespace
*
* @return string
*/
private function calculateMaxNamespace(\SplFileInfo $file, $namespace)
{
if (null === $this->configuration['dir']) {
$root = \dirname($file->getRealPath());
while ($root !== \dirname($root)) {
$root = \dirname($root);
}
} else {
$root = realpath($this->configuration['dir']);
}

$namespaceAccordingToFileLocation = trim(str_replace(\DIRECTORY_SEPARATOR, '\\', substr(\dirname($file->getRealPath()), \strlen($root))), '\\');

if (null === $namespace) {
return $namespaceAccordingToFileLocation;
}

$namespaceAccordingToFileLocationPartsReversed = array_reverse(explode('\\', $namespaceAccordingToFileLocation));
$namespacePartsReversed = array_reverse(explode('\\', $namespace));

foreach ($namespacePartsReversed as $key => $namespaceParte) {
if (!isset($namespaceAccordingToFileLocationPartsReversed[$key])) {
break;
}
if (strtolower($namespaceParte) !== strtolower($namespaceAccordingToFileLocationPartsReversed[$key])) {
break;
}
unset($namespaceAccordingToFileLocationPartsReversed[$key]);
}

return implode('\\', array_reverse($namespaceAccordingToFileLocationPartsReversed));
}
}
2 changes: 1 addition & 1 deletion tests/Fixer/Basic/Psr0FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testFixCase()
$fileProphecy->willExtend(\SplFileInfo::class);
$fileProphecy->getBasename('.php')->willReturn('Bar');
$fileProphecy->getExtension()->willReturn('php');
$fileProphecy->getRealPath()->willReturn(__DIR__.'/Psr0/Foo/Bar.php');
$fileProphecy->getRealPath()->willReturn(__DIR__.\DIRECTORY_SEPARATOR.'Psr0'.\DIRECTORY_SEPARATOR.'Foo'.\DIRECTORY_SEPARATOR.'Bar.php');
$file = $fileProphecy->reveal();

$expected = <<<'EOF'
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixer/Basic/Psr4FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testFixCase()
$fileProphecy->willExtend(\SplFileInfo::class);
$fileProphecy->getBasename('.php')->willReturn('Bar');
$fileProphecy->getExtension()->willReturn('php');
$fileProphecy->getRealPath()->willReturn(__DIR__.'/Psr4/Foo/Bar.php');
$fileProphecy->getRealPath()->willReturn(__DIR__.\DIRECTORY_SEPARATOR.'Psr4'.\DIRECTORY_SEPARATOR.'Foo'.\DIRECTORY_SEPARATOR.'Bar.php');
$file = $fileProphecy->reveal();

$expected = <<<'EOF'
Expand All @@ -47,7 +47,7 @@ class bar {}

$expected = <<<'EOF'
<?php
class Bar {}
class Psr4_Foo_Bar {}
EOF;
$input = <<<'EOF'
<?php
Expand Down
Loading

0 comments on commit 5d4d1ec

Please sign in to comment.