Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed May 27, 2021
1 parent 2934960 commit d5a6b27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Analyzers/ComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class ComposerJson

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

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

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

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

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

// add the root composer.json
Expand Down
18 changes: 18 additions & 0 deletions src/Analyzers/NamespaceCorrector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,22 @@ public static function getRelativePathFromNamespace($namespace)

return \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, \str_replace($namespaces, $paths, $namespace));
}

public static function getNamespaceFromRelativePath($relPath)
{
// Remove .php from class path
$relPath = str_replace([base_path(), '.php'], '', $relPath);

$autoload = ComposerJson::readAutoload();
uksort($autoload, function ($a, $b) {
return strlen($b) <=> strlen($a);
});

$namespaces = array_keys($autoload);
$paths = array_values($autoload);

$relPath = \str_replace('\\', '/', $relPath);

return trim(\str_replace('/', '\\', \str_replace($paths, $namespaces, $relPath)), '\\');
}
}

0 comments on commit d5a6b27

Please sign in to comment.