Skip to content

Commit

Permalink
Update CheckClasses.php
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 authored Apr 22, 2021
1 parent c8c3bed commit 9e431a2
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/CheckClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function check($tokens, $absFilePath)
try {
self::checkImports($tokens, $absFilePath);
} catch (\ErrorException $e) {
// In case a file is moved or deleted...
// In case a file is moved or deleted,
// composer will need a dump autoload.
if (! Str::endsWith($e->getFile(), 'vendor\composer\ClassLoader.php')) {
throw $e;
Expand Down Expand Up @@ -104,7 +104,7 @@ private static function checkImportedClassesExist($imports, $absFilePath)
$isInUserSpace = Str::startsWith($import[0], array_keys(ComposerJson::readAutoload()));
$result = ReplaceLine::fixReference($absFilePath, $import[0], $import[1]);
if ($isInUserSpace && $result[0]) {
self::printFixation($absFilePath, $import[0], $import[1]);
self::printFixation($absFilePath, $import[0], $import[1], $result[1]);
} else {
app(ErrorPrinter::class)->wrongImport($absFilePath, $import[0], $import[1]);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public static function checkAtSignStrings($tokens, $absFilePath, $onlyAbsClassPa
$isInUserSpace = Str::startsWith($class, \array_keys(ComposerJson::readAutoload()));
$result = ReplaceLine::fixReference($absFilePath, $class, $token[2]);
if ($isInUserSpace && $result[0]) {
self::printFixation($absFilePath, $class, $token[2]);
self::printFixation($absFilePath, $class, $token[2], $result[1]);
} else {
app(ErrorPrinter::class)->wrongUsedClassError($absFilePath, $token[1], $token[2]);
}
Expand All @@ -173,37 +173,40 @@ private static function checkNotImportedClasses($tokens, $absFilePath)
$nonImportedClasses = ParseUseStatement::findClassReferences($tokens, $absFilePath);

foreach ($nonImportedClasses as $nonImportedClass) {
$cls = \trim($nonImportedClass['class'], '\\');
if (! self::isAbsent($cls) || \function_exists($cls)) {
$class = \trim($nonImportedClass['class'], '\\');
if (! self::isAbsent($class) || \function_exists($class)) {
continue;
}

$isInUserSpace = Str::startsWith($cls, \array_keys(ComposerJson::readAutoload()));
$isInUserSpace = Str::startsWith($class, \array_keys(ComposerJson::readAutoload()));
$line = $nonImportedClass['line'];
$result = ReplaceLine::fixReference($absFilePath, $cls, $line);
$result = ReplaceLine::fixReference($absFilePath, $class, $line);
if (! $result[0]) {
$cls1 = \str_replace($nonImportedClass['namespace'].'\\', '', $cls);
$cls1 = \str_replace($nonImportedClass['namespace'].'\\', '', $class);
$result = ReplaceLine::fixReference($absFilePath, $cls1, $line, '\\');
}

if ($isInUserSpace && $result[0]) {
self::printFixation($absFilePath, $cls, $line);
self::printFixation($absFilePath, $class, $line, $result[1]);
} else {
$fixes = self::possibleFixMsg($result[1]);
app(ErrorPrinter::class)->simplePendError(
$absFilePath,
$line,
$cls.' <=== \(-_-)/ '.$fixes,
$class.' <=== \(-_-)/ '.$fixes,
'wrongUsedClassError',
'Class Does not exist:'
);
}
}
}

private static function printFixation($absPath, $class, $line)
private static function printFixation($absPath, $wrongClass, $lineNumber, $correct)
{
app(ErrorPrinter::class)->simplePendError($absPath, $line, $class, 'ns_replacement', 'Namespace auto-corrected');
$line1 = $wrongClass . ' <=== Did not exist.';
$line2 = 'Auto-corrected to: '. substr($correct[0], 0, 55);

app(ErrorPrinter::class)->simplePendError($absPath, $lineNumber, $line2, 'ns_replacement', $line1);
}

private static function possibleFixMsg($pieces)
Expand Down

0 comments on commit 9e431a2

Please sign in to comment.