Skip to content

Commit d5a6b27

Browse files
committed
bug fix
1 parent 2934960 commit d5a6b27

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Analyzers/ComposerJson.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class ComposerJson
1010

1111
public static function readKey($key, $composerPath = null)
1212
{
13-
$path = $composerPath ?: app()->basePath();
13+
$path = $composerPath ?: '';
1414

1515
if (isset(self::$result[$path][$key])) {
1616
return self::$result[$path][$key];
1717
}
1818

19-
$composer = \json_decode(\file_get_contents($path.DIRECTORY_SEPARATOR.'composer.json'), true);
19+
$composer = \json_decode(\file_get_contents(app()->basePath($path.'composer.json')), true);
2020

2121
$value = (array) data_get($composer, $key, []);
2222

@@ -45,7 +45,7 @@ public static function readAutoload()
4545
$res = [];
4646
foreach ($composers as $path) {
4747
// We avoid autoload-dev for repositories.
48-
$res = $res + self::readKey('autoload.psr-4', app()->basePath($path));
48+
$res = $res + self::readKey('autoload.psr-4', $path);
4949
}
5050

5151
// add the root composer.json

src/Analyzers/NamespaceCorrector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ public static function getRelativePathFromNamespace($namespace)
6666

6767
return \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, \str_replace($namespaces, $paths, $namespace));
6868
}
69+
70+
public static function getNamespaceFromRelativePath($relPath)
71+
{
72+
// Remove .php from class path
73+
$relPath = str_replace([base_path(), '.php'], '', $relPath);
74+
75+
$autoload = ComposerJson::readAutoload();
76+
uksort($autoload, function ($a, $b) {
77+
return strlen($b) <=> strlen($a);
78+
});
79+
80+
$namespaces = array_keys($autoload);
81+
$paths = array_values($autoload);
82+
83+
$relPath = \str_replace('\\', '/', $relPath);
84+
85+
return trim(\str_replace('/', '\\', \str_replace($paths, $namespaces, $relPath)), '\\');
86+
}
6987
}

0 commit comments

Comments
 (0)