Skip to content

Commit

Permalink
fix wrong extension usage for mjml scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Jan 26, 2024
1 parent 05c0af1 commit c403a3c
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ php vendor/bin/phpunuhi scan:usage --dir=./src --scanner=twig --configuration=./

# custom set
php vendor/bin/phpunuhi scan:usage --dir=./src --scanner=twig --set=storefront

# verbose mode to show more output, like scanned files and so on
php vendor/bin/phpunuhi scan:usage --dir=./src --scanner=twig --verbose
```

> Please note, this is only a helper command to find unused translations in template files.
Expand Down Expand Up @@ -985,7 +988,7 @@ Once set to **false**, the validator will automatically warn you, if you have a
times within a single locale.

You need to configure this rule per locale.
If you want to configure all locales (or all that are not explicitely configured), you can use the wildcard for the rest.
If you want to configure all locales (or all that are not explicitly configured), you can use the wildcard for the rest.

```xml

Expand Down
8 changes: 8 additions & 0 deletions src/Bundles/Scanner/MJML/MjmlScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public function getScannerName(): string
return 'mjml';
}

/**
* @return string
*/
public function getExtension(): string
{
return 'mjml';
}

/**
* {{ 'email.contact.subject' | trans | sw_sanitize }}
*
Expand Down
5 changes: 5 additions & 0 deletions src/Bundles/Scanner/ScannerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ interface ScannerInterface
*/
public function getScannerName(): string;

/**
* @return string
*/
public function getExtension(): string;

/**
* @param string $key
* @param string $content
Expand Down
8 changes: 8 additions & 0 deletions src/Bundles/Scanner/TWIG/TwigScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public function getScannerName(): string
return 'twig';
}

/**
* @return string
*/
public function getExtension(): string
{
return 'twig';
}

/**
* {{ 'header.example' | trans }}
*
Expand Down
33 changes: 21 additions & 12 deletions src/Commands/ScanUsageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ class ScanUsageCommand extends Command
/**
* @return void
*/
protected function configure()
protected function configure(): void
{
$this
->setName('scan:usage')
->setDescription('Scans the usage of translations in the provided files')
->addOption('configuration', null, InputOption::VALUE_REQUIRED, '', '')
->addOption('set', null, InputOption::VALUE_REQUIRED, '', '')
->addOption('dir', null, InputOption::VALUE_REQUIRED, '', '')
->addOption('scanner', null, InputOption::VALUE_REQUIRED, '', '');
$this->setName('scan:usage');
$this->setDescription('Scans the usage of translations in the provided files');
$this->addOption('configuration', null, InputOption::VALUE_REQUIRED, '', '');
$this->addOption('set', null, InputOption::VALUE_REQUIRED, '', '');
$this->addOption('dir', null, InputOption::VALUE_REQUIRED, '', '');
$this->addOption('scanner', null, InputOption::VALUE_REQUIRED, '', '');

parent::configure();
}
Expand All @@ -57,7 +56,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$scannerName = $this->getConfigStringValue('scanner', $input);
$directory = $this->getConfigStringValue('dir', $input);
$setName = $this->getConfigStringValue('set', $input);

$isVerboseMode = (bool)$input->getOption('verbose');

if ($scannerName === '' || $scannerName === '0') {
throw new Exception('No scanner provided');
Expand All @@ -79,16 +78,26 @@ public function execute(InputInterface $input, OutputInterface $output): int

$scanner = ScannerFactory::getInstance()->getScanner($scannerName);
$io->writeln('Using scanner: ' . $scanner->getScannerName());
$io->writeln('');

$scannedFiles = $directoryLoader->getFiles($directory, $scanner->getExtension());

$fileContents = [];

$scannedFiles = $directoryLoader->getFiles($directory, 'twig');
if ($isVerboseMode) {
$io->writeln('Found files:');
}

$fileContents = [];
foreach ($scannedFiles as $file) {
$fileContents[$file] = $fileReader->load($file);

if ($isVerboseMode) {
$io->writeln(' * ' . $file);
}
}

$io->writeln('');


if ($fileContents === []) {
$io->warning('No files found in directory: ' . $directory);
Expand Down Expand Up @@ -155,7 +164,7 @@ public function execute(InputInterface $input, OutputInterface $output): int

if ($errorCount > 0) {
$io->error('Found ' . $errorCount . ' translation keys that do not seem to be used in any of the scanned files');
$io->note('Please keep in mind, the keys have not been found in your scanned files, but might be used somewhere else, like in JS or PHP files. Do not delete them without further investigation!');
$io->note('Please keep in mind, the keys have not been found in your scanned files, but might be used somewhere else, like in JS, PHP, C# or other files. Do not delete them without further investigation!');
return Command::FAILURE;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/Bundles/Scanner/MJML/MjmlScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public function testGetScannerName(): void
$this->assertEquals('mjml', (new MjmlScanner())->getScannerName());
}

/**
* @return void
*/
public function testGetExtension(): void
{
$this->assertEquals('mjml', (new MjmlScanner())->getExtension());
}

/**
* @return array<mixed>
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/Bundles/Scanner/TWIG/TwigScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public function testGetScannerName(): void
$this->assertEquals('twig', (new TwigScanner())->getScannerName());
}

/**
* @return void
*/
public function testGetExtension(): void
{
$this->assertEquals('twig', (new TwigScanner())->getExtension());
}

/**
* @return array<mixed>
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/playground/json/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ migrate: ## Migrate command
cd ../../.. && php bin/phpunuhi migrate --configuration=./tests/playground/json/phpunuhi.xml --output=ini

scan: ## Scans all TWIG files
cd ../../.. && php bin/phpunuhi scan:usage --configuration=./tests/playground/json/phpunuhi.xml --dir=templates --scanner=twig
cd ../../.. && php bin/phpunuhi scan:usage --configuration=./tests/playground/json/phpunuhi.xml --dir=./tests/playground/json/templates --scanner=mjml --verbose
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/playground/json/templates/subfolder/test.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'card-section.titleTest' | trans }}

0 comments on commit c403a3c

Please sign in to comment.