Skip to content

Commit

Permalink
chore: extract progress logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nerg4l committed Jan 22, 2024
1 parent 819f61b commit 98e823c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 74 deletions.
87 changes: 13 additions & 74 deletions src/Console/Commands/FindMissingTranslationStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

namespace CodingSocks\LostInTranslation\Console\Commands;

use Closure;
use CodingSocks\LostInTranslation\LostInTranslation;
use CodingSocks\LostInTranslation\NonStringArgumentException;
use CodingSocks\LostInTranslation\MissingTranslationFileVisitor;
use Countable;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\SplFileInfo;

class FindMissingTranslationStrings extends Command
{
Expand All @@ -34,13 +31,6 @@ class FindMissingTranslationStrings extends Command
*/
protected $description = 'Find missing translation strings in your Laravel blade files';

/**
* Buffer for invalid arguments.
*
* @var string[]
*/
protected $invalidArguments;

/**
* Execute the console command.
*/
Expand All @@ -64,15 +54,17 @@ public function handle(LostInTranslation $lit)
return Str::endsWith($file->getExtension(), 'php');
});

$missing = $this->trackProgress($files, $this->makeMissingTranslationFinderCallback($lit, $locale));
$visitor = new MissingTranslationFileVisitor($locale, $lit);

$missing = array_unique(array_merge(...$missing));
$this->trackProgress($files, $visitor);

$missing = array_unique($visitor->getTranslations());

if ($this->output->getVerbosity() >= $this->parseVerbosity(OutputInterface::VERBOSITY_VERBOSE)) {
$errOutput = $this->output->getErrorStyle();

foreach ($this->invalidArguments as $argument) {
$errOutput->writeln("skipping dynamic language key: `{$argument}`");
foreach ($visitor->getErrors() as $error) {
$errOutput->writeln($error);
}
}

Expand All @@ -89,83 +81,30 @@ public function handle(LostInTranslation $lit)
* Execute a given callback while advancing a progress bar.
*
* @param \Countable|array $totalSteps
* @param \Closure $callback
* @return array
* @param callable $callback
* @return void
*/
protected function trackProgress(Countable|array $totalSteps, Closure $callback)
protected function trackProgress(Countable|array $totalSteps, callable $callback)
{
$items = [];

if ($this->option('no-progress')) {
foreach ($totalSteps as $value) {
if (($item = $callback($value)) && $item !== null) {
$items[] = $item;
}
$callback($value);
}

return $items;
return;
}

$bar = $this->output->createProgressBar(count($totalSteps));

$bar->start();

foreach ($totalSteps as $value) {
if (($item = $callback($value)) && $item !== null) {
$items[] = $item;
}
$callback($value);

$bar->advance();
}

$bar->finish();
$bar->clear();

return $items;
}

/**
* @param \CodingSocks\LostInTranslation\LostInTranslation $lit
* @param array $nodes
* @return array
*/
protected function resolveFirstArgs(LostInTranslation $lit, array $nodes): array
{
$translationKeys = [];
foreach ($nodes as $node) {
try {
if (($key = $lit->resolveFirstArg($node)) !== null) {
$translationKeys[] = $key;
}
} catch (NonStringArgumentException $e) {
$this->invalidArguments[] = $e->argument;
}
}
return array_unique($translationKeys);
}

/**
* @param \CodingSocks\LostInTranslation\LostInTranslation $lit
* @param string|null $locale
* @return \Closure
*/
protected function makeMissingTranslationFinderCallback(LostInTranslation $lit, string|null $locale): Closure
{
return function (SplFileInfo $file) use ($lit, $locale) {
$nodes = $lit->findInFile($file);

$translationKeys = $this->resolveFirstArgs($lit, $nodes);

$missing = [];

foreach ($translationKeys as $key) {
if (!Lang::has($key, $locale)) {
// TODO: find a better way to check uniqueness
$missing[] = $key;
}
}

return $missing;
};
}
}
75 changes: 75 additions & 0 deletions src/MissingTranslationFileVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace CodingSocks\LostInTranslation;

use Illuminate\Support\Facades\Lang;
use Symfony\Component\Finder\SplFileInfo;

class MissingTranslationFileVisitor
{
/** @var string[] Buffer for valid arguments. */
protected $translations = [];

/** @var string[] Buffer for invalid arguments. */
protected $errors = [];

/** @var string */
protected $locale;

/** @var \CodingSocks\LostInTranslation\LostInTranslation */
protected $lit;

/**
* @param $locale
* @param $lit
*/
public function __construct($locale, $lit)
{
$this->locale = $locale;
$this->lit = $lit;
}


public function __invoke(SplFileInfo $file)
{
$nodes = $this->lit->findInFile($file);

$translationKeys = $this->resolveFirstArgs($nodes);

foreach ($translationKeys as $key) {
if (!Lang::has($key, $this->locale)) {
// TODO: find a better way to check uniqueness
$this->translations[] = $key;
}
}
}

/**
* @param array $nodes
* @return array
*/
protected function resolveFirstArgs(array $nodes): array
{
$translationKeys = [];
foreach ($nodes as $node) {
try {
if (($key = $this->lit->resolveFirstArg($node)) !== null) {
$translationKeys[] = $key;
}
} catch (NonStringArgumentException $e) {
$this->errors[] = "skipping dynamic language key: `{$e->argument}`";
}
}
return array_unique($translationKeys);
}

public function getTranslations(): array
{
return $this->translations;
}

public function getErrors(): array
{
return $this->errors;
}
}

0 comments on commit 98e823c

Please sign in to comment.