Skip to content

Commit

Permalink
Fix some CS
Browse files Browse the repository at this point in the history
  • Loading branch information
landrok committed May 10, 2021
1 parent 97f30d9 commit c57f890
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/LanguageDetector/EmptyLanguage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/*
* This file is part of the LanguageDetector package.
Expand All @@ -15,16 +15,16 @@

/**
* Empty language
*/
class EmptyLanguage extends Language
*/
final class EmptyLanguage extends Language
{
/**
* Emulate default values for an empty language
*/
public function __construct()
{
$this->file = null;
$this->loaded = true;
$this->file = null;
$this->loaded = true;
$this->subset = [
'freq' => [],
'n_words' => [],
Expand Down
18 changes: 11 additions & 7 deletions src/LanguageDetector/Language.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/*
* This file is part of the LanguageDetector package.
Expand All @@ -17,7 +17,7 @@

/**
* Language binds a subset
*/
*/
class Language
{
/**
Expand All @@ -39,12 +39,12 @@ class Language

/**
* Config a subset file to load
*
*
* @param string $file File which contains subset data.
*/
public function __construct(string $file)
{
$this->file = $file;
$this->file = $file;
}

/**
Expand Down Expand Up @@ -74,10 +74,12 @@ public function load(): self

/**
* Get frequencies by ngrams
*
* @return array<string,int>
*/
public function getFreq(): array
{
if (!$this->loaded) {
if (! $this->loaded) {
$this->load();
}

Expand All @@ -86,10 +88,12 @@ public function getFreq(): array

/**
* Get total numbers of occurences by ngram sizes
*
* @return array<int>
*/
public function getNWords(): array
{
if (!$this->loaded) {
if (! $this->loaded) {
$this->load();
}

Expand All @@ -101,7 +105,7 @@ public function getNWords(): array
*/
public function getCode(): string
{
if (!$this->loaded) {
if (! $this->loaded) {
$this->load();
}

Expand Down
46 changes: 23 additions & 23 deletions src/LanguageDetector/LanguageDetector.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/*
* This file is part of the LanguageDetector package.
Expand All @@ -19,15 +19,15 @@
/**
* LanguageDetector is the entry point for the detecting process.
*/
class LanguageDetector
final class LanguageDetector
{
/**
* @var \LanguageDetector\Language[]
* @var array<Language>
*/
private $languages = [];

/**
* @var array
* @var array<string,float>
*/
private $scores = [];

Expand All @@ -37,23 +37,24 @@ class LanguageDetector
private $text = '';

/**
* @var \LanguageDetector\LanguageDetector
* @var LanguageDetector
*/
private static $detector;

/**
* Configure all subset languages
*
* @param string $dir A directory where subsets are.
* @param array $languages Language codes to load models for. By default, all languages are loaded.
* @param array<string> $languages Language codes to load models for. By default, all languages are loaded.
*/
public function __construct(string $dir = null, array $languages = [])
public function __construct(?string $dir = null, array $languages = [])
{
$datadir = null === $dir
? __DIR__ . '/subsets' : rtrim($dir, '/');
$datadir = is_null($dir)
? __DIR__ . '/subsets'
: rtrim($dir, '/');

foreach (glob($datadir . '/*') as $file) {
if (!count($languages)
if (! count($languages)
|| in_array(basename($file), $languages)
) {
$this->languages[basename($file)] = new Language($file);
Expand All @@ -69,7 +70,7 @@ public function __construct(string $dir = null, array $languages = [])
*/
public function evaluate(string $text): self
{
if (empty($text)) {
if ($text === '') {
return $this;
}

Expand All @@ -85,7 +86,6 @@ public function evaluate(string $text): self
/**
* Static call for oneliners
*
* @param string $text
* @param array $languages Language codes to load models for. By
* default, all languages are loaded.
* @return \LanguageDetector\LanguageDetector
Expand All @@ -94,7 +94,7 @@ public function evaluate(string $text): self
public static function detect(string $text, array $languages = []): self
{
// Current loaded models
$current = !is_null(self::$detector)
$current = ! is_null(self::$detector)
? self::$detector->getLanguages()
: [];

Expand All @@ -121,9 +121,9 @@ public static function detect(string $text, array $languages = []): self
* Language definition.
* @api
*/
public function getLanguage(string $code = null): Language
public function getLanguage(?string $code = null): Language
{
if (!count($this->scores)) {
if (! count($this->scores)) {
return new EmptyLanguage();
}

Expand All @@ -136,10 +136,10 @@ public function getLanguage(string $code = null): Language
return $this->languages[$code];
}

/**
/**
* Get loaded languages
*
* @return []string An array of ISO codes
* @return array<string> An array of ISO codes
* @api
*/
public function getLanguages(): array
Expand All @@ -150,13 +150,13 @@ public function getLanguages(): array
/**
* Get all scored subsets
*
* @return array An array of ISO codes => scores
* @return array<string,float> An array of ISO codes => scores
* @throws \Exception if nothing has been evaluated
* @api
*/
public function getScores(): array
{
if (!count($this->scores)) {
if (! count($this->scores)) {
throw new Exception('No string has been evaluated');
}

Expand All @@ -166,7 +166,7 @@ public function getScores(): array
/**
* Get all supported languages
*
* @return array An array of ISO codes
* @return array<string> An array of ISO codes
* @api
*/
public function getSupportedLanguages(): array
Expand All @@ -188,16 +188,16 @@ public function getText(): string
*/
public function __toString(): string
{
return (string)$this->getLanguage();
return (string) $this->getLanguage();
}

/**
* Get a callable evaluator that will calculate probabilities
* Get a callable evaluator that will calculate probabilities
* for one language.
*/
private function calculate(array $chunks): callable
{
return function($language, $code) use ($chunks) {
return function ($language, $code) use ($chunks): void {
$this->scores[$code] =
array_sum(
array_intersect_key(
Expand Down

0 comments on commit c57f890

Please sign in to comment.