Skip to content

Commit

Permalink
add cache for PatternRefactorings.php
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Jul 25, 2024
1 parent bfa8cee commit 6cd3ace
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 101 deletions.
1 change: 1 addition & 0 deletions .github/workflows/imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'True' # Skip if cache hit
run: composer install --prefer-dist --no-progress

- name: Check Imports
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/EnforceHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private function getPatterns(): array

return [
'full_facade_paths' => [
'cache_key' => 'full_facade_paths-v1',
'search' => '<class_ref>::',
'replace' => '<1>()->',
'filters' => [
Expand All @@ -56,6 +57,7 @@ private function getPatterns(): array
'mutator' => $mutator,
],
'facade_aliases' => [
'cache_key' => 'facade_aliases-v1',
'search' => '<class_ref>::',
'replace' => '<1>()->',
'filters' => [
Expand Down
1 change: 1 addition & 0 deletions src/Commands/EnforceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private function getPatterns()
{
return [
'enforce_query' => [
'cache_key' => 'enforce_query-v1',
'search' => '<class_ref>::<name>',
'replace' => '<1>::query()-><2>',
'filters' => [
Expand Down
33 changes: 26 additions & 7 deletions src/Commands/PatternApply.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Imanghafoori\LaravelMicroscope\Commands;

use Generator;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters;
use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses;
use Imanghafoori\LaravelMicroscope\Iterators\BladeFiles;
use Imanghafoori\LaravelMicroscope\Iterators\BladeFiles\CheckBladePaths;
use Imanghafoori\LaravelMicroscope\Iterators\ClassMapIterator;
use Imanghafoori\LaravelMicroscope\SearchReplace\CachedFiles;
use Imanghafoori\LaravelMicroscope\SearchReplace\PatternRefactorings;
use Imanghafoori\SearchReplace\PatternParser;

Expand All @@ -27,16 +29,19 @@ private function patternCommand(ErrorPrinter $errorPrinter): int
};

$patterns = $this->getPatterns();
$this->appliesPatterns($patterns, $fileName, $folder);

$report = $this->appliesPatterns($patterns, $fileName, $folder);

$this->getOutput()->writeln($report);

$this->finishCommand($errorPrinter);

$errorPrinter->printTime();

return $errorPrinter->hasErrors() ? 1 : 0;
return $this->hasFoundPatterns() ? 1 : 0;
}

private function appliesPatterns(array $patterns, string $fileName, string $folder): void
private function appliesPatterns(array $patterns, string $fileName, string $folder)
{
$parsedPatterns = PatternParser::parsePatterns($patterns);

Expand All @@ -51,9 +56,23 @@ private function appliesPatterns(array $patterns, string $fileName, string $fold
CheckBladePaths::$readOnly = false;
$bladeStats = BladeFiles::check($check, [$parsedPatterns], $fileName, $folder);

$this->getOutput()->writeln(implode(PHP_EOL, [
Reporters\Psr4Report::printAutoload($psr4Stats, $classMapStats),
Reporters\BladeReport::getBladeStats($bladeStats),
]));
return self::getFinalMessage($psr4Stats, $classMapStats, $bladeStats);
}

private static function getFinalMessage(array $psr4Stats, array $classMapStats, Generator $bladeStats): string
{
try {
return implode(PHP_EOL, [
Reporters\Psr4Report::printAutoload($psr4Stats, $classMapStats),
Reporters\BladeReport::getBladeStats($bladeStats),
]);
} finally {
CachedFiles::writeCacheFiles();
}
}

private function hasFoundPatterns(): bool
{
return PatternRefactorings::$patternFound;
}
}
4 changes: 2 additions & 2 deletions src/Features/ActionComments/CheckActionComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function handle(ErrorPrinter $errorPrinter)

ActionsComments::$controllers = self::findDefinedRouteActions();

$results = ForPsr4LoadedClasses::check([ActionsComments::class], [], ltrim($this->option('file'), '='), ltrim($this->option('folder'), '='));
iterator_to_array($results);
$psr4Stats = ForPsr4LoadedClasses::check([ActionsComments::class], [], ltrim($this->option('file'), '='), ltrim($this->option('folder'), '='));
iterator_to_array($psr4Stats);

return 0;
}
Expand Down
1 change: 0 additions & 1 deletion src/FileReaders/FilePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public static function getFolderFile($absFilePath): array
return [$fileName, implode('/', $segments)];
}


#[Pure]
public static function contains($filePath, $folder, $file)
{
Expand Down
5 changes: 4 additions & 1 deletion src/ForPsr4LoadedClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Imanghafoori\LaravelMicroscope;

use Imanghafoori\LaravelMicroscope\Handlers\ErrorExceptionHandler;
use Imanghafoori\LaravelMicroscope\Iterators\CheckSingleMapping;
use Imanghafoori\LaravelMicroscope\Iterators\ChecksOnPsr4Classes;

class ForPsr4LoadedClasses
Expand All @@ -18,7 +19,9 @@ public static function check($checks, $params = [], $includeFile = '', $includeF
{
ChecksOnPsr4Classes::$errorExceptionHandler = ErrorExceptionHandler::class;

return ChecksOnPsr4Classes::apply($checks, $params, $includeFile, $includeFolder);
$checker = CheckSingleMapping::init($checks, $params, $includeFile, $includeFolder);

return ChecksOnPsr4Classes::apply($checker);
}

public static function checkNow($checks, $params = [], $includeFile = '', $includeFolder = '', $callback = null)
Expand Down
10 changes: 10 additions & 0 deletions src/Foundations/PhpFileDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static function make($absolutePath)
return $obj;
}

public function getMd5()
{
return md5_file($this->path->getWithUnixDirectorySeprator());
}

public function setTokenizer($tokenizer)
{
$this->tokenizer = $tokenizer;
Expand Down Expand Up @@ -134,4 +139,9 @@ public function insertNewLine($newLine, $atLine)
{
return FileManipulator::insertNewLine((string) $this->path, $newLine, $atLine);
}

public function getFileName()
{
return basename($this->path->__toString());
}
}
95 changes: 95 additions & 0 deletions src/Iterators/CheckSingleMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Iterators;

use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
use Throwable;

class CheckSingleMapping
{
use FiltersFiles;

public $includeFolder;

public $params;

/**
* @var array<class-string<\Imanghafoori\LaravelMicroscope\Iterators\Check>>
*/
public $checks;

private $namespace;

private $path;

/**
* @var \Throwable[]
*/
public $exceptions = [];

public static function init($checks, $params, $includeFile, $includeFolder): CheckSingleMapping
{
$includeFile && PhpFinder::$fileName = $includeFile;

$obj = new self;
$obj->checks = $checks;
$obj->params = $params;
$obj->includeFolder = $includeFolder;

return $obj;
}

/**
* @param string $psr4Namespace
* @param string $psr4Path
* @return int
*/
public function applyChecksInPath($psr4Namespace, $psr4Path): int
{
$this->namespace = $psr4Namespace;
$this->path = $psr4Path;

$finder = PhpFinder::getAllPhpFiles($psr4Path);
$this->includeFolder && $finder = self::filterFiles($finder, $this->includeFolder);

$filesCount = 0;
foreach ($finder as $phpFilePath) {
$filesCount++;
$this->applyChecks($phpFilePath);
}

return $filesCount;
}

private function applyChecks($phpFileObj)
{
$absFilePath = $phpFileObj->getRealPath();

$file = PhpFileDescriptor::make($absFilePath);

$processedParams = $this->getParams($file);

foreach ($this->checks as $check) {
try {
/**
* @var $check \Imanghafoori\LaravelMicroscope\Iterators\Check
*/
$newTokens = $check::check($file, $processedParams, $this->path, $this->namespace);
if ($newTokens) {
$file->setTokens($newTokens);
$processedParams = $this->getParams($file);
}
} catch (Throwable $exception) {
$this->exceptions[] = $exception;
}
}
}

private function getParams(PhpFileDescriptor $file)
{
$params = $this->params;

return (! is_array($params) && is_callable($params)) ? $params($file, $this->path, $this->namespace) : $params;
}
}
Loading

0 comments on commit 6cd3ace

Please sign in to comment.