Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed May 20, 2024
1 parent a1b5bc1 commit 6a7fb4c
Show file tree
Hide file tree
Showing 53 changed files with 705 additions and 404 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^7.2|8.0.*|8.1.*|8.2.*|8.3.*",
"imanghafoori/composer-json": "^1.0.14",
"imanghafoori/composer-json": "^1.0.15",
"composer/class-map-generator": "^1.0.0",
"imanghafoori/php-abstract-filesystem": "^0.1.5",
"imanghafoori/php-search-replace": "^1.1.12",
Expand Down
58 changes: 22 additions & 36 deletions src/Analyzers/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use ImanGhafoori\ComposerJson\NamespaceCalculator;
use Imanghafoori\Filesystem\FileManipulator;
use Imanghafoori\Filesystem\Filesystem;
use Imanghafoori\LaravelMicroscope\ClassListProvider;
use Imanghafoori\SearchReplace\Searcher;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Imanghafoori\TokenAnalyzer\ParseUseStatement;

class Fixer
Expand Down Expand Up @@ -62,10 +61,11 @@ public static function fixReference($absPath, $inlinedClassRef, $lineNum)
}
$fullClassPath = $correct[0];

$contextClassNamespace = ComposerJson::make()->getNamespacedClassFromPath($absPath);
$file = PhpFileDescriptor::make($absPath);
$contextClassNamespace = $file->getNamespace();

if (NamespaceCalculator::haveSameNamespace($contextClassNamespace, $fullClassPath)) {
return [self::doReplacement($absPath, $inlinedClassRef, class_basename($fullClassPath), $lineNum), $correct];
return [self::doReplacement($file, $inlinedClassRef, class_basename($fullClassPath), $lineNum), $correct];
}

$uses = ParseUseStatement::parseUseStatements(token_get_all(file_get_contents($absPath)))[1];
Expand All @@ -80,11 +80,11 @@ public static function fixReference($absPath, $inlinedClassRef, $lineNum)
$fullClassPath = $classBaseName;
}

return [self::doReplacement($absPath, $inlinedClassRef, $fullClassPath, $lineNum), $correct];
return [self::doReplacement($file, $inlinedClassRef, $fullClassPath, $lineNum), $correct];
}

// Replace in the class reference
self::doReplacement($absPath, $inlinedClassRef, $classBaseName, $lineNum);
self::doReplacement($file, $inlinedClassRef, $classBaseName, $lineNum);

// Insert a new import at the top
$lineNum = array_values($uses)[0][1]; // first use statement
Expand All @@ -105,49 +105,35 @@ public static function fixImport($absPath, $import, $lineNum, $isAliased)
{
$correct = self::guessCorrect(class_basename($import));

if (\count($correct) !== 1) {
if (count($correct) !== 1) {
return [false, $correct];
}

$tokens = token_get_all(file_get_contents($absPath));
$hostNamespacedClass = ComposerJson::make()->getNamespacedClassFromPath($absPath);
$file = PhpFileDescriptor::make($absPath);
$hostNamespacedClass = $file->getNamespace();
// We just remove the wrong import if it is not needed.
if (! $isAliased && NamespaceCalculator::haveSameNamespace($hostNamespacedClass, $correct[0])) {
return [self::replaceSave("use $import;", '', $tokens, $absPath), [' Deleted!']];
}

return [self::replaceSave("use $import;", 'use '.$correct[0].';'.PHP_EOL, $tokens, $absPath), $correct];
}
$lines = $file->searchReplacePatterns("use $import;", '');

private static function replaceSave($old, $new, array $tokens, $absPath)
{
[$newVersion, $lines] = Searcher::searchReplace([
'fix' => [
'search' => $old,
'replace' => $new,
],
], $tokens);
return [$lines, [' Deleted!']];
}

Filesystem::$fileSystem::file_put_contents($absPath, $newVersion);
$lines = $file->searchReplacePatterns("use $import;", 'use '.$correct[0].';'.PHP_EOL);

return $lines;
return [$lines, $correct];
}

private static function doReplacement($absPath, $wrongRef, $correctRef, $lineNum)
private static function doReplacement(PhpFileDescriptor $file, $wrongRef, $correctRef, $lineNum)
{
if (version_compare(PHP_VERSION, '8.0.0') === 1) {
return FileManipulator::replaceFirst($absPath, $wrongRef, $correctRef, $lineNum);
if (self::phpVersionIsMoreOrEqualTo('8.0.0')) {
return $file->replaceAtLine($wrongRef, $correctRef, $lineNum);
}

$tokens = token_get_all(file_get_contents($absPath));
[$newVersion, $lines] = Searcher::searchReplace([
'fix' => [
'search' => $wrongRef,
'replace' => $correctRef,
],
], $tokens);
Filesystem::$fileSystem::file_put_contents($absPath, $newVersion);
return (bool) $file->searchReplacePatterns($wrongRef, $correctRef);
}

return (bool) $lines;
private static function phpVersionIsMoreOrEqualTo($version): bool
{
return version_compare(PHP_VERSION, $version) !== -1;
}
}
6 changes: 5 additions & 1 deletion src/Checks/CheckEarlyReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use Imanghafoori\LaravelMicroscope\Check;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Imanghafoori\TokenAnalyzer\Refactor;

class CheckEarlyReturn implements Check
{
public static function check(array $tokens, $absFilePath, $params)
public static function check(PhpFileDescriptor $file, $params)
{
$tokens = $file->getTokens();
$absFilePath = $file->getAbsolutePath();

$nofix = $params['nofix'];
$nofixCallback = $params['nofixCallback'];
$fixCallback = $params['fixCallback'];
Expand Down
6 changes: 5 additions & 1 deletion src/Checks/CheckIsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use Illuminate\Support\Facades\DB;
use Imanghafoori\LaravelMicroscope\Check;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Imanghafoori\TokenAnalyzer\ParseUseStatement;

class CheckIsQuery implements Check
{
public static function check($tokens, $absPath)
public static function check(PhpFileDescriptor $file)
{
$tokens = $file->getTokens();
$absPath = $file->getAbsolutePath();

[$classes] = ParseUseStatement::findClassReferences($tokens);

foreach ($classes as $class) {
Expand Down
6 changes: 5 additions & 1 deletion src/Checks/CheckRouteCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Imanghafoori\LaravelMicroscope\Check;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Imanghafoori\TokenAnalyzer\FunctionCall;

class CheckRouteCalls implements Check
Expand All @@ -12,11 +13,14 @@ class CheckRouteCalls implements Check

public static $skippedRouteCallsNum = 0;

public static function check($tokens, $absFilePath)
public static function check(PhpFileDescriptor $file)
{
// we skip the very first tokens: '<?php '
$i = 4;
// we skip the very end of the file.
$tokens = $file->getTokens();
$absFilePath = $file->getAbsolutePath();

$total = \count($tokens) - 3;
while ($i < $total) {
$index = FunctionCall::isGlobalCall('route', $tokens, $i);
Expand Down
7 changes: 5 additions & 2 deletions src/Checks/CheckRubySyntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
use Imanghafoori\LaravelMicroscope\Check;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Imanghafoori\TokenAnalyzer\Refactor;
use Imanghafoori\TokenAnalyzer\SyntaxNormalizer;

class CheckRubySyntax implements Check
{
public static function check($tokens, $absFilePath)
public static function check(PhpFileDescriptor $file)
{
if (empty($tokens) || $tokens[0][0] !== T_OPEN_TAG) {
return false;
}

$absFilePath = $file->getAbsolutePath();

try {
$tokens = SyntaxNormalizer::normalizeSyntax($tokens, true);
} catch (Exception $e) {
Expand All @@ -41,7 +44,7 @@ private static function getConfirm($filePath)
return ErrorPrinter::singleton()->printer->confirm('Replacing endif in: '.$filePath);
}

private static function requestIssue(string $path)
private static function requestIssue($path)
{
dump('(O_o) Well, It seems we had some problem parsing the contents of: (o_O)');
dump('Submit an issue on github: https://github.com/imanghafoori1/microscope');
Expand Down
97 changes: 0 additions & 97 deletions src/Checks/CheckStringy.php

This file was deleted.

12 changes: 8 additions & 4 deletions src/Checks/PSR12/CurlyBraces.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
namespace Imanghafoori\LaravelMicroscope\Checks\PSR12;

use Imanghafoori\LaravelMicroscope\Check;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Imanghafoori\TokenAnalyzer\Refactor;

class CurlyBraces implements Check
{
public static $command;

public static function check($tokens, $absFilePath)
public static function check(PhpFileDescriptor $file)
{
$tokens = $file->getTokens();
$absFilePath = $file->getAbsolutePath();

self::addPublicKeyword($tokens, $absFilePath);
}

Expand All @@ -37,20 +41,20 @@ private static function addPublicKeyword($tokens, $absolutePath)
}
}

private static function openCurly($token, $level, $tokens, $i, $classFilePath)
private static function openCurly($token, $level, $tokens, $i, $absFilePath)
{
if ($token == '{' && ! in_array($tokens[$i - 1][0], [T_DOUBLE_COLON, T_OBJECT_OPERATOR])) {
$sp = str_repeat(' ', $level);
if ($tokens[$i + 1][0] === T_WHITESPACE) {
if ($tokens[$i + 1][1] !== PHP_EOL.$sp && $tokens[$i + 1][1] !== "\n".$sp) {
$tokens[$i + 1][1] = PHP_EOL.$sp;
Refactor::saveTokens($classFilePath, $tokens);
Refactor::saveTokens($absFilePath, $tokens);
} else {
//
}
} else {
array_splice($tokens, $i + 1, 0, [[T_WHITESPACE, PHP_EOL.$sp]]);
Refactor::saveTokens($classFilePath, $tokens);
Refactor::saveTokens($absFilePath, $tokens);
}
}
}
Expand Down
33 changes: 0 additions & 33 deletions src/Commands/ClassifyStrings.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Commands/PatternApply.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters;
use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses;
use Imanghafoori\LaravelMicroscope\Iterators\BladeFiles;
use Imanghafoori\LaravelMicroscope\Iterators\ClassMapIterator;
use Imanghafoori\LaravelMicroscope\SearchReplace\PatternRefactorings;
use Imanghafoori\SearchReplace\PatternParser;
Expand Down
Loading

0 comments on commit 6a7fb4c

Please sign in to comment.