Skip to content

Commit f3841ad

Browse files
Use cache version as string instead of number
This helps to prevent PHP from encoding the version as a high precision floating point number in the cache file, e.g. 1.399999999999 instead of 1.4.
1 parent 2e6ddc6 commit f3841ad

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Syllable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Syllable
2323
/**
2424
* Version string, used to recalculate language caches if needed.
2525
*/
26-
const CACHE_VERSION = 1.4;
26+
const CACHE_VERSION = '1.4';
2727

2828
/**
2929
* @var Cache

tests/src/SyllableTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function dataCache()
117117
'json',
118118
'json/syllable.en-us.json',
119119
'{'.
120-
'"version":1.4,'.
120+
'"version":"1.4",'.
121121
'"patterns":{%a},'.
122122
'"max_pattern":9,'.
123123
'"hyphenation":{%a},'.
@@ -131,7 +131,7 @@ public function dataCache()
131131
'serialized',
132132
'serialized/syllable.en-us.serialized',
133133
'a:6:{'.
134-
's:7:"version";d:1.4;'.
134+
's:7:"version";s:3:"1.4";'.
135135
's:8:"patterns";a:4939:{%a}'.
136136
's:11:"max_pattern";i:9;'.
137137
's:11:"hyphenation";a:15:{%a}'.
@@ -169,14 +169,41 @@ public function testCache($cacheClass, $language, $cacheDirectory, $cacheFile, $
169169

170170
$this->assertFileExists($cacheFile);
171171
$this->assertStringMatchesFormat($expectedCacheContent, file_get_contents($cacheFile));
172-
$this->assertSame(1.4, $this->object->getCache()->__get('version'));
172+
$this->assertSame('1.4', $this->object->getCache()->__get('version'));
173173
$this->assertNotEmpty($this->object->getCache()->__get('patterns'));
174174
$this->assertGreaterThan(0, $this->object->getCache()->__get('max_pattern'));
175175
$this->assertNotEmpty($this->object->getCache()->__get('hyphenation'));
176176
$this->assertInternalType('int', $this->object->getCache()->__get('left_min_hyphen'));
177177
$this->assertInternalType('int', $this->object->getCache()->__get('right_min_hyphen'));
178178
}
179179

180+
public function dataCacheVersionMatchesCacheFileVersionIsRelaxed()
181+
{
182+
return [
183+
'Cache file version is float.' => [1.4],
184+
'Cache file version is string.' => ['1.4'],
185+
'Cache file version is float with unexpected precision.' => [1.3999999999999999],
186+
'Cache file version is string with unexpected precision.' => ['1.3999999999999999'],
187+
];
188+
}
189+
190+
/**
191+
* Some PHP versions have the Syllable cache version number 1.4 encoded in 1.39999999999999
192+
* instead of 1.4 in the cache files. To fix this, the internal representation of
193+
* the cache version is changed from float to string. This test shows that the user's existing
194+
* cache files are still valid after this change and do not need to be recreated.
195+
*
196+
* @dataProvider dataCacheVersionMatchesCacheFileVersionIsRelaxed
197+
*
198+
* @return void
199+
*/
200+
public function testCacheVersionMatchesCacheFileVersionIsRelaxed($cacheFileVersion)
201+
{
202+
$cacheVersion = '1.4';
203+
204+
$this->assertTrue($cacheVersion == $cacheFileVersion);
205+
}
206+
180207
/**
181208
* @todo Implement testSetSource().
182209
*/

0 commit comments

Comments
 (0)