Skip to content

Commit

Permalink
Added more type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Aug 22, 2018
1 parent 7e6e4fa commit faf6cec
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
15 changes: 5 additions & 10 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@
*/
class Helper
{
public static function className()
{
return get_called_class();
}

/**
* Merges two or more arrays into one recursively.
* Based on Yii2 yii\helpers\BaseArrayHelper::merge.
* @return array the merged array
*/
public static function mergeConfig()
public static function mergeConfig(): array
{
$args = func_get_args();
$res = array_shift($args) ?: [];
Expand Down Expand Up @@ -62,7 +57,7 @@ public static function mergeConfig()
* @param Closure $closure
* @return string
*/
public static function dumpClosure(Closure $closure)
public static function dumpClosure(Closure $closure): string
{
$res = 'function (';
$fun = new ReflectionFunction($closure);
Expand Down Expand Up @@ -99,7 +94,7 @@ public static function dumpClosure(Closure $closure)
* @param mixed $value
* @return string
*/
public static function exportVar($value)
public static function exportVar($value): string
{
$closures = self::collectClosures($value);
$res = var_export($value, true);
Expand All @@ -114,7 +109,7 @@ public static function exportVar($value)
return $res;
}

public static function exportDefines(array $defines)
public static function exportDefines(array $defines): string
{
$res = '';
foreach ($defines as $key => $value) {
Expand All @@ -131,7 +126,7 @@ public static function exportDefines(array $defines)
* @param mixed $input will be changed
* @return array array of found closures
*/
private static function collectClosures(&$input)
private static function collectClosures(&$input): array
{
static $closureNo = 1;
$closures = [];
Expand Down
30 changes: 17 additions & 13 deletions src/configs/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getValues(): array
return $this->values;
}

public function load(array $paths = [])
public function load(array $paths = []): self
{
$configs = [];
foreach ($paths as $path) {
Expand All @@ -86,7 +86,7 @@ public function load(array $paths = [])
* @param string $path
* @return array configuration read from file
*/
protected function loadFile($path)
protected function loadFile($path): array
{
$reader = ReaderFactory::get($this->builder, $path);

Expand All @@ -98,28 +98,28 @@ protected function loadFile($path)
* @param mixed $name
* @param array $configs
*/
public function build()
public function build(): self
{
$this->values = $this->calcValues($this->sources);

return $this;
}

public function write()
public function write(): self
{
$this->writeFile($this->getOutputPath(), $this->values);

return $this;
}

protected function calcValues(array $sources)
protected function calcValues(array $sources): array
{
$values = call_user_func_array([Helper::class, 'mergeConfig'], $sources);

return $this->substituteOutputDirs($values);
}

protected function writeFile(string $path, array $data)
protected function writeFile(string $path, array $data): void
{
$this->writePhpFile($path, $data, true);
}
Expand All @@ -130,7 +130,7 @@ protected function writeFile(string $path, array $data)
* @param string|array $data
* @param bool $withEnvAndDefines
*/
protected function writePhpFile(string $path, $data, bool $withEnvAndDefines)
protected function writePhpFile(string $path, $data, bool $withEnvAndDefines): void
{
static::putFile($path, $this->replaceMarkers(implode("\n\n", array_filter([
'header' => '<?php',
Expand All @@ -141,7 +141,11 @@ protected function writePhpFile(string $path, $data, bool $withEnvAndDefines)
]))) . "\n");
}

protected function renderVars(array $vars)
/**
* @param array $vars array to be exported
* @return string
*/
protected function renderVars(array $vars): string
{
return 'return ' . Helper::exportVar($vars) . ';';
}
Expand All @@ -158,7 +162,7 @@ protected function replaceMarkers(string $content): string
* @param string $path
* @param string $content
*/
protected static function putFile($path, $content)
protected static function putFile($path, $content): void
{
if (file_exists($path) && $content === file_get_contents($path)) {
return;
Expand All @@ -176,7 +180,7 @@ protected static function putFile($path, $content)
* @param array $data
* @return array
*/
public function substituteOutputDirs(array $data)
public function substituteOutputDirs(array $data): array
{
$dir = static::normalizePath(dirname(dirname(dirname($this->getOutputDir()))));

Expand All @@ -190,7 +194,7 @@ public function substituteOutputDirs(array $data)
* @param string $ds directory separator
* @return string
*/
public static function normalizePath($path, $ds = self::UNIX_DS)
public static function normalizePath($path, $ds = self::UNIX_DS): string
{
return rtrim(strtr($path, '/\\', $ds . $ds), $ds);
}
Expand All @@ -202,7 +206,7 @@ public static function normalizePath($path, $ds = self::UNIX_DS)
* @param string $alias
* @return array
*/
public static function substitutePaths($data, $dir, $alias)
public static function substitutePaths($data, $dir, $alias): array
{
foreach ($data as &$value) {
if (is_string($value)) {
Expand All @@ -222,7 +226,7 @@ public static function substitutePaths($data, $dir, $alias)
* @param string $alias
* @return string
*/
protected static function substitutePath($path, $dir, $alias)
protected static function substitutePath($path, $dir, $alias): string
{
$end = $dir . self::UNIX_DS;
$skippable = 0 === strncmp($path, '?', 1) ? '?' : '';
Expand Down
4 changes: 2 additions & 2 deletions src/configs/Defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class Defines extends Config
{
protected function loadFile($path)
protected function loadFile($path): array
{
parent::loadFile($path);
if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
Expand All @@ -37,7 +37,7 @@ public function buildRequires(): string
return implode("\n", $res);
}

protected function writeFile(string $path, array $data)
protected function writeFile(string $path, array $data): void
{
$this->writePhpFile($path, $this->buildRequires(), false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/configs/DotEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class DotEnv extends Config
{
protected function writeFile(string $path, array $data)
protected function writeFile(string $path, array $data): void
{
$this->writePhpFile($path, $data, false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/configs/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
*/
class Params extends Config
{
protected function calcValues(array $sources)
protected function calcValues(array $sources): array
{
return $this->pushEnvVars(parent::calcValues($sources));
}

protected function pushEnvVars($vars)
protected function pushEnvVars($vars): array
{
$env = $this->builder->getConfig('dotenv')->getValues();
if (!empty($vars)) {
Expand Down
2 changes: 1 addition & 1 deletion src/configs/Rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class Rebuild extends Config
{
protected function writeFile(string $path, array $data)
protected function writeFile(string $path, array $data): void
{
static::putFile($path, file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '__rebuild.php'));
}
Expand Down
16 changes: 10 additions & 6 deletions src/configs/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@
*/
class System extends Config
{
public function setValue(string $name, $value)
public function setValue(string $name, $value): Config
{
$this->values[$name] = $value;

return $this;
}

public function setValues(array $values)
public function setValues(array $values): Config
{
$this->values = $values;

return $this;
}

public function mergeValues(array $values)
public function mergeValues(array $values): Config
{
$this->values = array_merge($this->values, $values);

return $this;
}

protected function writeFile(string $path, array $data)
protected function writeFile(string $path, array $data): void
{
$this->writePhpFile($path, $data, false);
}

public function load(array $paths = [])
public function load(array $paths = []): Config
{
$path = $this->getOutputPath();
if (!file_exists($path)) {
Expand All @@ -52,7 +56,7 @@ public function load(array $paths = [])
return $this;
}

public function build()
public function build(): Config
{
$this->values = $this->substituteOutputDirs($this->values);

Expand Down

0 comments on commit faf6cec

Please sign in to comment.