landrok / language-detector

A fast and reliable PHP library for detecting languages
MIT License
117 stars 18 forks source link

Uncaught Error: Call to a member function getLanguages() on null #6

Closed deevroman closed 3 years ago

deevroman commented 3 years ago

Code from README.md

echo LanguageDetector::detect(
    'My tailor is rich and Alison is in the kitchen with Bob.',
    ['en', 'de', 'fr', 'es']
);
PHP Fatal error:  Uncaught Error: Call to a member function getLanguages() on null in PATH/LanguageDetector/LanguageDetector.php:104
Stack trace:
#0 PATH(31): LanguageDetector\LanguageDetector::detect('My tailor is ri...', Array)
public static function detect($text, array $languages = []): self
{
    // All specified models have been loaded
    $diff = count($languages)
        ? array_diff(
                    self::$detector->getLanguages(),
                    $languages
                )
        : [];
    if (is_null(self::$detector) || count($diff)) {
        self::$detector = new self(null, $languages);
    }

    return self::$detector->evaluate($text);
}

Error on line self::$detector->getLanguages(),

Maybe before need to add:

if (is_null(self::$detector)) {
    self::$detector = new self(null, $languages);
}
landrok commented 3 years ago

Hi @deevroman

I was sure that it had been tested but it surely wasn't enough.

I've just fixed and release v1.3.1.

Thanks for this catch.