Browse Source

bug #5421 PsrAutoloadingFixer - Fix PSR autoloading outside configured directory (kelunik, keradus)

This PR was merged into the 2.18 branch.

Discussion
----------

PsrAutoloadingFixer - Fix PSR autoloading outside configured directory

Fixes https://github.com/amphp/php-cs-fixer-config/issues/5.

Commits
-------

5d2d99da4 PsrAutoloadingFixer - call realpath only once
55a999187 PsrAutoloadingFixer - Fix PSR autoloading outside configured directory
Dariusz Ruminski 4 years ago
parent
commit
06f764e3cb

+ 16 - 0
src/Fixer/Basic/PsrAutoloadingFixer.php

@@ -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}
      */
@@ -137,6 +149,10 @@ class InvalidName {}
      */
     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;

+ 6 - 0
tests/Fixer/Basic/PsrAutoloadingFixerTest.php

@@ -103,6 +103,12 @@ final class PsrAutoloadingFixerTest extends AbstractFixerTestCase
             __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 {}',
         ];