Skip to content

Commit

Permalink
bug #5421 PsrAutoloadingFixer - Fix PSR autoloading outside configure…
Browse files Browse the repository at this point in the history
…d directory (kelunik, keradus)

This PR was merged into the 2.18 branch.

Discussion
----------

PsrAutoloadingFixer - Fix PSR autoloading outside configured directory

Fixes amphp/php-cs-fixer-config#5.

Commits
-------

5d2d99d PsrAutoloadingFixer - call realpath only once
55a9991 PsrAutoloadingFixer - Fix PSR autoloading outside configured directory
  • Loading branch information
keradus committed Mar 20, 2021
2 parents d3aaf70 + 5d2d99d commit 06f764e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Fixer/Basic/PsrAutoloadingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ class InvalidName {}
);
}

/**
* {@inheritdoc}
*/
public function configure(array $configuration = null)
{
parent::configure($configuration);

if (null !== $this->configuration['dir']) {
$this->configuration['dir'] = realpath($this->configuration['dir']);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -137,6 +149,10 @@ protected function createConfigurationDefinition()
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
if (null !== $this->configuration['dir'] && 0 !== strpos($file->getRealPath(), $this->configuration['dir'])) {
return;
}

$namespace = null;
$namespaceStartIndex = null;
$namespaceEndIndex = null;
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixer/Basic/PsrAutoloadingFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public static function provideFixNewCases()
__DIR__.'/../..',
];

yield 'configured directory (other directory)' => [
'<?php namespace Basic; class Foobar {}',
null,
__DIR__.'/../../Test',
];

yield 'multiple classy elements in file' => [
'<?php interface Foo {} class Bar {}',
];
Expand Down

0 comments on commit 06f764e

Please sign in to comment.