From 4afeb40287e8056676271101732d29648246a74b Mon Sep 17 00:00:00 2001 From: mbaklouti Date: Mon, 18 Oct 2021 13:11:26 +0200 Subject: [PATCH 1/2] Updated composer requires symfony/filesystem ^5.3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index afc174f..51e1874 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php" : ">=5.4.0", - "symfony/filesystem": "^2.8 | ^3.0" + "symfony/filesystem": "^5.3" }, "require-dev": { "phpunit/phpunit" : "^4.8.36 || ^5.7 || ^6.5", From 7fc8b77cc2b14fe6c1cc389c76cf7e9ba66e71ba Mon Sep 17 00:00:00 2001 From: mbaklouti Date: Tue, 19 Oct 2021 09:52:12 +0200 Subject: [PATCH 2/2] migrated to php 7 compatibility --- composer.json | 2 +- src/Cache/CacheProvider.php | 44 ++++---- src/Loader/AbstractLoader.php | 8 +- src/Loader/DirectoryLoader.php | 37 +++--- src/Loader/EnvLoader.php | 10 +- src/Loader/IniLoader.php | 9 +- src/Loader/JsonLoader.php | 9 +- src/Loader/LoaderProvider.php | 39 ++++--- src/Loader/PhpLoader.php | 8 +- src/Loader/TomlLoader.php | 10 +- src/Loader/XmlLoader.php | 8 +- src/Loader/YamlLoader.php | 10 +- src/Provider/Silex/VarsServiceProvider.php | 12 +- src/Resource/AbstractResource.php | 32 +++--- src/Resource/FileResource.php | 65 ++++++----- src/Resource/ResourceProvider.php | 57 +++++----- src/Traits/FileTrait.php | 48 +++++--- src/Traits/PathTrait.php | 20 ++-- src/Traits/ResourceFlagsTrait.php | 19 ++-- src/Traits/TransformerTrait.php | 8 +- src/Variables/ReplacementStore.php | 23 ++-- src/Variables/VariableProvider.php | 40 +++---- src/Variables/VariableStore.php | 14 +-- src/Vars.php | 67 +++++------ tests/Plugin/FakeFileResource.php | 2 +- tests/Plugin/TextLoader.php | 2 +- tests/Plugin/TextNoExtensionLoader.php | 3 +- tests/VarsTest.php | 125 +++++++++------------ 28 files changed, 378 insertions(+), 353 deletions(-) diff --git a/composer.json b/composer.json index 51e1874..3902891 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ { "name": "Miles Croxford", "email": "hello@milescroxford.com", - "homepage": "http://milescroxford.com", + "homepage": "https://milescroxford.com", "role": "Developer" } ], diff --git a/src/Cache/CacheProvider.php b/src/Cache/CacheProvider.php index db23c30..53c664c 100644 --- a/src/Cache/CacheProvider.php +++ b/src/Cache/CacheProvider.php @@ -38,7 +38,7 @@ class CacheProvider * * @var bool $attempted */ - private $attempted = false; + private bool $attempted = false; /** * How long for the cache to be fresh @@ -52,43 +52,43 @@ class CacheProvider * * @var bool $hit */ - private $hit = false; + private bool $hit = false; /** * The loaded Vars object from cache * - * @var \M1\Vars\Vars $loaded_vars + * @var Vars $loaded_vars */ - private $loaded_vars; + private Vars $loaded_vars; /** * The cache file name * * @var string $name */ - private $name; + private string $name; /** * Is the cache turned on * * @var boolean $provide */ - private $provide; + private bool $provide; /** * If cached, the time when the class was cached * * @var int $time */ - private $time; + private int $time; /** * Creates a new instance of the cacheProvider for Vars * * @param string|array $resource The main configuration resource - * @param array $options The options being used for Vars + * @param array $options The options being used for Vars */ - public function __construct($resource, $options) + public function __construct($resource, array $options) { $this->setProvide($options['cache']); $this->setPath($options['cache_path'], true); @@ -102,7 +102,7 @@ public function __construct($resource, $options) * * @return bool Returns true if has the cached resource */ - public function checkCache() + public function checkCache(): bool { if ($this->provide && $this->path && !$this->getAttempted()) { $file = sprintf('%s/vars/%s.php', $this->path, $this->name); @@ -129,7 +129,7 @@ public function load() /** * Transfer the contents of the parent Vars object into a file for cache * - * @param \M1\Vars\Vars $vars Parent vars object + * @param Vars $vars Parent vars object * * @codeCoverageIgnore */ @@ -151,7 +151,7 @@ public function makeCache(Vars $vars) * * @return bool Is cache on or off */ - public function getProvide() + public function getProvide(): bool { return $this->provide; } @@ -161,9 +161,9 @@ public function getProvide() * * @param bool $provide Does the cache want to be on or off * - * @return \M1\Vars\Cache\CacheProvider + * @return CacheProvider */ - public function setProvide($provide) + public function setProvide(bool $provide): CacheProvider { $this->provide = $provide; return $this; @@ -174,7 +174,7 @@ public function setProvide($provide) * * @return bool Has the cache been attempted */ - public function getAttempted() + public function getAttempted(): bool { return $this->attempted; } @@ -184,7 +184,7 @@ public function getAttempted() * * @return int Cache expire time */ - public function getExpire() + public function getExpire(): int { return $this->expire; } @@ -192,7 +192,7 @@ public function getExpire() /** * Returns how long the cache lasts for * - * @return int Cache expire time + * @return bool Cache expire time */ public function isHit() { @@ -204,7 +204,7 @@ public function isHit() * * @return int Cache creation time */ - public function getTime() + public function getTime(): int { return $this->time; } @@ -214,9 +214,9 @@ public function getTime() * * @param int $time Time when vars cached was created * - * @return \M1\Vars\Cache\CacheProvider The cacheProvider object + * @return CacheProvider The cacheProvider object */ - public function setTime($time) + public function setTime(int $time): CacheProvider { $this->time = $time; return $this; @@ -225,9 +225,9 @@ public function setTime($time) /** * Returns the loaded Vars object * - * @return \M1\Vars\Vars The loaded Vars object + * @return Vars The loaded Vars object */ - public function getLoadedVars() + public function getLoadedVars(): Vars { return $this->loaded_vars; } diff --git a/src/Loader/AbstractLoader.php b/src/Loader/AbstractLoader.php index d2eb47c..e1dd365 100644 --- a/src/Loader/AbstractLoader.php +++ b/src/Loader/AbstractLoader.php @@ -37,21 +37,21 @@ abstract class AbstractLoader * * @var string */ - protected $entity; + protected string $entity; /** * The supported extensions * * @var array */ - public static $supported = array(); + public static array $supported = array(); /** * Construct the loader with the passed entity * * @param string $entity The passed entity */ - public function __construct($entity) + public function __construct(string $entity) { $this->entity = $entity; } @@ -68,7 +68,7 @@ abstract public function load(); * * @return bool Does the loader support the file */ - public function supports() + public function supports(): bool { $extension = pathinfo($this->entity, PATHINFO_EXTENSION); return in_array($extension, static::$supported); diff --git a/src/Loader/DirectoryLoader.php b/src/Loader/DirectoryLoader.php index cc460d2..958e7d1 100644 --- a/src/Loader/DirectoryLoader.php +++ b/src/Loader/DirectoryLoader.php @@ -18,6 +18,11 @@ namespace M1\Vars\Loader; +use FilesystemIterator; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; +use RegexIterator; + /** * This file provides dir loading support for Vars * @@ -25,13 +30,15 @@ */ class DirectoryLoader extends AbstractLoader { + private bool $recursive; + /** * Construct the loader with the passed entity * * @param string $entity The passed entity - * @param bool $recursive Search the directories recursively + * @param bool $recursive Search the directories recursively */ - public function __construct($entity, $recursive) + public function __construct($entity, bool $recursive) { parent::__construct($entity); $this->recursive = $recursive; @@ -58,16 +65,16 @@ public function load() /** * Returns the supported files in the directory recursively * - * @return \RegexIterator The supported files in the directories + * @return RegexIterator The supported files in the directories */ - private function getSupportedFilesRecursively() + private function getSupportedFilesRecursively(): RegexIterator { - $dir_it = new \RecursiveDirectoryIterator($this->entity, \RecursiveDirectoryIterator::SKIP_DOTS); + $dir_it = new RecursiveDirectoryIterator($this->entity, FilesystemIterator::SKIP_DOTS); - $files = new \RecursiveIteratorIterator( + $files = new RecursiveIteratorIterator( $dir_it, - \RecursiveIteratorIterator::LEAVES_ONLY, - \RecursiveIteratorIterator::CATCH_GET_CHILD + RecursiveIteratorIterator::LEAVES_ONLY, + RecursiveIteratorIterator::CATCH_GET_CHILD ); return $this->createRegexIterator($files); @@ -76,9 +83,9 @@ private function getSupportedFilesRecursively() /** * Returns the supported files in the directory * - * @return \RegexIterator The supported files in the directory + * @return RegexIterator The supported files in the directory */ - private function getSupportedFiles() + private function getSupportedFiles(): RegexIterator { $files = new \FilesystemIterator($this->entity); @@ -88,13 +95,13 @@ private function getSupportedFiles() /** * Returns the supported files in the directory * - * @param \FilesystemIterator|\RecursiveIteratorIterator $files The found files in the directory/ies + * @param \FilesystemIterator|RecursiveIteratorIterator $files The found files in the directory/ies * - * @return \RegexIterator The supported files in the directory using the regexiterator + * @return RegexIterator The supported files in the directory using the regexiterator */ - private function createRegexIterator($files) + private function createRegexIterator($files): RegexIterator { - return new \RegexIterator( + return new RegexIterator( $files, '/^.*\.(' . implode('|', static::$supported) . ')$/i' ); @@ -107,7 +114,7 @@ private function createRegexIterator($files) * * @return array|bool The usable resources if any, else false */ - private function makeResources($paths) + private function makeResources(array $paths) { if ($paths && !empty($paths)) { $resources = array(); diff --git a/src/Loader/EnvLoader.php b/src/Loader/EnvLoader.php index 42f6844..936ba46 100644 --- a/src/Loader/EnvLoader.php +++ b/src/Loader/EnvLoader.php @@ -19,6 +19,8 @@ namespace M1\Vars\Loader; use M1\Env\Parser; +use RuntimeException; +use Throwable; /** * The Env file loader @@ -30,19 +32,19 @@ class EnvLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('env'); + public static array $supported = array('env'); /** * {@inheritdoc} * - * @throws \RuntimeException If `m1/env` library is not installed or the file can not be parsed + * @throws RuntimeException If `m1/env` library is not installed or the file can not be parsed */ public function load() { try { $this->content = Parser::parse(file_get_contents($this->entity)); - } catch (\Exception $e) { - throw new \RuntimeException(sprintf( + } catch (Throwable $e) { + throw new RuntimeException(sprintf( "%s threw an exception: %s", $this->entity, $e diff --git a/src/Loader/IniLoader.php b/src/Loader/IniLoader.php index 4f0cec1..d4dbd3d 100644 --- a/src/Loader/IniLoader.php +++ b/src/Loader/IniLoader.php @@ -18,6 +18,9 @@ namespace M1\Vars\Loader; +use RuntimeException; +use Throwable; + /** * The ini file loader * @@ -28,7 +31,7 @@ class IniLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('ini'); + public static array $supported = array('ini'); /** * {@inheritdoc} @@ -37,8 +40,8 @@ public function load() { try { $this->content = parse_ini_file($this->entity, true); - } catch (\Exception $e) { - throw new \RuntimeException(sprintf( + } catch (Throwable $e) { + throw new RuntimeException(sprintf( "%s threw an exception: %s", $this->entity, $e diff --git a/src/Loader/JsonLoader.php b/src/Loader/JsonLoader.php index 13fd624..039213f 100644 --- a/src/Loader/JsonLoader.php +++ b/src/Loader/JsonLoader.php @@ -18,6 +18,9 @@ namespace M1\Vars\Loader; +use RunntimeException; +use RuntimeException; + /** * The json file loader * @@ -28,12 +31,12 @@ class JsonLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('json'); + public static array $supported = array('json'); /** * {@inheritdoc} * - * @throws \RunntimeException If the json file fails to load + * @throws RunntimeException If the json file fails to load */ public function load() { @@ -42,7 +45,7 @@ public function load() if (json_last_error() !== JSON_ERROR_NONE) { $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Parse error'; - throw new \RuntimeException( + throw new RuntimeException( sprintf("'%s' failed to load with the error '%s'", $this->entity, $message) ); } diff --git a/src/Loader/LoaderProvider.php b/src/Loader/LoaderProvider.php index 25ba5e6..c339fa4 100644 --- a/src/Loader/LoaderProvider.php +++ b/src/Loader/LoaderProvider.php @@ -18,6 +18,9 @@ namespace M1\Vars\Loader; +use InvalidArgumentException; +use RuntimeException; + /** * This file provides the loaders and extensions supported by Vars * @@ -30,22 +33,22 @@ class LoaderProvider * * @var array $extensions */ - private $extensions = array(); + private array $extensions = array(); /** * The available loaders * * @var array $loaders */ - private $loaders = array(); + private array $loaders = array(); /** * The constructor for LoaderProvider, makes the loaders and extensions * * @param array|null $options The options being used for Vars - * @param array $default_loaders The default loaders for Vars + * @param array $default_loaders The default loaders for Vars */ - public function __construct($options, $default_loaders) + public function __construct(?array $options, array $default_loaders) { $this->makeLoaders($options, $default_loaders); } @@ -54,9 +57,9 @@ public function __construct($options, $default_loaders) * Get loaders and make extensions for the loaders * * @param array|null $options The options being used for Vars - * @param array $default_loaders The default loaders for Vars + * @param array $default_loaders The default loaders for Vars */ - private function makeLoaders($options, $default_loaders) + private function makeLoaders(?array $options, array $default_loaders) { $loaders = $this->preParseLoaders($options, $default_loaders); $parsed_loaders = array(); @@ -79,11 +82,11 @@ private function makeLoaders($options, $default_loaders) * Pre parse the loaders for use in make loaders * * @param array|null $options The options being used for Vars - * @param array $default_loaders The default loaders for Vars + * @param array $default_loaders The default loaders for Vars * * @return array The pre parsed loaders */ - private function preParseLoaders($options, $default_loaders) + private function preParseLoaders(?array $options, array $default_loaders): array { $loaders = array(); @@ -102,13 +105,13 @@ private function preParseLoaders($options, $default_loaders) * Makes namespace loaders from loader strings * * @param array $loaders The options being used for Vars - * @param array $default_loaders The default loaders for Vars - * - * @throws \InvalidArgumentException If a loader from options isn't found + * @param array $default_loaders The default loaders for Vars * * @return array The namespace loaders + *@throws InvalidArgumentException If a loader from options isn't found + * */ - private function makeNameSpaceLoaders($loaders, $default_loaders) + private function makeNameSpaceLoaders(array $loaders, array $default_loaders): array { $parsed_loaders = array(); @@ -118,7 +121,7 @@ private function makeNameSpaceLoaders($loaders, $default_loaders) } if (!class_exists($loader)) { - throw new \InvalidArgumentException(sprintf("'%s' loader class does not exist", $loader)); + throw new InvalidArgumentException(sprintf("'%s' loader class does not exist", $loader)); } $parsed_loaders[] = $loader; @@ -134,11 +137,11 @@ private function makeNameSpaceLoaders($loaders, $default_loaders) * * @param array $loaders File loaders * - * @throws \RuntimeException If no loader extensions were found + * @throws RuntimeException If no loader extensions were found * * @return array File loader supported extensions */ - private function makeExtensions(array $loaders) + private function makeExtensions(array $loaders): array { $extensions = array(); @@ -147,7 +150,7 @@ private function makeExtensions(array $loaders) } if (empty($extensions)) { - throw new \RuntimeException('No loader extensions were found'); + throw new RuntimeException('No loader extensions were found'); } return $extensions; @@ -158,7 +161,7 @@ private function makeExtensions(array $loaders) * * @return array The Vars file loaders */ - public function getLoaders() + public function getLoaders(): array { return $this->loaders; } @@ -168,7 +171,7 @@ public function getLoaders() * * @return array The Vars file loader extensions */ - public function getExtensions() + public function getExtensions(): array { return $this->extensions; } diff --git a/src/Loader/PhpLoader.php b/src/Loader/PhpLoader.php index e949c8b..759050b 100644 --- a/src/Loader/PhpLoader.php +++ b/src/Loader/PhpLoader.php @@ -18,6 +18,8 @@ namespace M1\Vars\Loader; +use RuntimeException; + /** * The PHP file loader * @@ -28,12 +30,12 @@ class PhpLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('php', 'php5'); + public static array $supported = array('php', 'php5'); /** * {@inheritdoc} * - * @throws \RuntimeException If the php file doesn't return an array + * @throws RuntimeException If the php file doesn't return an array */ public function load() { @@ -44,7 +46,7 @@ public function load() } if (!is_array($content)) { - throw new \RuntimeException(sprintf("'%s' does not return an array", $this->entity)); + throw new RuntimeException(sprintf("'%s' does not return an array", $this->entity)); } $this->content = $content; diff --git a/src/Loader/TomlLoader.php b/src/Loader/TomlLoader.php index 29145ce..4c87920 100644 --- a/src/Loader/TomlLoader.php +++ b/src/Loader/TomlLoader.php @@ -18,6 +18,8 @@ namespace M1\Vars\Loader; +use RuntimeException; +use Throwable; use Yosymfony\Toml\Toml; /** @@ -30,19 +32,19 @@ class TomlLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('toml', 'tml'); + public static array $supported = array('toml', 'tml'); /** * {@inheritdoc} * - * @throws \RuntimeException If the `yosymfonytoml` library is not installed or the toml file is not valid + * @throws RuntimeException If the `yosymfonytoml` library is not installed or the toml file is not valid */ public function load() { try { $this->content = Toml::parse($this->entity); - } catch (\Exception $e) { - throw new \RuntimeException( + } catch (Throwable $e) { + throw new RuntimeException( sprintf("'%s' failed to load with the error '%s'", $this->entity, $e) ); } diff --git a/src/Loader/XmlLoader.php b/src/Loader/XmlLoader.php index 705ee56..8b6bb0d 100644 --- a/src/Loader/XmlLoader.php +++ b/src/Loader/XmlLoader.php @@ -18,6 +18,8 @@ namespace M1\Vars\Loader; +use RuntimeException; + /** * The XML file loader * @@ -28,12 +30,12 @@ class XmlLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('xml'); + public static array $supported = array('xml'); /** * {@inheritdoc} * - * @throws \RuntimeException If the xml file fails to load + * @throws RuntimeException If the xml file fails to load */ public function load() { @@ -41,7 +43,7 @@ public function load() $content = simplexml_load_file($this->entity); if (!$content) { - throw new \RuntimeException( + throw new RuntimeException( sprintf("'%s' failed to load with the error '%s'", $this->entity, libxml_get_errors()[0]->message) ); } diff --git a/src/Loader/YamlLoader.php b/src/Loader/YamlLoader.php index 77e3971..a71e414 100644 --- a/src/Loader/YamlLoader.php +++ b/src/Loader/YamlLoader.php @@ -18,7 +18,9 @@ namespace M1\Vars\Loader; +use RuntimeException; use Symfony\Component\Yaml\Yaml; +use Throwable; /** * The Yaml file loader @@ -30,19 +32,19 @@ class YamlLoader extends AbstractLoader /** * {@inheritdoc} */ - public static $supported = array('yaml', 'yml'); + public static array $supported = array('yaml', 'yml'); /** * {@inheritdoc} * - * @throws \RuntimeException If `symfony/yaml` library is not installed or the file can not be parsed + * @throws RuntimeException If `symfony/yaml` library is not installed or the file can not be parsed */ public function load() { try { $this->content = Yaml::parse(file_get_contents($this->entity)); - } catch (\Exception $e) { - throw new \RuntimeException(sprintf( + } catch (Throwable $e) { + throw new RuntimeException(sprintf( "%s threw an exception: %s", $this->entity, $e diff --git a/src/Provider/Silex/VarsServiceProvider.php b/src/Provider/Silex/VarsServiceProvider.php index f063e97..7cd92bf 100644 --- a/src/Provider/Silex/VarsServiceProvider.php +++ b/src/Provider/Silex/VarsServiceProvider.php @@ -41,7 +41,7 @@ class VarsServiceProvider implements ServiceProviderInterface * * @var array */ - private $option_keys = array( + private array $option_keys = array( 'cache', 'cache_path', 'cache_expire', @@ -63,7 +63,7 @@ public function __construct($entity = null) /** * Registers the service provider, sets the user defined options and returns the vars instance * - * @param \Silex\Application $app The silex app + * @param Application $app The silex app */ public function register(Application $app) { @@ -91,11 +91,11 @@ public function register(Application $app) /** * Creates the defined options into a way that Vars can use * - * @param \Silex\Application $app The silex app + * @param Application $app The silex app * * @return array The created options */ - private function createOptions($app) + private function createOptions(Application $app): array { $options = array(); @@ -126,7 +126,7 @@ private function createOptions($app) * * @return array The keyed options */ - private function createKeyedOptions($options, $vars_options) + private function createKeyedOptions(array $options, array $vars_options): array { foreach ($this->option_keys as $option) { $options[$option] = (isset($vars_options[$option])) ? $vars_options[$option] : null; @@ -138,7 +138,7 @@ private function createKeyedOptions($options, $vars_options) /** * The silex service provider boot function * - * @param \Silex\Application $app The silex app + * @param Application $app The silex app * * @codeCoverageIgnore */ diff --git a/src/Resource/AbstractResource.php b/src/Resource/AbstractResource.php index d8cfcab..60b8524 100644 --- a/src/Resource/AbstractResource.php +++ b/src/Resource/AbstractResource.php @@ -18,19 +18,21 @@ namespace M1\Vars\Resource; +use ArrayAccess; + /** * Abstract Resource enables normal and dot notation array access on resources * * @since 0.1.0 */ -abstract class AbstractResource implements \ArrayAccess +abstract class AbstractResource implements ArrayAccess { /** * The resource content * * @var array */ - protected $content = array(); + protected array $content = array(); /** * Sets the resource contents @@ -39,7 +41,7 @@ abstract class AbstractResource implements \ArrayAccess * * @return $this */ - public function setContent($content) + public function setContent(array $content): AbstractResource { $this->content = $content; return $this; @@ -50,7 +52,7 @@ public function setContent($content) * * @return array The content */ - public function getContent() + public function getContent(): array { return $this->content; } @@ -64,7 +66,7 @@ public function getContent() */ public function offsetGet($key) { - return $this->internalGet($this->content, $key); + return $this->internalGet($this -> content, $key); } /** @@ -76,7 +78,7 @@ public function offsetGet($key) */ public function get($key) { - return $this->internalGet($this->content, $key); + return $this->internalGet($this -> content, $key); } /** @@ -84,11 +86,11 @@ public function get($key) * * @param array $array The array to use -- will always be $this->content * @param mixed $key The key to find the value for - * @param bool $exists Whether to return null or false dependant on the calling function + * @param bool $exists Whether to return null or false dependant on the calling function * * @return array|bool|null The resource key value */ - private function internalGet(array $array, $key, $exists = false) + private function internalGet(array $array, $key, bool $exists) { if (isset($array[$key])) { return (!$exists) ? $array[$key] : true; @@ -124,7 +126,7 @@ public function offsetSet($key, $value) * @param string $key The key to set the value for * @param string $value The value to set */ - public function set($key, $value) + public function set(string $key, string $value) { $this->internalSet($this->content, $key, $value); } @@ -136,12 +138,13 @@ public function set($key, $value) * @param mixed $key The key to set the value for * @param mixed $value The value to set * - * @return array Returns the modified array + * @return void Returns the modified array */ - private function internalSet(array &$array, $key, $value) + private function internalSet(array &$array, $key, $value): void { if (is_null($key)) { - return $array = $value; + $array = $value; + return; } $keys = explode('.', $key); @@ -158,7 +161,6 @@ private function internalSet(array &$array, $key, $value) $array[array_shift($keys)] = $value; - return $array; } /** @@ -170,7 +172,7 @@ private function internalSet(array &$array, $key, $value) */ public function offsetExists($key) { - return $this->internalGet($this->content, $key, true); + return $this->internalGet($this -> content, $key, true); } /** @@ -211,7 +213,7 @@ protected function internalUnset(array &$array, $key) * * @return bool Does the key exist */ - public function arrayKeyExists($key) + public function arrayKeyExists($key): bool { if (array_key_exists($key, $this->content)) { return true; diff --git a/src/Resource/FileResource.php b/src/Resource/FileResource.php index 27b9e06..ee41ae4 100644 --- a/src/Resource/FileResource.php +++ b/src/Resource/FileResource.php @@ -20,6 +20,7 @@ use M1\Vars\Traits\FileTrait; use M1\Vars\Traits\ResourceFlagsTrait; +use M1\Vars\Variables\VariableProvider; use Symfony\Component\Filesystem\Filesystem; /** @@ -44,36 +45,36 @@ class FileResource extends AbstractResource * * @var string */ - private $filename; + private string $filename; /** * The parent ResourceProvider * - * @var \M1\Vars\Resource\ResourceProvider + * @var ResourceProvider */ - private $provider; + private ResourceProvider $provider; /** * The raw content from the passed file * * @var mixed */ - private $raw_content = array(); + private $raw_content; /** * The VariableProvider * - * @var \M1\Vars\Variables\VariableProvider + * @var VariableProvider */ - private $variables; + private VariableProvider $variables; /** * The file resource constructor to get and parse the content from files * - * @param \M1\Vars\Resource\ResourceProvider $provider The parent ResourceProvider - * @param string $file The passed file + * @param ResourceProvider $provider The parent ResourceProvider + * @param string $file The passed file */ - public function __construct(ResourceProvider $provider, $file) + public function __construct(ResourceProvider $provider, string $file) { $this->provider = $provider; $this->vars = $provider->vars; @@ -97,7 +98,7 @@ public function __construct(ResourceProvider $provider, $file) * * @param string $file The passed file */ - private function makePaths($file) + private function makePaths(string $file) { $file = realpath($file); @@ -121,7 +122,7 @@ private function makePaths($file) * * @return array Returns the parsed content */ - private function searchForResources($content = array(), $prefix = '') + private function searchForResources($content = array(), string $prefix = ''): array { $returned_content = array(); @@ -138,14 +139,14 @@ private function searchForResources($content = array(), $prefix = '') * * @param mixed $key The key of the content array * @param mixed $value The value of the key - * @param array $returned_content The modified content array to return + * @param array $returned_content The modified content array to return * @param string $prefix The array prefix for the entity * * @return array Returns the modified content array */ - private function parseContent($key, $value, $returned_content, $prefix) + private function parseContent($key, $value, array $returned_content, string $prefix): array { - if ($key === 'imports' && !is_null($value) && !empty($value)) { + if ($key === 'imports' && !empty($value)) { $imported_resource = $this->useImports($value); if ($imported_resource) { @@ -172,7 +173,7 @@ private function parseContent($key, $value, $returned_content, $prefix) * * @return string|null The parsed string */ - private function parseText($text) + private function parseText(string $text): ?string { if (is_string($text)) { return $this->variables->parse($text); @@ -188,7 +189,7 @@ private function parseText($text) * * @return array The parsed imported resources */ - private function useImports($imports) + private function useImports($imports): array { $imported_resources = array(); @@ -211,7 +212,7 @@ private function useImports($imports) * * @return array The parsed imported resources */ - private function processImport($import, array $imported_resources) + private function processImport($import, array $imported_resources): array { if (is_array($import) && array_key_exists('resource', $import) && is_array($import['resource'])) { foreach ($import['resource'] as $resource) { @@ -238,7 +239,7 @@ private function processImport($import, array $imported_resources) * * @return array The imported resources */ - private function import2Resource($import, array $imported_resources) + private function import2Resource($import, array $imported_resources): array { $resource = $this->createResource($import); @@ -254,9 +255,9 @@ private function import2Resource($import, array $imported_resources) * * @param array|string $import The import to create a resource from * - * @return \M1\Vars\Resource\ResourceProvider The resource of the import + * @return ResourceProvider The resource of the import */ - private function createResource($import) + private function createResource($import): ResourceProvider { if (is_array($import) && array_key_exists('resource', $import)) { $import_resource = $import; @@ -266,14 +267,12 @@ private function createResource($import) $import_resource = array('resource' => $import, 'relative' => true, 'recursive' => true); } - $import_resource = new ResourceProvider( - $this->provider->vars, - $this->createImportName($import_resource['resource']), + return new ResourceProvider( + $this -> provider -> vars, + $this -> createImportName($import_resource['resource']), $import_resource['relative'], $import_resource['recursive'] ); - - return $import_resource; } /** @@ -283,11 +282,11 @@ private function createResource($import) * * @return string The parsed resource */ - private function createImportName($resource) + private function createImportName(string $resource): string { $resource = $this->explodeResourceIfElse($resource); $resource_pieces = array(); - + foreach ($resource as $r) { $parsed_r = $this->trimFlags($r); $parsed_r = sprintf('%s/%s', dirname($this->file), $parsed_r); @@ -303,11 +302,11 @@ private function createImportName($resource) * Import resource into the imported resources and merge contents * * @param ResourceProvider $provider The new imported resource - * @param array $imported_resources The imported resources + * @param array $imported_resources The imported resources * * @return array The modified imported resources */ - private function importResource(ResourceProvider $provider, $imported_resources) + private function importResource(ResourceProvider $provider, array $imported_resources): array { $content = $provider->getContent(); $parent_content = $provider->getParentContent(); @@ -330,7 +329,7 @@ private function importResource(ResourceProvider $provider, $imported_resources) * * @return bool Is the passed array associative */ - private function isAssoc(array $array) + private function isAssoc(array $array): bool { return array_keys($array) !== range(0, count($array) - 1); } @@ -343,7 +342,7 @@ private function isAssoc(array $array) * * @return bool Returns the value of the boolean */ - public function checkBooleanValue($value, $import) + public function checkBooleanValue(string $value, $import): bool { $default = false; @@ -363,7 +362,7 @@ public function checkBooleanValue($value, $import) * * @return bool Returns the value of the boolean */ - private function getBooleanValue($value) + private function getBooleanValue(string $value): bool { $value = strtolower($value); @@ -379,7 +378,7 @@ private function getBooleanValue($value) * * @return string The filename */ - public function getFilename() + public function getFilename(): string { return $this->filename; } diff --git a/src/Resource/ResourceProvider.php b/src/Resource/ResourceProvider.php index 8b447e5..532af2e 100644 --- a/src/Resource/ResourceProvider.php +++ b/src/Resource/ResourceProvider.php @@ -18,6 +18,7 @@ namespace M1\Vars\Resource; +use InvalidArgumentException; use M1\Vars\Loader\DirectoryLoader; use M1\Vars\Traits\ResourceFlagsTrait; use M1\Vars\Vars; @@ -43,50 +44,50 @@ class ResourceProvider extends AbstractResource * * @var array */ - private $parent_content = array(); + private array $parent_content = array(); /** * Are dirs wanting to be recursively searched * * @var bool */ - private $recursive; + private bool $recursive; /** * Is the import relative * * @var bool */ - private $relative; + private bool $relative; /** * Suppress file not found exceptions * * @var bool */ - private $suppress_file_exceptions = false; + private bool $suppress_file_exceptions = false; /** * The parent Vars class * - * @var \M1\Vars\Vars + * @var Vars */ - public $vars; + public Vars $vars; /** * The ResourceProvider constructor creates the content from the entity * - * @param \M1\Vars\Vars $vars The calling Vars class + * @param Vars $vars The calling Vars class * @param string|array $entity The configuration entity - * @param bool $relative Is the entity relative to the calling entity or class - * @param bool $recursive If entity a dir, do you want to recursively check directories + * @param bool $relative Is the entity relative to the calling entity or class + * @param bool $recursive If entity a dir, do you want to recursively check directories * - * @throws \InvalidArgumentException If the entity passed is not a string or array + * @throws InvalidArgumentException If the entity passed is not a string or array */ - public function __construct(Vars $vars, $entity, $relative = true, $recursive = false) + public function __construct(Vars $vars, $entity, bool $relative = true, bool $recursive) { if (!is_string($entity) && !is_array($entity)) { - throw new \InvalidArgumentException('You can only pass strings or arrays as Resources'); + throw new InvalidArgumentException('You can only pass strings or arrays as Resources'); } $this->vars = $vars; @@ -110,7 +111,7 @@ public function __construct(Vars $vars, $entity, $relative = true, $recursive = * @param array $resources The array of resources * @param string $type The type of the resource */ - private function createResources(array $resources, $type) + private function createResources(array $resources, string $type) { foreach ($resources as $resource) { if ($type === "string") { @@ -128,7 +129,7 @@ private function createResources(array $resources, $type) $resource = new FileResource($this, $resource); $this->vars->updateResource($resource, $pos); } else { - $resource = new ResourceProvider($this->vars, $resource); + $resource = new ResourceProvider($this -> vars, $resource); } $this->addContent($resource->getContent()); @@ -139,13 +140,13 @@ private function createResources(array $resources, $type) * Creates the content from the entity * * @param string|array $entity The configuration entity - * @param string $type The type of entity + * @param string $type The type of entity * - * @throws \InvalidArgumentException If the entity is not array|file, is readable or exists + * @throws InvalidArgumentException If the entity is not array|file, is readable or exists * * @returns array The array of resources */ - private function processEntity($entity, $type) + private function processEntity($entity, string $type) { $resources = $entity; @@ -159,7 +160,7 @@ private function processEntity($entity, $type) } elseif ($this->suppress_file_exceptions) { $resources = false; } else { - throw new \InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity)); + throw new InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity)); } } @@ -173,7 +174,7 @@ private function processEntity($entity, $type) * * @returns string The parsed entity */ - private function parseEntity($entity) + private function parseEntity(string $entity): string { $files = $this->explodeResourceIfElse($entity); @@ -195,7 +196,7 @@ private function parseEntity($entity) * * @param array $content The content from the resource */ - private function addContent($content) + private function addContent(array $content) { if ($this->relative) { $this->content = $this->mergeContents($this->content, $content); @@ -207,14 +208,14 @@ private function addContent($content) /** * Returns the supported files using the extensions from the loaders in the entity which is a directory * - * @see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions() - * @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders() - * * @param string $entity The resource entity * * @return array|bool Returns the supported files or false if no files were found + *@see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions() + * @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders() + * */ - private function getSupportedFilesInDir($entity) + private function getSupportedFilesInDir(string $entity) { $dir_loader = new DirectoryLoader($entity, $this->recursive); $dir_loader->setSupports($this->vars->loader->getExtensions()); @@ -228,7 +229,7 @@ private function getSupportedFilesInDir($entity) * * @return array The merged contents */ - private function mergeContents() + private function mergeContents(): array { $contents = func_get_args(); return call_user_func_array('array_replace_recursive', $contents); @@ -247,9 +248,9 @@ public function addParentContent(array $content) /** * Merges the content and the parent content together * - * @return \M1\Vars\Resource\ResourceProvider + * @return ResourceProvider */ - public function mergeParentContent() + public function mergeParentContent(): ResourceProvider { $this->content = $this->mergeContents($this->content, $this->parent_content); @@ -259,7 +260,7 @@ public function mergeParentContent() /** * Returns the parent content * - * @return mixed The parent content + * @return array The parent content */ public function getParentContent() { diff --git a/src/Traits/FileTrait.php b/src/Traits/FileTrait.php index 498f400..ac4b463 100644 --- a/src/Traits/FileTrait.php +++ b/src/Traits/FileTrait.php @@ -18,6 +18,12 @@ namespace M1\Vars\Traits; +use InvalidArgumentException; +use InvalidArugmentException; +use M1\Vars\Vars; +use ReflectionClass; +use RuntimeException; + /** * File trait gives common operation functions needed for files in Vars * @@ -30,32 +36,32 @@ trait FileTrait * * @var string */ - private $file; + private string $file; /** * The parent Vars instance * - * @var \M1\Vars\Vars + * @var Vars */ - private $vars; + private Vars $vars; /** * Validates the file passed to see if it is a file and is readable * - * @throws \RuntimeException If the file passed is not a file - * @throws \RuntimeException If the file passed is not readable + * @throws RuntimeException If the file passed is not a file + * @throws RuntimeException If the file passed is not readable */ private function validate() { $file = $this->file; if (!is_file($file)) { - throw new \RuntimeException(sprintf("'%s' is not a file", $file)); + throw new RuntimeException(sprintf("'%s' is not a file", $file)); } if (!is_readable($file)) { // @codeCoverageIgnoreStart - throw new \RuntimeException(sprintf("'%s' is not a readable file", $file)); + throw new RuntimeException(sprintf("'%s' is not a readable file", $file)); // @codeCoverageIgnoreEnd } } @@ -63,19 +69,27 @@ private function validate() /** * Gets the supported loader for the passed file * - * @see \M1\Vars\Vars::getLoaders() \M1\Vars\Vars::getLoaders() - * * @param string $data The passed file * * @return mixed Returns the loader class or false if no loader calls was found + * @throws \ReflectionException + * @throws \ReflectionException + * @see \M1\Vars\Vars::getLoaders() \M1\Vars\Vars::getLoaders() + * */ - private function getSupportedLoader($data) + private function getSupportedLoader(string $data) { $loaders = $this->vars->loader->getLoaders(); foreach ($loaders as $loader) { - $class_loader = new \ReflectionClass($loader); - $class_loader = $class_loader->newInstanceArgs(array($data)); + try { + $class_loader = new ReflectionClass($loader); + } catch (\ReflectionException $e) { + } + try { + $class_loader = $class_loader -> newInstanceArgs(array($data)); + } catch (\ReflectionException $e) { + } if ($class_loader->supports()) { return $class_loader; @@ -89,16 +103,16 @@ private function getSupportedLoader($data) * * @param string $data The passed file * - * @throws \InvalidArugmentException If the file passed is not supported by the current loaders - * * @return mixed The content from the file via the loader + *@throws InvalidArugmentException If the file passed is not supported by the current loaders + * */ - private function loadContent($data) + private function loadContent(string $data) { $loader = $this->getSupportedLoader($data); if (!$loader) { - throw new \InvalidArgumentException(sprintf("'%s' is not supported by the current loaders", $this->file)); + throw new InvalidArgumentException(sprintf("'%s' is not supported by the current loaders", $this->file)); } $loader->load(); @@ -110,7 +124,7 @@ private function loadContent($data) * * @return string The passed file */ - public function getFile() + public function getFile(): string { return $this->file; } diff --git a/src/Traits/PathTrait.php b/src/Traits/PathTrait.php index ade2408..25bfb1e 100644 --- a/src/Traits/PathTrait.php +++ b/src/Traits/PathTrait.php @@ -1,4 +1,4 @@ -path; } @@ -45,21 +48,20 @@ public function getPath() /** * Set the Vars base path * - * @param string $path The path to set + * @param string $path The path to set * @param boolean $check_writeable Check whether dir is writeable - - * @throws \InvalidArgumentException If the path does not exist or is not writable + * @return Vars|void + *@throws InvalidArgumentException If the path does not exist or is not writable * - * @return \M1\Vars\Vars */ - public function setPath($path, $check_writeable = false) + public function setPath(string $path, bool $check_writeable): Vars { if (is_null($path)) { return; } if (!is_dir($path) || ($check_writeable && !is_writable($path))) { - throw new \InvalidArgumentException(sprintf( + throw new InvalidArgumentException(sprintf( "'%s' base path does not exist or is not writable", $path )); diff --git a/src/Traits/ResourceFlagsTrait.php b/src/Traits/ResourceFlagsTrait.php index b59e0fe..8e77dce 100644 --- a/src/Traits/ResourceFlagsTrait.php +++ b/src/Traits/ResourceFlagsTrait.php @@ -32,7 +32,7 @@ trait ResourceFlagsTrait * * @return array The parsed resources */ - protected function explodeResourceIfElse($resource) + protected function explodeResourceIfElse(string $resource): array { $resource = explode("?:", $resource, 2); $resources = array(); @@ -51,7 +51,7 @@ protected function explodeResourceIfElse($resource) * * @return string The parsed string resource */ - protected function implodeResourceIfElse($resource) + protected function implodeResourceIfElse(array $resource): string { if (count($resource) > 1) { return implode(" ?: ", $resource); @@ -67,23 +67,20 @@ protected function implodeResourceIfElse($resource) * * @return string The parsed string resource */ - protected function trimFlags($resource) + protected function trimFlags(string $resource): string { $resource = trim($resource, '@'); - $resource = trim($resource, '*'); - - return $resource; + return trim($resource, '*'); } /** * Duplicates the flags from one string to another * - * @param string $resource The resource to add the flags to * @param string $resource The resource to duplicate the flags from - * + * @param $original_resource * @return string The parsed string resource */ - protected function replicateFlags($resource, $original_resource) + protected function replicateFlags(string $resource, $original_resource): string { return sprintf( '%s%s%s', @@ -100,7 +97,7 @@ protected function replicateFlags($resource, $original_resource) * * @return bool Is the recursive flag set */ - private function checkRecursive($resource) + private function checkRecursive(string $resource): bool { return substr($resource, -1) === "*"; } @@ -112,7 +109,7 @@ private function checkRecursive($resource) * * @return bool Is the exception suppressed */ - private function checkSuppression($resource) + private function checkSuppression(string $resource): bool { return substr($resource, 0, 1) === "@"; } diff --git a/src/Traits/TransformerTrait.php b/src/Traits/TransformerTrait.php index d7f25e3..c0e2a1e 100644 --- a/src/Traits/TransformerTrait.php +++ b/src/Traits/TransformerTrait.php @@ -46,7 +46,7 @@ public function toEnv() * * @return array The dot notation array */ - public function toDots($flatten_array = true) + public function toDots(bool $flatten_array = true): array { return (!is_null($this->content)) ? $this->dotTransformer($this->content, $flatten_array) : $this->content; } @@ -54,13 +54,13 @@ public function toDots($flatten_array = true) /** * Converts the array into a flat dot notation array * - * @param array $content The content array - * @param bool $flatten_array Flatten arrays into none existent keys + * @param array $content The content array + * @param bool $flatten_array Flatten arrays into none existent keys * @param string $prefix The prefix for the key * * @return array The dot notation array */ - private function dotTransformer($content, $flatten_array, $prefix = '') + private function dotTransformer(array $content, bool $flatten_array, string $prefix = ''): array { $parsed = array(); foreach ($content as $arr_k => $arr_v) { diff --git a/src/Variables/ReplacementStore.php b/src/Variables/ReplacementStore.php index 57d0b84..739aa8a 100644 --- a/src/Variables/ReplacementStore.php +++ b/src/Variables/ReplacementStore.php @@ -1,4 +1,4 @@ -vars = $vars; } @@ -47,19 +49,18 @@ public function __construct($vars) * Creates the replacement variable content from a file or array * * @param array $replacements The replacements - - * @throws \RuntimeException If variable data is not array or a file + * @return void The replacement variables + *@throws RuntimeException If variable data is not array or a file * - * @return array The replacement variables */ - public function load($replacements) + public function load(array $replacements) { if (is_array($replacements)) { $variables = $replacements; - } elseif (is_file($replacements)) { - $variables = $this->loadContent($replacements); + } elseif (is_file((string)$replacements)) { + $variables = $this->loadContent((string)$replacements); } else { - throw new \RuntimeException('Config replacements must be a array or a file'); + throw new RuntimeException('Config replacements must be a array or a file'); } $this->content = $variables; diff --git a/src/Variables/VariableProvider.php b/src/Variables/VariableProvider.php index 750c061..ab8af06 100644 --- a/src/Variables/VariableProvider.php +++ b/src/Variables/VariableProvider.php @@ -18,6 +18,9 @@ namespace M1\Vars\Variables; +use InvalidArgumentException; +use M1\Vars\Vars; + /** * Vars variable file resource for getting replacement variables from files or arrays * @@ -52,28 +55,28 @@ class VariableProvider * * @var array $variable_types */ - private static $variable_types = array('replacement', 'variable', 'env'); + private static array $variable_types = array('replacement', 'variable', 'env'); /** * The replacement store * - * @var \M1\Vars\Variables\ReplacementStore $rstore + * @var ReplacementStore $rstore */ - public $rstore; + public ReplacementStore $rstore; /** * The variable store * - * @var \M1\Vars\Variables\VariableStore $vstore + * @var VariableStore $vstore */ - public $vstore; + public VariableStore $vstore; /** * Creates new instance of VariableProvider * - * @param \M1\Vars\Vars $vars Instance of the calling Vars + * @param Vars $vars Instance of the calling Vars */ - public function __construct($vars) + public function __construct(Vars $vars) { $this->vstore = new VariableStore(); $this->rstore = new ReplacementStore($vars); @@ -86,7 +89,7 @@ public function __construct($vars) * * @return string The parsed variable */ - public function parse($value) + public function parse(string $value): string { foreach (self::$variable_types as $variable_type) { $value = $this->typeParse($value, $variable_type); @@ -103,7 +106,7 @@ public function parse($value) * * @return string The parsed variable */ - private function typeParse($value, $type) + private function typeParse(string $value, string $type): string { $const_str = sprintf('REGEX_%s_SYNTAX', strtoupper($type)); $regex = constant('\M1\Vars\Variables\VariableProvider::'.$const_str); @@ -126,9 +129,9 @@ private function typeParse($value, $type) * @param string $value The string to fetch matches for * @param string $regex The variable type regex * - * @return array The matches + * @return array|false */ - private function fetchVariableMatches($value, $regex) + private function fetchVariableMatches(string $value, string $regex) { preg_match_all('/' . $regex . '/', $value, $matches); @@ -147,7 +150,7 @@ private function fetchVariableMatches($value, $regex) * * @return string The fetches value for the variable */ - private function fetchVariable($variable_name, $type) + private function fetchVariable(string $variable_name, string $type): string { $this->checkVariableExists($variable_name, $type); @@ -166,34 +169,33 @@ private function fetchVariable($variable_name, $type) * @param string $variable The variable to check * @param string $type The variable type * - * @throws \InvalidArgumentException If the variable does not exist + * @return void Does the variable exist + *@throws InvalidArgumentException If the variable does not exist * - * @return bool Does the variable exist */ - private function checkVariableExists($variable, $type) + private function checkVariableExists(string $variable, string $type): void { if (($type === 'env' && !getenv($variable)) || ($type === 'replacement' && !$this->rstore->arrayKeyExists($variable)) || ($type === 'variable' && !$this->vstore->arrayKeyExists($variable)) ) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf('Variable has not been defined as a `%s`: %s', $variable, $type) ); } - return true; } /** * Does the replacements in the string for the variable * * @param string $value The string to parse - * @param array $matches The matches + * @param array $matches The matches * @param string $type The variable type * * @return string The parsed variable */ - public function doReplacements($value, $matches, $type) + public function doReplacements(string $value, array $matches, string $type): string { $replacements = array(); for ($i = 0; $i <= (count($matches[0]) - 1); $i++) { diff --git a/src/Variables/VariableStore.php b/src/Variables/VariableStore.php index b1c9475..a26cd4c 100644 --- a/src/Variables/VariableStore.php +++ b/src/Variables/VariableStore.php @@ -18,7 +18,7 @@ namespace M1\Vars\Variables; -use \M1\Vars\Resource\AbstractResource; +use M1\Vars\Resource\AbstractResource; /** * Stores the in-file variables @@ -32,21 +32,21 @@ class VariableStore extends AbstractResource * * @var string $current_prefix */ - private $current_prefix; + private string $current_prefix; /** * The relative prefix for the variable store * * @var string $path */ - private $prefix; + private string $prefix; /** * Creates a relative prefix for the store * * @param bool $relative Is the prefix relative */ - public function createPrefix($relative) + public function createPrefix(bool $relative) { if ($relative) { $this->prefix = (empty($this->current_prefix)) ? '' : $this->current_prefix; @@ -64,7 +64,7 @@ public function createPrefix($relative) * * @return string The new prefix */ - public function createPrefixName($prefix, $key) + public function createPrefixName(string $prefix, string $key): string { return $prefix.$key.'.'; } @@ -74,7 +74,7 @@ public function createPrefixName($prefix, $key) * * @return string The prefix */ - public function getPrefix() + public function getPrefix(): string { return $this->prefix; } @@ -84,7 +84,7 @@ public function getPrefix() * * @param string $prefix The new prefix */ - public function setCurrentPrefix($prefix) + public function setCurrentPrefix(string $prefix) { $this->current_prefix = $prefix; } diff --git a/src/Vars.php b/src/Vars.php index f3464ca..bff539d 100644 --- a/src/Vars.php +++ b/src/Vars.php @@ -21,6 +21,7 @@ use M1\Vars\Cache\CacheProvider; use M1\Vars\Loader\LoaderProvider; use M1\Vars\Resource\AbstractResource; +use M1\Vars\Resource\FileResource; use M1\Vars\Resource\ResourceProvider; use M1\Vars\Traits\PathTrait; use M1\Vars\Traits\TransformerTrait; @@ -46,16 +47,16 @@ class Vars extends AbstractResource /** * The cache object if the cache is wanted, else false * - * @var \M1\Vars\Cache\CacheProvider $cache + * @var CacheProvider $cache */ - public $cache; + public CacheProvider $cache; /** * The default options for Vars * * @var array $default_options */ - private $default_options = array( + private array $default_options = array( 'path' => null, 'cache' => true, 'cache_path' => null, @@ -69,43 +70,43 @@ class Vars extends AbstractResource * * @var array globals */ - private $globals = array(); + private array $globals = array(); /** * The loaderProvider for Vars supplies the file loaders and the extensions that are supported * - * @var \M1\Vars\Loader\LoaderProvider $loader + * @var LoaderProvider $loader */ - public $loader; + public LoaderProvider $loader; /** * Have the base and cache paths been set * * @var bool $paths_loaded */ - private $paths_loaded = false; + private bool $paths_loaded = false; /** * The imported resources * * @var array $resources */ - private $resources = array(); + private array $resources = array(); /** * The variable provider * - * @var \M1\Vars\Variables\VariableProvider $variables + * @var VariableProvider $variables */ - public $variables; + public VariableProvider $variables; /** * Creates a new instance of Vars * * @param string|array $resource The main configuration resource - * @param array $options The options being used for Vars + * @param array $options The options being used for Vars */ - public function __construct($resource, $options = array()) + public function __construct($resource, array $options = array()) { $options = $this->parseOptions($options); $this->makeCache($options, $resource); @@ -134,7 +135,7 @@ public function __construct($resource, $options = array()) * * @return array The parsed options */ - private function parseOptions(array $options) + private function parseOptions(array $options): array { $parsed_options = array_merge($this->default_options, $options); $parsed_options['loaders'] = (isset($options['loaders'])) ? @@ -146,10 +147,10 @@ private function parseOptions(array $options) /** * Makes the CacheProvider with the options * - * @param array $options The options being used for Vars + * @param array $options The options being used for Vars * @param array|string $resource The main configuration resource */ - private function makeCache($options, $resource) + private function makeCache(array $options, $resource) { $cache = new CacheProvider($resource, $options); $this->cache = $cache; @@ -161,7 +162,7 @@ private function makeCache($options, $resource) * * @param array $options The options being used for Vars */ - private function makePaths($options) + private function makePaths(array $options) { $this->setPath($options['path']); @@ -176,7 +177,7 @@ private function makePaths($options) * * @param array $options The options being used for Vars */ - private function makeLoader($options) + private function makeLoader(array $options) { $loader = new LoaderProvider($options, $this->default_options['loaders']); $this->loader = $loader; @@ -187,7 +188,7 @@ private function makeLoader($options) * * @param array|null $options The options being used for Vars */ - private function makeVariables($options) + private function makeVariables(?array $options) { $this->variables = new VariableProvider($this); @@ -229,7 +230,7 @@ private function loadFromCache() * * @param string $resource The resource to use to set the paths if they haven't been set */ - public function pathsLoadedCheck($resource) + public function pathsLoadedCheck(string $resource) { if (!$this->paths_loaded) { $path = $this->getPath(); @@ -257,7 +258,7 @@ public function pathsLoadedCheck($resource) * * @return array $content The parsed content */ - private function mergeGlobals($content, $options) + private function mergeGlobals(array $content, array $options): array { if (array_key_exists('_globals', $content)) { $this->globals = $content['_globals']; @@ -279,7 +280,7 @@ private function mergeGlobals($content, $options) * * @return int The position of the added resource */ - public function addResource($resource) + public function addResource(string $resource): int { $r = realpath($resource); $pos = count($this->resources); @@ -290,12 +291,12 @@ public function addResource($resource) /** * Updates the string resource with the FileResource * - * @param \M1\Vars\Resource\FileResource $resource The FileResource to add - * @param int $pos The position of the string resource + * @param FileResource $resource The FileResource to add + * @param int $pos The position of the string resource * - * @return \M1\Vars\Vars + * @return Vars */ - public function updateResource($resource, $pos) + public function updateResource(FileResource $resource, int $pos): Vars { $this->resources[$pos] = $resource; return $this; @@ -304,11 +305,11 @@ public function updateResource($resource, $pos) /** * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop * - * @param \M1\Vars\Resource\FileResource|string $resource Resource to check + * @param FileResource|string $resource Resource to check * * @return bool Has resource already been imported */ - public function resourceImported($resource) + public function resourceImported($resource): bool { $resource = realpath($resource); foreach ($this->getResources() as $r) { @@ -325,9 +326,9 @@ public function resourceImported($resource) * * @param string $resource The resource to search for * - * @return \M1\Vars\Resource\FileResource|bool Returns the resource if found + * @return FileResource|bool Returns the resource if found */ - public function getResource($resource) + public function getResource(string $resource) { foreach ($this->getResources() as $r) { if ($resource === $r->getFilename()) { @@ -343,7 +344,7 @@ public function getResource($resource) * * @return array The Vars imported resources */ - public function getResources() + public function getResources(): array { return $this->resources; } @@ -353,7 +354,7 @@ public function getResources() * * @return array The Vars imported resources */ - public function getGlobals() + public function getGlobals(): array { return $this->globals; } @@ -361,9 +362,9 @@ public function getGlobals() /** * Returns the CacheProvider if set * - * @return \M1\Vars\Cache\CacheProvider The CacheProvider + * @return CacheProvider The CacheProvider */ - public function getCache() + public function getCache(): CacheProvider { return $this->cache; } diff --git a/tests/Plugin/FakeFileResource.php b/tests/Plugin/FakeFileResource.php index ccdf67f..6242cff 100644 --- a/tests/Plugin/FakeFileResource.php +++ b/tests/Plugin/FakeFileResource.php @@ -8,7 +8,7 @@ namespace M1\Vars\Test\Plugin; -use \M1\Vars\Traits\FileTrait; +use M1\Vars\Traits\FileTrait; class FakeFileResource { diff --git a/tests/Plugin/TextLoader.php b/tests/Plugin/TextLoader.php index cf812d1..96c11c4 100644 --- a/tests/Plugin/TextLoader.php +++ b/tests/Plugin/TextLoader.php @@ -6,7 +6,7 @@ class TextLoader extends AbstractLoader { - public static $supported = array('txt'); + public static array $supported = array('txt'); public function load() { diff --git a/tests/Plugin/TextNoExtensionLoader.php b/tests/Plugin/TextNoExtensionLoader.php index 3387f72..bc51c25 100644 --- a/tests/Plugin/TextNoExtensionLoader.php +++ b/tests/Plugin/TextNoExtensionLoader.php @@ -6,7 +6,8 @@ class TextNoExtensionLoader extends AbstractLoader { - public static $supported = array(); + public static array $supported = array(); + private $file; public function load() { diff --git a/tests/VarsTest.php b/tests/VarsTest.php index df69726..31571d2 100644 --- a/tests/VarsTest.php +++ b/tests/VarsTest.php @@ -2,11 +2,30 @@ namespace M1\Vars\Test; +use InvalidArgumentException; +use M1\Vars\Provider\Silex\VarsServiceProvider; +use M1\Vars\Test\Plugin\FakeFileResource; use M1\Vars\Vars; use PHPUnit\Framework\TestCase; +use RuntimeException; +use Silex\Application; +use stdClass; class VarsTest extends TestCase { + /** + * @var string[] + */ + private array $default_loaders_namespace; + /** + * @var string[] + */ + private array $default_loaders; + /** + * @var string[] + */ + private array $basic_array; + public function setUp() { $this->basic_array = array( @@ -1052,9 +1071,9 @@ public function testGlobalsMergeFalse() public function testBasicSilexServiceProvider() { - $app = new \Silex\Application(); + $app = new Application(); - $app->register(new \M1\Vars\Provider\Silex\VarsServiceProvider(__DIR__ . '/mocks/basic/test_pass_1.yml'), array( + $app->register(new VarsServiceProvider(__DIR__ . '/mocks/basic/test_pass_1.yml'), array( 'vars.options' => array( 'cache' => false, ), @@ -1072,10 +1091,10 @@ public function testOptionsSilexServiceProvider() $cache_path = __DIR__ . '/mocks/cache/output'; $cache_expire = 1000; - $app = new \Silex\Application(); + $app = new Application(); $app->register( - new \M1\Vars\Provider\Silex\VarsServiceProvider($resource), + new VarsServiceProvider($resource), array( 'vars.path' => $path, 'vars.options' => array( @@ -1101,9 +1120,9 @@ public function testOptionsSilexServiceProvider() public function testSilexGlobalsMerge() { - $app = new \Silex\Application(); + $app = new Application(); - $app->register(new \M1\Vars\Provider\Silex\VarsServiceProvider(__DIR__ . '/mocks/globals/globals.yml'), array( + $app->register(new VarsServiceProvider(__DIR__ . '/mocks/globals/globals.yml'), array( 'vars.options' => array( 'cache' => false, ), @@ -1118,11 +1137,11 @@ public function testDebugSilexServiceProvider() { $resource = __DIR__ . '/mocks/provider/test_1.yml'; - $app = new \Silex\Application(); + $app = new Application(); $app['debug'] = true; $app->register( - new \M1\Vars\Provider\Silex\VarsServiceProvider($resource), + new VarsServiceProvider($resource), array() ); @@ -1151,19 +1170,15 @@ public function testToEnvTransformer() $this->assertEquals("value", getenv('test_key_4')); } - /** - * @expectedException \InvalidArgumentException - */ public function testInvalidResource() { - $vars = new Vars(new \stdClass()); + $this -> expectException(InvalidArgumentException::class); + $vars = new Vars(new stdClass()); } - /** - * @expectedException \InvalidArgumentException - */ public function testInvalidLoader() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars(__DIR__ . '/mocks/basic/test_pass_1.yml', array( 'cache' => false, 'loaders' => 'Foo\Bar\FooBarLoader', @@ -1172,11 +1187,9 @@ public function testInvalidLoader() $this->assertEquals($this->basic_array, $vars->getContent()); } - /** - * @expectedException \RuntimeException - */ public function testNoLoaderExtensions() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/loader/test.txt', array( @@ -1186,11 +1199,9 @@ public function testNoLoaderExtensions() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNoLoaderForExtension() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars(__DIR__ . '/mocks/basic/test_pass_1.yml', array( 'cache' => false, 'loaders' => array( @@ -1199,12 +1210,9 @@ public function testNoLoaderForExtension() )); } - /** - * - * @expectedException \RuntimeException - */ public function testBasicInvalidYML() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/basic/test_fail_1.yml', array( @@ -1215,13 +1223,14 @@ public function testBasicInvalidYML() /** * @requires PHP 5.5 - * @expectedException \RuntimeException + * */ public function testBasicInvalidIni() { + $this -> expectException(RuntimeException::class); // hack for hhvm if (defined('HHVM_VERSION')) { - throw new \RuntimeException(); + throw new RuntimeException(); return; } @@ -1233,11 +1242,9 @@ public function testBasicInvalidIni() ); } - /** - * @expectedException \RuntimeException - */ public function testBasicInvalidXml() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/basic/test_fail_1.xml', array( @@ -1246,11 +1253,9 @@ public function testBasicInvalidXml() ); } - /** - * @expectedException \RuntimeException - */ public function testBasicInvalidJson() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/basic/test_fail_1.json', array( @@ -1259,11 +1264,9 @@ public function testBasicInvalidJson() ); } - /** - * @expectedException \RuntimeException - */ public function testBasicInvalidPhp() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/basic/test_fail_1.php', array( @@ -1272,11 +1275,9 @@ public function testBasicInvalidPhp() ); } - /** - * @expectedException \RuntimeException - */ public function testBasicInvalidToml() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/basic/test_fail_1.toml', array( @@ -1285,11 +1286,9 @@ public function testBasicInvalidToml() ); } - /** - * @expectedException \RuntimeException - */ public function testBasicInvalidEnv() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/basic/test_fail_1.env', array( @@ -1298,11 +1297,9 @@ public function testBasicInvalidEnv() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNonexistentFile() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/FILE_NON_EXISTENT.php', array( @@ -1311,11 +1308,9 @@ public function testNonexistentFile() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNonexistentImportedFile() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/FILE_NON_EXISTENT.php', array( @@ -1324,11 +1319,9 @@ public function testNonexistentImportedFile() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNonexistentImportedIfElseFile() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/importing/ifelse_5.yml', array( @@ -1337,11 +1330,9 @@ public function testNonexistentImportedIfElseFile() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNonexistentFolder() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/FOLDER_NON_EXISTENT/', array( @@ -1350,11 +1341,9 @@ public function testNonexistentFolder() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNoneExistentFolderImport() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/importing/dir_fail_1.yml', array( @@ -1363,11 +1352,9 @@ public function testNoneExistentFolderImport() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNoneExistentBasePath() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/importing/dir_fail_1.yml', array( @@ -1377,11 +1364,9 @@ public function testNoneExistentBasePath() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNoneExistentCachePath() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/importing/dir_fail_1.yml', array( @@ -1391,11 +1376,9 @@ public function testNoneExistentCachePath() ); } - /** - * @expectedException \RuntimeException - */ public function testDoReplacementVariablesFromNonExistentFile() { + $this -> expectException(RuntimeException::class); $vars = new Vars( __DIR__ . '/mocks/variables/basic_1.yml', array( @@ -1405,11 +1388,9 @@ public function testDoReplacementVariablesFromNonExistentFile() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testNonExistentVariable() { + $this -> expectException(InvalidArgumentException::class); $vars = new Vars( __DIR__ . '/mocks/variables/fail_1.yml', array( @@ -1418,11 +1399,9 @@ public function testNonExistentVariable() ); } - /** - * @expectedException \RuntimeException - */ public function testFileTraitValidate() { - $fake_file_resource = new \M1\Vars\Test\Plugin\FakeFileResource(__DIR__ . '/mocks/FAKE_FILE'); + $this -> expectException(RuntimeException::class); + $fake_file_resource = new FakeFileResource(__DIR__ . '/mocks/FAKE_FILE'); } }