Skip to content

Commit 0f76f90

Browse files
committed
Add deprecations
1 parent 6efec7a commit 0f76f90

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515
- Switched to v3 API which uses updated language detection model
16-
- ⚠️ `simpleDetect` method renamed to `detectCode`
1716
- ⚠️ `detect` method result fields are `language` and `score`
18-
- ⚠️ `detect` method no longer accept arrays - use `detectBatch` instead
19-
- HTTPS is used by default. Removed secure mode configuration.
17+
- ⚠️ `simpleDetect` deprecated, use `detectCode` instead
18+
- ⚠️ `detect` for batch detection is deprecated, use `detectBatch` instead
19+
20+
### Removed
21+
- Secure mode configuration. HTTPS is always used.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": ">=5.3.0"
1616
},
1717
"require-dev": {
18-
"yoast/phpunit-polyfills": "^1.0"
18+
"phpunit/phpunit": "^12.2"
1919
},
2020
"config": {
2121
"bin-dir": "bin"
@@ -24,5 +24,8 @@
2424
"psr-0": {
2525
"DetectLanguage": "lib/"
2626
}
27+
},
28+
"scripts": {
29+
"test": "php -d memory_limit=512M vendor/phpunit/phpunit/phpunit"
2730
}
2831
}

lib/DetectLanguage/DetectLanguage.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static function setApiKey($apiKey)
5959
public static function detect($text)
6060
{
6161
if (is_array($text)) {
62-
throw new Error('detect method does not accept arrays, use detectBatch instead');
62+
trigger_error('detect method does not accept arrays, use detectBatch instead', E_USER_DEPRECATED);
63+
return self::detectBatch($text);
6364
}
6465

6566
return Client::request('POST', 'detect', array('q' => $text));
@@ -115,4 +116,17 @@ public static function getLanguages()
115116
{
116117
return Client::request('GET', 'languages');
117118
}
119+
120+
// DEPRECATED METHODS
121+
122+
/**
123+
* @deprecated use self::detectCode instead
124+
* @param string $text The text for language detection
125+
* @return string|null detected language code
126+
*/
127+
public static function simpleDetect($text)
128+
{
129+
trigger_error('simpleDetect method is deprecated, use detectCode instead', E_USER_DEPRECATED);
130+
return self::detectCode($text);
131+
}
118132
}

phpunit.xml.dist

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">./lib/DetectLanguage/</directory>
6-
</include>
7-
</coverage>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
backupGlobals="false"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.0/phpunit.xsd"
8+
executionOrder="random"
9+
displayDetailsOnPhpunitDeprecations="true"
10+
>
811
<testsuites>
912
<testsuite name="DetectLanguage Test Suite">
1013
<directory suffix="Test.php">./tests/DetectLanguage/</directory>

tests/DetectLanguage/DetectLanguageTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace DetectLanguage\Test;
44

55
use \DetectLanguage\DetectLanguage;
6-
use \Yoast\PHPUnitPolyfills\TestCases\TestCase;
6+
use PHPUnit\Framework\TestCase;
77

88
class DetectLanguageTest extends TestCase
99
{
10-
public function set_up()
10+
protected function setUp(): void
1111
{
12-
parent::set_up();
12+
parent::setUp();
1313

1414
DetectLanguage::$apiKey = getenv('DETECTLANGUAGE_API_KEY');
1515
}
@@ -38,8 +38,12 @@ public function testDetect()
3838

3939
public function testDetectWithArray()
4040
{
41-
$this->expectException('\DetectLanguage\Error');
41+
$this->expectUserDeprecationMessage('detect method does not accept arrays, use detectBatch instead');
42+
4243
$result = DetectLanguage::detect(array('Hello world'));
44+
45+
$this->assertEquals('en', $result[0][0]->language,
46+
'To detect English language.');
4347
}
4448

4549
public function testDetectCode()
@@ -50,6 +54,14 @@ public function testDetectCode()
5054
'To detect English language.');
5155
}
5256

57+
public function testSimpleDetect()
58+
{
59+
$this->expectUserDeprecationMessage('simpleDetect method is deprecated, use detectCode instead');
60+
$result = DetectLanguage::simpleDetect('Hello world');
61+
$this->assertEquals('en', $result,
62+
'To detect English language.');
63+
}
64+
5365
public function testCurlRequest()
5466
{
5567
$this->setRequestEngine('curl');

0 commit comments

Comments
 (0)