Skip to content

Commit 17c7e92

Browse files
committed
Upgrade dependencies.
Prepared code for PHP8.
1 parent 5a0045f commit 17c7e92

20 files changed

+85
-76
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
.idea
3-
composer.lock
3+
composer.lock
4+
.phpunit.result.cache

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
"ext-json": "*",
88
"ext-zip": "*",
99
"ext-gettext": "*",
10-
"gettext/gettext": "^4.7.0",
11-
"league/flysystem": "^1.0"
10+
"gettext/gettext": "^4.7.0"
1211
},
1312
"require-dev": {
14-
"phpunit/phpunit": "^6.4",
13+
"phpunit/phpunit": "^9.0",
1514
"mockery/mockery": "^1.1",
1615
"mikey179/vfsstream": "^1.6"
1716
},

phpunit.xml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false"
12-
>
13-
<filter>
14-
<whitelist>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
3+
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
5+
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
6+
<coverage>
7+
<include>
158
<directory>./src/</directory>
16-
</whitelist>
17-
</filter>
18-
9+
</include>
10+
</coverage>
1911
<testsuites>
2012
<testsuite name="Printful GettextCms Test Suite">
2113
<directory suffix=".php">./tests/</directory>
2214
<exclude>./tests/assets</exclude>
2315
<exclude>./tests/Stubs</exclude>
2416
</testsuite>
2517
</testsuites>
26-
</phpunit>
18+
</phpunit>

src/MessageExtractor.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
namespace Printful\GettextCms;
44

5+
use FilesystemIterator;
56
use Gettext\Extractors\ExtractorInterface;
67
use Gettext\Extractors\ExtractorMultiInterface;
78
use Gettext\Extractors\JsCode;
89
use Gettext\Extractors\PhpCode;
910
use Gettext\Extractors\VueJs;
1011
use Gettext\Translations;
11-
use League\Flysystem\Adapter\Local;
12-
use League\Flysystem\Filesystem;
13-
use League\Flysystem\Plugin\ListFiles;
1412
use Printful\GettextCms\Exceptions\GettextCmsException;
1513
use Printful\GettextCms\Exceptions\InvalidPathException;
1614
use Printful\GettextCms\Exceptions\UnknownExtractorException;
1715
use Printful\GettextCms\Interfaces\MessageConfigInterface;
1816
use Printful\GettextCms\Structures\ScanItem;
17+
use RecursiveDirectoryIterator;
18+
use RecursiveIteratorIterator;
1919

2020
/**
2121
* Class extracts gettext function calls from source files and converts them to Translation objects
@@ -55,7 +55,7 @@ public function extract(array $items, array $domains = null): array
5555
}
5656

5757
/** @var Translations[] $allTranslations [domain => translations, ...] */
58-
$allTranslations = array_reduce($domains, function (&$carry, string $domain) use ($defaultDomain) {
58+
$allTranslations = array_reduce($domains, function ($carry, string $domain) use ($defaultDomain) {
5959
$translations = new Translations();
6060

6161
// When we scan for default domain, we have to specify an empty value
@@ -160,23 +160,32 @@ private function resolveDirectoryFiles(ScanItem $item): array
160160
{
161161
$dir = realpath($item->path);
162162

163-
$adapter = new Local($dir);
164-
$filesystem = new Filesystem($adapter);
165-
$filesystem->addPlugin(new ListFiles());
166-
167-
$files = $filesystem->listFiles('', $item->recursive);
163+
if ($item->recursive) {
164+
$iterator = new RecursiveIteratorIterator(
165+
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)
166+
);
167+
} else {
168+
$iterator = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
169+
}
168170

169171
// If no extensions are given, fallback to known extensions
170172
$extensions = $item->extensions ?: array_keys(self::EXTRACTORS);
171173

172-
// If extensions are set, filter other files out
173-
$files = array_filter($files, function ($file) use ($item, $extensions) {
174-
return isset($file['extension']) && in_array($file['extension'], $extensions);
175-
});
174+
$matchingPathnames = [];
175+
176+
foreach ($iterator as $file) {
177+
if ($file->isDir()) {
178+
continue;
179+
}
180+
181+
$extension = strtolower($file->getExtension());
182+
183+
if ($extensions && in_array($extension, $extensions)) {
184+
$matchingPathnames[] = $file->getRealPath();
185+
}
186+
}
176187

177-
return array_map(function ($file) use ($dir) {
178-
return $dir . DIRECTORY_SEPARATOR . $file['path'];
179-
}, $files);
188+
return $matchingPathnames;
180189
}
181190

182191
/**

tests/TestCase.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Printful\GettextCms\Tests;
44

5-
use League\Flysystem\Adapter\Local;
6-
use League\Flysystem\Filesystem;
5+
use FilesystemIterator;
76
use Mockery;
7+
use RecursiveDirectoryIterator;
8+
use RecursiveIteratorIterator;
89

910
abstract class TestCase extends \PHPUnit\Framework\TestCase
1011
{
@@ -15,22 +16,29 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
1516
*/
1617
protected function deleteDirectory($path)
1718
{
18-
$path = rtrim($path, '/');
19+
if (!is_dir($path)) {
20+
return;
21+
}
1922

20-
$childDirName = pathinfo($path, PATHINFO_BASENAME);
21-
$parentDir = dirname($path);
23+
$iterator = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
24+
$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);
25+
foreach ($files as $file) {
26+
if ($file->isDir()) {
27+
rmdir($file->getRealPath());
28+
} else {
29+
unlink($file->getRealPath());
30+
}
31+
}
2232

23-
$adapter = new Local($parentDir);
24-
$filesystem = new Filesystem($adapter);
25-
$filesystem->deleteDir($childDirName);
33+
rmdir($path);
2634
}
2735

2836
protected function getDummyFile(string $filename = 'dummy-file.php', string $directory = 'dummy-directory'): string
2937
{
3038
return __DIR__ . '/assets/' . $directory . '/' . $filename;
3139
}
3240

33-
protected function tearDown()
41+
protected function tearDown(): void
3442
{
3543
Mockery::close();
3644
parent::tearDown();

tests/TestCases/BuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BuilderTest extends TestCase
4545
/** @var string */
4646
private $domain;
4747

48-
protected function setUp()
48+
protected function setUp(): void
4949
{
5050
parent::setUp();
5151

@@ -158,7 +158,7 @@ public function testDefaultLocaleIsNotExported()
158158
$this->config->shouldReceive('getDefaultLocale')->andReturn('en_EN')->byDefault();
159159
$this->config->shouldReceive('getMoDirectory')->andReturn('whatever')->never();
160160

161-
// If there were an attempt to export, error would happend because mo directory does not exist
161+
// If there were an attempt to export, error would happen because mo directory does not exist
162162
self::assertTrue($this->exporter->export('en_EN', 'domain'), 'Default domain is not exported');
163163
}
164164

@@ -180,7 +180,7 @@ private function verifyTranslations(array $messages, string $locale, string $dom
180180
}
181181
}
182182

183-
private function getMoPathname($domain)
183+
private function getMoPathname($domain): string
184184
{
185185
return $this->locale . '/LC_MESSAGES/' . $domain . '.mo';
186186
}

tests/TestCases/DynamicImporterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DynamicImporterTest extends TestCase
2323
/** @var DynamicMessageImporter */
2424
private $importer;
2525

26-
protected function setUp()
26+
protected function setUp(): void
2727
{
2828
parent::setUp();
2929

tests/TestCases/FileResolvingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FileResolvingTest extends TestCase
1515
/** @var MessageExtractor */
1616
private $scanner;
1717

18-
protected function setUp()
18+
protected function setUp(): void
1919
{
2020
parent::setUp();
2121

tests/TestCases/ImporterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ImporterTest extends TestCase
2727
/** @var MessageExtractor */
2828
private $extractor;
2929

30-
protected function setUp()
30+
protected function setUp(): void
3131
{
3232
parent::setUp();
3333

tests/TestCases/JsBuilderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class JsBuilderTest extends TestCase
1818
/** @var MessageJsBuilder */
1919
private $builder;
2020

21-
protected function setUp()
21+
protected function setUp(): void
2222
{
2323
parent::setUp();
2424

@@ -39,17 +39,17 @@ public function testExportJsonp()
3939

4040
$js = $this->builder->exportJsonp($this->locale, $domains, 'loaded');
4141

42-
self::assertNotContains('non-js', $js, 'Non-js translation is not included');
42+
self::assertStringNotContainsString('non-js', $js, 'Non-js translation is not included');
4343

4444
foreach ($domains as $domain) {
4545
$ts = $this->storage->getEnabledTranslatedJs($this->locale, $domain);
4646

47-
self::assertTrue(count($ts) > 0, 'Domain ' . $domain . ' has translations');
47+
self::assertNotEmpty($ts, 'Domain ' . $domain . ' has translations');
4848

4949
foreach ($ts as $t) {
5050
/** @var Translation $t */
51-
self::assertContains($t->getOriginal(), $js, $t->getOriginal() . ' is within JS');
52-
self::assertContains($t->getTranslation(), $js, $t->getTranslation() . ' translation is within JS');
51+
self::assertStringContainsString($t->getOriginal(), $js, $t->getOriginal() . ' is within JS');
52+
self::assertStringContainsString($t->getTranslation(), $js, $t->getTranslation() . ' translation is within JS');
5353
}
5454
}
5555
}

0 commit comments

Comments
 (0)