Skip to content

Commit

Permalink
Merge pull request #50 from creative-commoners/pulls/2/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
GuySartorelli authored Apr 26, 2022
2 parents 95659b0 + d2b6e0b commit 6bdf037
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Handling/SpellCheckAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getLanguages()
foreach (SpellController::get_locales() as $locale) {
$localeName = i18n::getData()->localeName($locale);
// Fix incorrectly spelled Māori language
$localeName = str_replace('Maori', 'Māori', $localeName);
$localeName = str_replace('Maori', 'Māori', $localeName ?? '');
$languages[] = $localeName . '=' . $locale;
}
return $languages;
Expand Down
2 changes: 1 addition & 1 deletion src/Handling/SpellCheckMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getLanguages()
foreach (SpellController::get_locales() as $locale) {
$localeName = i18n::getData()->localeName($locale);
// Fix incorrectly spelled Māori language
$localeName = str_replace('Maori', 'Māori', $localeName);
$localeName = str_replace('Maori', 'Māori', $localeName ?? '');
$languages[] = $localeName . '=' . $locale;
}
return $languages;
Expand Down
6 changes: 3 additions & 3 deletions src/Handling/SpellController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function index()
// Perform action
try {
$method = $data['method'];
$words = explode(' ', $data['text']);
$words = explode(' ', $data['text'] ?? '');
switch ($method) {
case 'spellcheck':
return $this->success($this->assembleData($locale, $words));
Expand Down Expand Up @@ -279,11 +279,11 @@ protected function getLocale(array $data)
$locale = $data['lang'];

// Check if the locale is actually a language
if (strpos($locale, '_') === false) {
if (strpos($locale ?? '', '_') === false) {
$locale = i18n::getData()->localeFromLang($locale);
}

if (!in_array($locale, self::get_locales())) {
if (!in_array($locale, self::get_locales() ?? [])) {
return false;
}

Expand Down
14 changes: 7 additions & 7 deletions src/Providers/HunSpellProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HunSpellProvider implements SpellProvider
protected function invoke($locale, $input, &$stdout, &$stderr)
{
// Prepare arguments
$command = 'hunspell -d ' . escapeshellarg($locale);
$command = 'hunspell -d ' . escapeshellarg($locale ?? '');
$descriptorSpecs = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
Expand All @@ -41,13 +41,13 @@ protected function invoke($locale, $input, &$stdout, &$stderr)
'LANG' => $locale . '.utf-8'
);
// Invoke command
$proc = proc_open($command, $descriptorSpecs, $pipes, null, $env);
$proc = proc_open($command ?? '', $descriptorSpecs ?? [], $pipes, null, $env);
if (!is_resource($proc)) {
return 255;
}

// Send content as input
fwrite($pipes[0], $input);
fwrite($pipes[0], $input ?? '');
fclose($pipes[0]);

// Get output
Expand Down Expand Up @@ -82,9 +82,9 @@ protected function getResults($locale, $words)
// Parse results
$pattern = Config::inst()->get(__CLASS__, 'pattern');
$results = array();
foreach (preg_split('/$\R?^/m', $stdout) as $line) {
if (preg_match($pattern, $line, $matches)) {
$results[$matches['original']] = explode(', ', $matches['misses']);
foreach (preg_split('/$\R?^/m', $stdout ?? '') as $line) {
if (preg_match($pattern ?? '', $line ?? '', $matches)) {
$results[$matches['original']] = explode(', ', $matches['misses'] ?? '');
}
}
return $results;
Expand All @@ -93,7 +93,7 @@ protected function getResults($locale, $words)
public function checkWords($locale, $words)
{
$results = $this->getResults($locale, $words);
return array_keys($results);
return array_keys($results ?? []);
}

public function getSuggestions($locale, $word)
Expand Down
2 changes: 1 addition & 1 deletion tests/HunSpellProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HunSpellProviderTest extends SapphireTest
*/
protected function assertArrayContains($needles, $haystack)
{
$overlap = array_intersect($needles, $haystack);
$overlap = array_intersect($needles ?? [], $haystack);
$this->assertEquals($overlap, $needles, "Assert that array contains all values specified");
}

Expand Down
24 changes: 12 additions & 12 deletions tests/SpellControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ public function testSecurityID()
// Test request sans token
$response = $this->get('spellcheck', Injector::inst()->create(Session::class, $session));
$this->assertEquals(400, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertEquals($tokenError, $jsonBody->error);

// Test request with correct token (will fail with an unrelated error)
$response = $this->get(
'spellcheck/?SecurityID='.urlencode($token),
'spellcheck/?SecurityID='.urlencode($token ?? ''),
Injector::inst()->create(Session::class, $session)
);
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertNotEquals($tokenError, $jsonBody->error);

// Test request with check disabled
Config::modify()->set(SpellController::class, 'enable_security_token', false);
$response = $this->get('spellcheck', Injector::inst()->create(Session::class, $session));
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertNotEquals($tokenError, $jsonBody->error);
}

Expand All @@ -104,20 +104,20 @@ public function testPermissions()
Config::modify()->set(SpellController::class, 'required_permission', 'ADMIN');
$this->logInWithPermission('ADMIN');
$response = $this->get('spellcheck');
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertNotEquals($securityError, $jsonBody->error);

// Test insufficient permissions
$this->logInWithPermission('CMS_ACCESS_CMSMain');
$response = $this->get('spellcheck');
$this->assertEquals(403, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertEquals($securityError, $jsonBody->error);

// Test disabled permissions
Config::modify()->set(SpellController::class, 'required_permission', false);
$response = $this->get('spellcheck');
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertNotEquals($securityError, $jsonBody->error);
}

Expand Down Expand Up @@ -195,23 +195,23 @@ public function testInputRejection()
];
$response = $this->post('spellcheck', ['ajax' => true] + $mockData);
$this->assertEquals(200, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertNotEmpty($jsonBody->words);
$this->assertNotEmpty($jsonBody->words->collor);
$this->assertEquals(['collar', 'colour'], $jsonBody->words->collor);

// Test non-ajax rejection
$response = $this->post('spellcheck', $mockData);
$this->assertEquals(400, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertEquals($invalidRequest, $jsonBody->error);

// Test incorrect method
$dataInvalidMethod = $mockData;
$dataInvalidMethod['method'] = 'validate';
$response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod);
$this->assertEquals(400, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertEquals(
_t(
'SilverStripe\\SpellCheck\\Handling\\SpellController.UnsupportedMethod',
Expand All @@ -226,7 +226,7 @@ public function testInputRejection()
unset($dataNoMethod['method']);
$response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod);
$this->assertEquals(400, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertEquals($invalidRequest, $jsonBody->error);

// Test unsupported locale
Expand All @@ -235,7 +235,7 @@ public function testInputRejection()

$response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale);
$this->assertEquals(400, $response->getStatusCode());
$jsonBody = json_decode($response->getBody());
$jsonBody = json_decode($response->getBody() ?? '');
$this->assertEquals(_t(
'SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidLocale',
'Not a supported locale'
Expand Down

0 comments on commit 6bdf037

Please sign in to comment.