Skip to content

Commit

Permalink
style: apply wdes/coding-standard v3
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Mar 21, 2021
1 parent f9ff9d4 commit 0078b11
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 51 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"phpunit/phpunit": "^7 || ^8 || ^9",
"slevomat/coding-standard": "^6.0",
"phpstan/phpstan": "^0.12",
"wdes/coding-standard": "^2"
"wdes/coding-standard": "^3"
}
}
12 changes: 7 additions & 5 deletions example/simple.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

declare(strict_types = 1);

// Can be removed :)

require_once __DIR__ . '/../vendor/autoload.php';

use \Wdes\phpI18nL10n\plugins\MoReader;
use \Wdes\phpI18nL10n\Launcher;
use \Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use \Twig\Environment as TwigEnvironment;
use \Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;

$dataDir = __DIR__ . '/locale/';
$moReader = new MoReader(
Expand Down
24 changes: 15 additions & 9 deletions scripts/tools/generate-twig-cache.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env php
<?php
declare(strict_types = 1);

/**
* @license http://unlicense.org/UNLICENSE The UNLICENSE
* @author William Desportes <[email protected]>
*/

declare(strict_types = 1);

$options = getopt(
'', [
'',
[
'twig-cache-dir:',
'twig-templates-dir:',
'twig-templates-po-files:',
Expand Down Expand Up @@ -52,10 +55,10 @@

require_once(__DIR__ . '/../../vendor/autoload.php');

use \Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use \Twig\Cache\FilesystemCache;
use \Twig\Environment as TwigEnvironment;
use \Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Cache\FilesystemCache;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;

$shortTempDir = $options['twig-templates-po-files'];
$jsonMaping = $options['json-mapping'];
Expand All @@ -64,7 +67,8 @@
$loader = new TwigLoaderFilesystem([ $templateDir ]); // Load all templates from the dir
$cache = new FilesystemCache($options['twig-cache-dir']);
$twig = new TwigEnvironment(
$loader, [
$loader,
[
'cache' => $cache
]
);
Expand Down Expand Up @@ -107,10 +111,12 @@
$mappings->replacements[] = $templates;

// iterate over all templates
foreach (new RecursiveIteratorIterator(
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($templateDir),
RecursiveIteratorIterator::LEAVES_ONLY
) as $file) {
);

foreach ($files as $file) {
// force twig to generate cache
if ($file->isFile() && $file->getExtension() === 'twig') {
$shortName = str_replace($templateDir, '', $file);
Expand Down
9 changes: 6 additions & 3 deletions scripts/tools/update-po-files.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env php
<?php
declare(strict_types = 1);

/**
* @license http://unlicense.org/UNLICENSE The UNLICENSE
* @author William Desportes <[email protected]>
*/

declare(strict_types = 1);

$options = getopt(
'', [
'',
[
'po-dir:',
'po-template:',
'json-mapping::',
Expand Down Expand Up @@ -93,7 +96,7 @@ static function ($matchs) use ($mappings) {
echo 'PoDir: ' . $poDirectory . "\r\n";
foreach (glob($poDirectory . '*.po') as $file) {
exec('msgmerge --quiet --previous -U ' . $file . ' ' . $poTemplate);
echo 'File: '. $file . "\r\n";
echo 'File: ' . $file . "\r\n";
poupdate($file);
}
poupdate($poTemplate);
2 changes: 2 additions & 0 deletions src/Launcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/Extension/I18n.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Expand Down
14 changes: 9 additions & 5 deletions src/Twig/MemoryCache.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace Wdes\phpI18nL10n\Twig;

use \Twig\Cache\CacheInterface;
use \Twig\TemplateWrapper;
use Twig\Cache\CacheInterface;
use Twig\TemplateWrapper;

/**
* Token parser for Twig
Expand All @@ -30,7 +32,7 @@ class MemoryCache implements CacheInterface
*/
public function generateKey($name, $className): string
{
return 'memcache_'.$name;
return 'memcache_' . $name;
}

/**
Expand Down Expand Up @@ -75,7 +77,7 @@ public function getTimestamp($key): int
*/
public function getFromCache(TemplateWrapper $template): string
{
return $this->memory['memcache_'.$template->getTemplateName()];
return $this->memory['memcache_' . $template->getTemplateName()];
}

/**
Expand All @@ -89,7 +91,9 @@ public function extractDoDisplayFromCache(TemplateWrapper $template): string
$content = self::getFromCache($template);
// "/function ([a-z_]+)\("
preg_match_all(
'/protected function doDisplay\(([a-z\s,\$=\[\]]+)\)([\s]+){([=%\sa-z\/0-9-\>:\(\)\\\";.,\$\[\]?_-]+)/msi', $content, $output_array
'/protected function doDisplay\(([a-z\s,\$=\[\]]+)\)([\s]+){([=%\sa-z\/0-9-\>:\(\)\\\";.,\$\[\]?_-]+)/msi',
$content,
$output_array
);
return $output_array[3][0];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/TokenParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/TranslationNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/BasePlugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Expand Down
16 changes: 9 additions & 7 deletions src/plugins/MoReader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types = 1);

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace Wdes\phpI18nL10n\plugins;

use \Exception;
use \stdClass;
use Exception;
use stdClass;

/**
* Plugin for reading .mo files
Expand Down Expand Up @@ -42,7 +44,7 @@ class MoReader extends BasePlugin
public function __construct(array $options)
{
if (is_dir($options['localeDir']) == false) {
throw new Exception('The directory does not exist : '.$options['localeDir']);
throw new Exception('The directory does not exist : ' . $options['localeDir']);
}
}

Expand All @@ -67,14 +69,14 @@ public function readFile(string $file): stdClass
$this->data->endian = self::determineByteOrder($this->fileRes);
if ($this->data->endian === -1) {
fclose($this->fileRes);
throw new Exception($file.' is not a valid gettext file.');
throw new Exception($file . ' is not a valid gettext file.');
}

fseek($this->fileRes, 4);
$this->data->fileFormatRevision = self::readInteger($this->data->endian, $this->fileRes);
if ($this->data->fileFormatRevision !== 0 && $this->data->fileFormatRevision !== 1) {
fclose($this->fileRes);
throw new Exception($file.' has an unknown major revision.');
throw new Exception($file . ' has an unknown major revision.');
}
$this->data->nbrOfStrings = self::readInteger($this->data->endian, $this->fileRes);
$this->data->tableOffsetOriginal = self::readInteger($this->data->endian, $this->fileRes);//offset of table with original strings
Expand Down Expand Up @@ -121,12 +123,12 @@ public function readTranslations(): array
$msgStr = null;
try {
$msgId = $this->readStringFromTable($counter, $this->data->msgIdTable);
} catch(Exception $e){
} catch (Exception $e) {
$msgId = [''];
}
try {
$msgStr = $this->readStringFromTable($counter, $this->data->msgStrTable);
} catch(Exception $e){
} catch (Exception $e) {
$msgStr = [];
}
$this->processRecord($data, $msgId, $msgStr);
Expand Down
33 changes: 18 additions & 15 deletions tests/I18nTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

declare(strict_types = 1);

/* The contents of this file is free and unencumbered software released into the
* public domain.
* For more information, please refer to <http://unlicense.org/>
*/
namespace Wdes\phpI18nL10n\Tests;

use \PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestCase;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use \Twig\Environment as TwigEnv;
use \Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
use Twig\Environment as TwigEnv;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\MemoryCache;
Expand Down Expand Up @@ -41,7 +43,7 @@ class I18nTest extends TestCase
*/
public function setUp(): void
{
$dataDir = __DIR__. DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR;
$dataDir = __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR;

$moReader = new MoReader(
['localeDir' => $dataDir]
Expand All @@ -52,7 +54,8 @@ public function setUp(): void
$loader = new TwigLoaderFilesystem();
$this->memoryCache = new MemoryCache();
$this->twig = new TwigEnv(
$loader, [
$loader,
[
'cache' => $this->memoryCache,
'debug' => true
]
Expand Down Expand Up @@ -185,8 +188,8 @@ public function testPluralTranslation(): void
);
$generatedCode = $this->memoryCache->extractDoDisplayFromCache($template);
$this->assertStringContainsString(
'echo strtr(\Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext("One person"'.
', "%nbr% persons", abs( // line 1' . "\n" . '($context["nbr_persons"] ?? null))),'.
'echo strtr(\Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext("One person"' .
', "%nbr% persons", abs( // line 1' . "\n" . '($context["nbr_persons"] ?? null))),' .
' array("%nbr%" => ($context["nbr"] ?? null), ));',
$generatedCode
);
Expand All @@ -206,9 +209,9 @@ public function testPluralTranslationWithComment(): void
);
$generatedCode = $this->memoryCache->extractDoDisplayFromCache($template);
$this->assertStringContainsString(
'echo strtr(\Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext('.
'"one user likes this.", "%nbr% users likes this.",'.
' abs( // line 1' . "\n" . '($context["nbr_persons"] ?? null))),'.
'echo strtr(\Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext(' .
'"one user likes this.", "%nbr% users likes this.",' .
' abs( // line 1' . "\n" . '($context["nbr_persons"] ?? null))),' .
' array("%nbr%" => ($context["nbr"] ?? null), ));',
$generatedCode
);
Expand Down Expand Up @@ -253,7 +256,7 @@ public function testSimplePluralTranslationCount(): void
);
$generatedCode = $this->memoryCache->extractDoDisplayFromCache($template);
$this->assertStringContainsString(
'echo \Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext("One person",'.
'echo \Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext("One person",' .
' "persons", abs(twig_get_attribute($this->env, $this->source, // line 1' . "\n" .
'($context["a"] ?? null), "count", [], "any", false, false, false, 1)));',
$generatedCode
Expand All @@ -274,10 +277,10 @@ public function testSimplePluralTranslationCountAndVars(): void
);
$generatedCode = $this->memoryCache->extractDoDisplayFromCache($template);
$this->assertStringContainsString(
'echo strtr(\Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext("One person",'.
' "persons and %count% dogs", abs(twig_get_attribute($this->env,'.
' $this->source, // line 1' . "\n" . '($context["a"] ?? null), "count", [], "any", false, false, false, 1))),'.
' array("%count%" => abs(twig_get_attribute($this->env,'.
'echo strtr(\Wdes\phpI18nL10n\Launcher::getPlugin()->ngettext("One person",' .
' "persons and %count% dogs", abs(twig_get_attribute($this->env,' .
' $this->source, // line 1' . "\n" . '($context["a"] ?? null), "count", [], "any", false, false, false, 1))),' .
' array("%count%" => abs(twig_get_attribute($this->env,' .
' $this->source, ($context["a"] ?? null), "count", [], "any", false, false, false, 1)), ));',
$generatedCode
);
Expand Down
4 changes: 3 additions & 1 deletion tests/LauncherTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types = 1);

/* The contents of this file is free and unencumbered software released into the
* public domain.
* For more information, please refer to <http://unlicense.org/>
*/
namespace Wdes\phpI18nL10n\Tests;

use \PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestCase;
use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;

Expand Down
Loading

0 comments on commit 0078b11

Please sign in to comment.