Completed
Push — master ( 3d1e8f...65275d )
by Gino
122:52 queued 120:37
created

examples/lookup.php (2 issues)

1
<?php
2
3
require_once __DIR__.'/../vendor/autoload.php';
4
5
use GinoPane\PHPolyglot\PHPolyglot;
6
7
try {
8
    $phpolyglot = new PHPolyglot();
9
10
    $textToLookup = 'Hello!';
11
12
    $languageFrom = 'en';
13
14
    $response = $phpolyglot->lookup($textToLookup, $languageFrom)->getEntries();
15
16
    if (!$response) {
0 ignored issues
show
The condition ! $response can never be false.
Loading history...
Bug Best Practice introduced by
The expression $response of type GinoPane\PHPolyglot\API\...Entry\DictionaryEntry[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
17
        throw new Exception('Nothing returned! Maybe API has changed?');
18
    }
19
20
    $synonyms = implode(", ", $response[0]->getSynonyms());
21
22
    $output = <<<TEXT
23
    Initial word: {$response[0]->getTextFrom()}
24
  
25
    Part of speech: {$response[0]->getPosFrom()}
26
    Transcription: {$response[0]->getTranscription()}
27
    
28
    Main alternative: {$response[0]->getTextTo()}
29
    Synonyms: {$synonyms}
30
31
TEXT;
32
33
    echo $output;
34
} catch (Exception $exception) {
35
    $errorMessage = $exception->getMessage();
36
37
    echo sprintf("Error happened: %s", $errorMessage);
38
}
39
40
echo PHP_EOL;