Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
- add return type declaration
- add argument type hints
- add strict types
  • Loading branch information
icanhazstring committed Jan 17, 2019
1 parent 8007814 commit cb7c8bd
Show file tree
Hide file tree
Showing 39 changed files with 211 additions and 138 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
],
"require": {
"php": "^7.2",
"symfony/process": "^4.0"
"symfony/process": "^4.0",
"ext-iconv": "*",
"ext-dom": "*",
"ext-libxml": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
Expand Down
13 changes: 7 additions & 6 deletions src/Aspell/Aspell.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down Expand Up @@ -29,7 +31,7 @@ class Aspell extends Ispell
*
* @var string[]|null
*/
private $supportedLanguages = null;
private $supportedLanguages;

/**
* @var Dictionary
Expand All @@ -43,7 +45,7 @@ class Aspell extends Ispell
*
* @since 1.6
*/
public function __construct($binaryPath = 'aspell')
public function __construct(string $binaryPath = 'aspell')
{
parent::__construct($binaryPath);
}
Expand All @@ -57,10 +59,9 @@ public function __construct($binaryPath = 'aspell')
* @throws InvalidArgumentException
* @throws LogicException
* @throws RuntimeException
*
* @since 1.6
*/
public function getSupportedLanguages()
public function getSupportedLanguages(): array
{
if (null === $this->supportedLanguages) {
$process = $this->createProcess('dump dicts');
Expand Down Expand Up @@ -96,7 +97,7 @@ public function getSupportedLanguages()
/**
* @param Dictionary $dictionary
*/
public function setPersonalDictionary(Dictionary $dictionary)
public function setPersonalDictionary(Dictionary $dictionary): void
{
$this->personalDictionary = $dictionary;
}
Expand All @@ -111,7 +112,7 @@ public function setPersonalDictionary(Dictionary $dictionary)
*
* @since 1.6
*/
protected function createArguments(EncodingAwareSource $source, array $languages)
protected function createArguments(EncodingAwareSource $source, array $languages): array
{
$args = [
// Input encoding
Expand Down
4 changes: 3 additions & 1 deletion src/Dictionary.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down Expand Up @@ -28,7 +30,7 @@ public function __construct(string $path)
/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return $this->dictionaryPath;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/EnvironmentException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/ExternalProgramFailedException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/PhpSpellerException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/SourceException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down
34 changes: 18 additions & 16 deletions src/ExternalSpeller.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down Expand Up @@ -51,9 +53,9 @@ abstract class ExternalSpeller implements Speller
*
* @since 1.6
*/
public function __construct($binaryPath)
public function __construct(string $binaryPath)
{
$this->binary = (string)$binaryPath;
$this->binary = $binaryPath;
}

/**
Expand All @@ -69,7 +71,7 @@ public function __construct($binaryPath)
* @see http://tools.ietf.org/html/bcp47
* @since 1.6
*/
public function checkText(EncodingAwareSource $source, array $languages)
public function checkText(EncodingAwareSource $source, array $languages): array
{
$process = $this->createProcess($this->createArguments($source, $languages));
$process->setEnv($this->createEnvVars($source, $languages));
Expand Down Expand Up @@ -120,7 +122,7 @@ public function checkText(EncodingAwareSource $source, array $languages)
* @see \Symfony\Component\Process\Process::setTimeout()
* @since 1.6
*/
public function setTimeout($seconds)
public function setTimeout($seconds): void
{
$this->timeout = $seconds;
}
Expand All @@ -134,16 +136,16 @@ public function setTimeout($seconds)
*
* @since 1.6
*/
protected function composeCommand($args)
protected function composeCommand($args): string
{
$command = $this->getBinary();

if (is_array($args)) {
if (\is_array($args)) {
$args = implode(' ', $args);
}

// Only append args if we have some
$command .= strlen($args) ? ' ' . $args : '';
$command .= $args !== '' ? ' ' . $args : '';

return $command;
}
Expand All @@ -155,7 +157,7 @@ protected function composeCommand($args)
*
* @since 1.6
*/
protected function getBinary()
protected function getBinary(): string
{
return $this->binary;
}
Expand All @@ -172,7 +174,7 @@ protected function getBinary()
*
* @SuppressWarnings(PMD.UnusedFormalParameter)
*/
protected function createArguments(EncodingAwareSource $source, array $languages)
protected function createArguments(EncodingAwareSource $source, array $languages): array
{
return [];
}
Expand All @@ -189,7 +191,7 @@ protected function createArguments(EncodingAwareSource $source, array $languages
*
* @SuppressWarnings(PMD.UnusedFormalParameter)
*/
protected function createEnvVars(EncodingAwareSource $source, array $languages)
protected function createEnvVars(EncodingAwareSource $source, array $languages): array
{
return [];
}
Expand All @@ -203,7 +205,7 @@ protected function createEnvVars(EncodingAwareSource $source, array $languages)
*
* @since 1.6
*/
abstract protected function parseOutput($output);
abstract protected function parseOutput(string $output): array;

/**
* Create new instance of external program.
Expand All @@ -217,7 +219,7 @@ abstract protected function parseOutput($output);
*
* @since 1.6
*/
protected function createProcess($args = null)
protected function createProcess($args = null): Process
{
$command = $this->composeCommand($args);

Expand All @@ -239,13 +241,13 @@ protected function createProcess($args = null)
*
* @since 2.0
*/
private function composeProcess(string $command)
private function composeProcess(string $command): Process
{
if ($this->process === null) {
$this->process = new Process($command);
$this->process = new Process([$command]);
}

$this->process->inheritEnvironmentVariables(true);
$this->process->inheritEnvironmentVariables();
$this->process->setTimeout($this->timeout);
$this->process->setCommandLine($command);

Expand All @@ -257,7 +259,7 @@ private function composeProcess(string $command)
*
* @return self
*/
public function setProcess(Process $process)
public function setProcess(Process $process): self
{
$this->process = $process;

Expand Down
6 changes: 4 additions & 2 deletions src/Helper/LanguageMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller
*
Expand Down Expand Up @@ -38,7 +40,7 @@ class LanguageMapper
* @link http://tools.ietf.org/html/bcp47
* @since 1.0
*/
public function map(array $requested, array $supported)
public function map(array $requested, array $supported): array
{
$index = [];
foreach ($supported as $tag) {
Expand Down Expand Up @@ -88,7 +90,7 @@ public function map(array $requested, array $supported)
*
* @since 1.1
*/
public function setPreferredMappings(array $mappings)
public function setPreferredMappings(array $mappings): void
{
foreach ($mappings as $language => $map) {
$this->preferred[$language] = (array) $map;
Expand Down
21 changes: 11 additions & 10 deletions src/Hunspell/Hunspell.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* PHP Speller.
*
Expand Down Expand Up @@ -29,7 +31,7 @@ class Hunspell extends Ispell
*
* @var string|null
*/
private $customDictPath = null;
private $customDictPath;

/**
* Custom dictionaries list.
Expand All @@ -43,7 +45,7 @@ class Hunspell extends Ispell
*
* @var string[]|null
*/
private $supportedLanguages = null;
private $supportedLanguages;

/**
* Create new hunspell adapter.
Expand All @@ -52,7 +54,7 @@ class Hunspell extends Ispell
*
* @since 1.0
*/
public function __construct($binaryPath = 'hunspell')
public function __construct(string $binaryPath = 'hunspell')
{
parent::__construct($binaryPath);
}
Expand All @@ -66,10 +68,9 @@ public function __construct($binaryPath = 'hunspell')
* @throws InvalidArgumentException
* @throws LogicException
* @throws RuntimeException
*
* @since 1.0
*/
public function getSupportedLanguages()
public function getSupportedLanguages(): array
{
if (null === $this->supportedLanguages) {
$process = $this->createProcess('-D');
Expand Down Expand Up @@ -120,9 +121,9 @@ public function getSupportedLanguages()
* @link setCustomDictionaries()
* @since 1.0
*/
public function setDictionaryPath($path)
public function setDictionaryPath(string $path): void
{
$this->customDictPath = (string) $path;
$this->customDictPath = $path;
$this->supportedLanguages = null; // Clear cache because $path can contain new languages
}

Expand All @@ -134,7 +135,7 @@ public function setDictionaryPath($path)
* @link setDictionaryPath()
* @since 1.0
*/
public function setCustomDictionaries(array $customDictionaries)
public function setCustomDictionaries(array $customDictionaries): void
{
$this->customDictionaries = $customDictionaries;
}
Expand All @@ -149,7 +150,7 @@ public function setCustomDictionaries(array $customDictionaries)
*
* @since 1.6
*/
protected function createArguments(EncodingAwareSource $source, array $languages)
protected function createArguments(EncodingAwareSource $source, array $languages): array
{
$args = [
// Input encoding
Expand Down Expand Up @@ -181,7 +182,7 @@ protected function createArguments(EncodingAwareSource $source, array $languages
*
* @SuppressWarnings(PMD.UnusedFormalParameter)
*/
protected function createEnvVars(EncodingAwareSource $source, array $languages)
protected function createEnvVars(EncodingAwareSource $source, array $languages): array
{
$vars = [];
if ($this->customDictPath) {
Expand Down
Loading

0 comments on commit cb7c8bd

Please sign in to comment.