From b63e95932a1afad953de21ac2459c97d65dbcc25 Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Sat, 20 Jul 2019 22:44:02 +0200 Subject: [PATCH 1/9] Add check for empty environmental variables --- config/config.php | 2 + phpunit.xml.dist | 3 + src/Checks/EnvVariablesExists.php | 132 +++++++++++++++++ tests/EnvVarsExistsTest.php | 29 ++++ translations/de/checks.php | 222 ++++++++++++++-------------- translations/en/checks.php | 230 +++++++++++++++--------------- 6 files changed, 396 insertions(+), 222 deletions(-) create mode 100644 src/Checks/EnvVariablesExists.php create mode 100644 tests/EnvVarsExistsTest.php diff --git a/config/config.php b/config/config.php index e029bf6..084f22d 100644 --- a/config/config.php +++ b/config/config.php @@ -29,6 +29,7 @@ ], \BeyondCode\SelfDiagnosis\Checks\EnvFileExists::class, \BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class, + \BeyondCode\SelfDiagnosis\Checks\EnvVariablesExists::class, \BeyondCode\SelfDiagnosis\Checks\LocalesAreInstalled::class => [ 'required_locales' => [ 'en_US', @@ -65,6 +66,7 @@ \BeyondCode\SelfDiagnosis\Checks\ConfigurationIsNotCached::class, \BeyondCode\SelfDiagnosis\Checks\RoutesAreNotCached::class, \BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreUpToDate::class, + \BeyondCode\SelfDiagnosis\Checks\EnvVariablesExists::class, ], 'production' => [ \BeyondCode\SelfDiagnosis\Checks\ComposerWithoutDevDependenciesIsUpToDate::class, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6e179a7..d9b5c81 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -26,4 +26,7 @@ + + + diff --git a/src/Checks/EnvVariablesExists.php b/src/Checks/EnvVariablesExists.php new file mode 100644 index 0000000..3d91a99 --- /dev/null +++ b/src/Checks/EnvVariablesExists.php @@ -0,0 +1,132 @@ + $this->empty, + ]); + } + + /** + * Directories to start recursive search for env()'s from + * Defaults to config_path() + * + * @var string $dir + */ + public $paths; + + /** + * Perform the actual verification of this check. + * + * @param array $config + * @return bool + * @throws \Exception + */ + public function check(array $config): bool + { + $this->paths = Collection::make(Arr::get($config, 'directories', [])); + + foreach ($this->paths as $path) { + $files = $this->recursiveDirSearch($path, '/.*?.php/'); + + foreach ($files as $file) { + $values = array_filter( + preg_split("#[\n]+#", shell_exec("tr -d '\n' < $file | grep -oP 'env\(\K[^)]+'")) + ); + + foreach ($values as $value) { + $result = $this->getResult( + explode(',', str_replace(["'", '"', ' '], '', $value)) + ); + + $this->storeResult($result); + } + } + } + + return $this->empty === 0; + } + + /** + * Get result based on comma separated parsed env() parameters + * + * @param array $values + * @return object + */ + private function getResult(array $values) + { + return (object)[ + 'envVar' => $values[0], + 'hasValue' => (bool)env($values[0]), + 'hasDefault' => isset($values[1]), + ]; + } + + /** + * Store result and optional runtime output + * + * @param $result + */ + private function storeResult($result) + { + if (! $result->hasValue && ! $result->hasDefault) { + $this->empty++; + } + } + + private function recursiveDirSearch(string $folder, string $pattern): array + { + if (! file_exists($folder)) { + return []; + } + + $files = new RegexIterator( + new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($folder) + ), + $pattern, RegexIterator::GET_MATCH + ); + + $list = []; + + foreach($files as $file) { + $list = array_merge($list, $file); + } + + return $list; + } +} diff --git a/tests/EnvVarsExistsTest.php b/tests/EnvVarsExistsTest.php new file mode 100644 index 0000000..d732bb1 --- /dev/null +++ b/tests/EnvVarsExistsTest.php @@ -0,0 +1,29 @@ + [ + __DIR__ + ], + ]; + + $check = new EnvVariablesExists(); + $result = $check->check($config); + + $this->assertTrue($check->empty === 1); + $this->assertFalse($result); + } +} diff --git a/translations/de/checks.php b/translations/de/checks.php index 4b6be7e..c687ec9 100644 --- a/translations/de/checks.php +++ b/translations/de/checks.php @@ -1,109 +1,113 @@ - [ - 'message' => 'Der Anwendungsschlüssel ist nicht gesetzt. Nutze "php artisan key:generate", um einen zu erstellen und zu setzen.', - 'name' => 'Anwendungsschlüssel ist gesetzt', - ], - 'composer_with_dev_dependencies_is_up_to_date' => [ - 'message' => 'Die Composer Abhängigkeiten sind nicht aktuell. Nutze "composer install", um diese zu aktualisieren. :more', - 'name' => 'Composer Abhängigkeiten (inkl. dev) sind aktuell', - ], - 'composer_without_dev_dependencies_is_up_to_date' => [ - 'message' => 'Die Composer Abhängigkeiten sind nicht aktuell. Nutze "composer install", um diese zu aktualisieren. :more', - 'name' => 'Composer Abhängigkeiten (ohne dev) sind aktuell', - ], - 'configuration_is_cached' => [ - 'message' => 'Die Konfiguration sollte für bessere Performance gecached sein im Produktivbetrieb. Nutze "php artisan config:cache", um den Konfigurations-Cache zu erstellen.', - 'name' => 'Konfiguration ist gecached', - ], - 'configuration_is_not_cached' => [ - 'message' => 'Die Konfiguration sollte während der Entwicklung nicht gecached sein. Nutze "php artisan config:clear", um den Konfigurations-Cache zu leeren.', - 'name' => 'Konfiguration ist nicht gecached', - ], - 'correct_php_version_is_installed' => [ - 'message' => 'Die benötigte PHP Version ist nicht installiert.' . PHP_EOL . 'Benötigt: :required' . PHP_EOL . 'In Verwendung: :used', - 'name' => 'Die richtige PHP Version ist installiert', - ], - 'database_can_be_accessed' => [ - 'message' => 'Auf die Datenbank kann nicht zugegriffen werden: :error', - 'name' => 'Die Datenbank ist erreichbar', - ], - 'debug_mode_is_not_enabled' => [ - 'message' => 'Der Debugging-Modus sollte im Produktivbetrieb nicht genutzt werden. Setze "APP_DEBUG" in der .env Datei auf "false".', - 'name' => 'Der Debugging-Modus ist deaktiviert', - ], - 'directories_have_correct_permissions' => [ - 'message' => 'Folgende Verzeichnisse sind nicht beschreibbar:' . PHP_EOL .':directories', - 'name' => 'Alle Verzeichnisse haben die richtigen Berechtigungen', - ], - 'env_file_exists' => [ - 'message' => 'Die .env Datei existiert nicht. Bitte kopiere die Datei .env.example zu .env und passe diese entsprechend an.', - 'name' => 'Die Umgebungsvariablendatei existiert', - ], - 'example_environment_variables_are_set' => [ - 'message' => 'Folgende Umgebungsvariablen fehlen im .env Umgebungsfile, sind aber in .env.example definiert:' . PHP_EOL . ':variables', - 'name' => 'Die Beispiel-Umgebungsvariablen sind gesetzt', - ], - 'locales_are_installed' => [ - 'message' => [ - 'cannot_run_on_windows' => 'Dieser Check kann unter Windows nicht ausgeführt werden..', - 'locale_command_not_available' => 'Der Befehl "locale -a" ist auf dem aktuellen System nicht verfügbar.', - 'missing_locales' => 'Folgende Sprachumgebungen (locales) fehlen:' . PHP_EOL . ':locales', - 'shell_exec_not_available' => 'Die Funktion "shell_exec" ist entweder nicht definiert oder deaktiviert, daher können die Sprachumgebungen nicht abgefragt werden.', - ], - 'name' => 'Benötigte Sprachumgebungen sind installiert', - ], - 'maintenance_mode_not_enabled' => [ - 'message' => 'Der Wartungsmodus ist noch aktiv. Deaktiviere ihn mit "php artisan up".', - 'name' => 'Wartungsmodus ist nicht aktiv', - ], - 'migrations_are_up_to_date' => [ - 'message' => [ - 'need_to_migrate' => 'Die Datenbank muss aktualisiert werden. Nutze "php artisan migrate", um die Migrationen einzuspielen.', - 'unable_to_check' => 'Die Migrationen konnten nicht geprüft werden: :reason', - ], - 'name' => 'Die Migrationen sind aktuell', - ], - 'php_extensions_are_disabled' => [ - 'message' => 'Die folgenden Erweiterungen sind noch immer aktiviert:' . PHP_EOL . ':extensions', - 'name' => 'Unerwünschte PHP Erweiterungen sind deaktiviert', - ], - 'php_extensions_are_installed' => [ - 'message' => 'Die folgenden Erweiterungen fehlen:' . PHP_EOL . ':extensions', - 'name' => 'Die benötigten PHP Erweiterungen sind installiert', - ], - 'redis_can_be_accessed' => [ - 'message' => [ - 'not_accessible' => 'Auf den Redis Cache kann nicht zugegriffen werden: :error', - 'default_cache' => 'Der Standard-Cache ist nicht erreichbar.', - 'named_cache' => 'Der Cache :name ist nicht erreichbar.', - ], - 'name' => 'Der Redis Cache ist erreichbar', - ], - 'routes_are_cached' => [ - 'message' => 'Die Routen sollten für bessere Performance gecached sein im Produktivbetrieb. Nutze "php artisan route:cache", um den Routen-Cache zu erstellen.', - 'name' => 'Routen sind gecached', - ], - 'routes_are_not_cached' => [ - 'message' => 'Die Routen sollten während der Entwicklung nicht gecached sein. Nutze "php artisan route:clear", um den Routen-Cache zu leeren.', - 'name' => 'Routen sind nicht gecached', - ], - 'servers_are_pingable' => [ - 'message' => "Der Server ':host' (Port: :port) ist nicht erreichbar (Timeout nach :timeout Sekunden).", - 'name' => 'Benötigte Server sind pingbar', - ], - 'storage_directory_is_linked' => [ - 'message' => 'Das Speicherverzeichnis ist nicht verlinkt. Nutze "php artisan storage:link", um eine symbolische Verknüpfung zu erstellen.', - 'name' => 'Das Speicherverzeichnis ist verlinkt', - ], - 'supervisor_programs_are_running' => [ - 'message' => [ - 'cannot_run_on_windows' => 'Dieser Check kann unter Windows nicht ausgeführt werden..', - 'not_running_programs' => 'Die folgenden Programme laufen nicht oder benötigen einen Neustart:' . PHP_EOL . ':programs', - 'shell_exec_not_available' => 'Die Funktion "shell_exec" ist entweder nicht definiert oder deaktiviert, daher können die laufenden Programme nicht abgefragt werden.', - 'supervisor_command_not_available' => 'Der Befehl "supervisorctl" ist auf dem aktuellen System nicht verfügbar.', - ], - 'name' => 'Alle supervisor Programme sind in Betrieb', - ], -]; + [ + 'message' => 'Der Anwendungsschlüssel ist nicht gesetzt. Nutze "php artisan key:generate", um einen zu erstellen und zu setzen.', + 'name' => 'Anwendungsschlüssel ist gesetzt', + ], + 'composer_with_dev_dependencies_is_up_to_date' => [ + 'message' => 'Die Composer Abhängigkeiten sind nicht aktuell. Nutze "composer install", um diese zu aktualisieren. :more', + 'name' => 'Composer Abhängigkeiten (inkl. dev) sind aktuell', + ], + 'composer_without_dev_dependencies_is_up_to_date' => [ + 'message' => 'Die Composer Abhängigkeiten sind nicht aktuell. Nutze "composer install", um diese zu aktualisieren. :more', + 'name' => 'Composer Abhängigkeiten (ohne dev) sind aktuell', + ], + 'configuration_is_cached' => [ + 'message' => 'Die Konfiguration sollte für bessere Performance gecached sein im Produktivbetrieb. Nutze "php artisan config:cache", um den Konfigurations-Cache zu erstellen.', + 'name' => 'Konfiguration ist gecached', + ], + 'configuration_is_not_cached' => [ + 'message' => 'Die Konfiguration sollte während der Entwicklung nicht gecached sein. Nutze "php artisan config:clear", um den Konfigurations-Cache zu leeren.', + 'name' => 'Konfiguration ist nicht gecached', + ], + 'correct_php_version_is_installed' => [ + 'message' => 'Die benötigte PHP Version ist nicht installiert.' . PHP_EOL . 'Benötigt: :required' . PHP_EOL . 'In Verwendung: :used', + 'name' => 'Die richtige PHP Version ist installiert', + ], + 'database_can_be_accessed' => [ + 'message' => 'Auf die Datenbank kann nicht zugegriffen werden: :error', + 'name' => 'Die Datenbank ist erreichbar', + ], + 'debug_mode_is_not_enabled' => [ + 'message' => 'Der Debugging-Modus sollte im Produktivbetrieb nicht genutzt werden. Setze "APP_DEBUG" in der .env Datei auf "false".', + 'name' => 'Der Debugging-Modus ist deaktiviert', + ], + 'directories_have_correct_permissions' => [ + 'message' => 'Folgende Verzeichnisse sind nicht beschreibbar:' . PHP_EOL .':directories', + 'name' => 'Alle Verzeichnisse haben die richtigen Berechtigungen', + ], + 'env_file_exists' => [ + 'message' => 'Die .env Datei existiert nicht. Bitte kopiere die Datei .env.example zu .env und passe diese entsprechend an.', + 'name' => 'Die Umgebungsvariablendatei existiert', + ], + 'example_environment_variables_are_set' => [ + 'message' => 'Folgende Umgebungsvariablen fehlen im .env Umgebungsfile, sind aber in .env.example definiert:' . PHP_EOL . ':variables', + 'name' => 'Die Beispiel-Umgebungsvariablen sind gesetzt', + ], + 'locales_are_installed' => [ + 'message' => [ + 'cannot_run_on_windows' => 'Dieser Check kann unter Windows nicht ausgeführt werden..', + 'locale_command_not_available' => 'Der Befehl "locale -a" ist auf dem aktuellen System nicht verfügbar.', + 'missing_locales' => 'Folgende Sprachumgebungen (locales) fehlen:' . PHP_EOL . ':locales', + 'shell_exec_not_available' => 'Die Funktion "shell_exec" ist entweder nicht definiert oder deaktiviert, daher können die Sprachumgebungen nicht abgefragt werden.', + ], + 'name' => 'Benötigte Sprachumgebungen sind installiert', + ], + 'maintenance_mode_not_enabled' => [ + 'message' => 'Der Wartungsmodus ist noch aktiv. Deaktiviere ihn mit "php artisan up".', + 'name' => 'Wartungsmodus ist nicht aktiv', + ], + 'migrations_are_up_to_date' => [ + 'message' => [ + 'need_to_migrate' => 'Die Datenbank muss aktualisiert werden. Nutze "php artisan migrate", um die Migrationen einzuspielen.', + 'unable_to_check' => 'Die Migrationen konnten nicht geprüft werden: :reason', + ], + 'name' => 'Die Migrationen sind aktuell', + ], + 'php_extensions_are_disabled' => [ + 'message' => 'Die folgenden Erweiterungen sind noch immer aktiviert:' . PHP_EOL . ':extensions', + 'name' => 'Unerwünschte PHP Erweiterungen sind deaktiviert', + ], + 'php_extensions_are_installed' => [ + 'message' => 'Die folgenden Erweiterungen fehlen:' . PHP_EOL . ':extensions', + 'name' => 'Die benötigten PHP Erweiterungen sind installiert', + ], + 'redis_can_be_accessed' => [ + 'message' => [ + 'not_accessible' => 'Auf den Redis Cache kann nicht zugegriffen werden: :error', + 'default_cache' => 'Der Standard-Cache ist nicht erreichbar.', + 'named_cache' => 'Der Cache :name ist nicht erreichbar.', + ], + 'name' => 'Der Redis Cache ist erreichbar', + ], + 'routes_are_cached' => [ + 'message' => 'Die Routen sollten für bessere Performance gecached sein im Produktivbetrieb. Nutze "php artisan route:cache", um den Routen-Cache zu erstellen.', + 'name' => 'Routen sind gecached', + ], + 'routes_are_not_cached' => [ + 'message' => 'Die Routen sollten während der Entwicklung nicht gecached sein. Nutze "php artisan route:clear", um den Routen-Cache zu leeren.', + 'name' => 'Routen sind nicht gecached', + ], + 'servers_are_pingable' => [ + 'message' => "Der Server ':host' (Port: :port) ist nicht erreichbar (Timeout nach :timeout Sekunden).", + 'name' => 'Benötigte Server sind pingbar', + ], + 'storage_directory_is_linked' => [ + 'message' => 'Das Speicherverzeichnis ist nicht verlinkt. Nutze "php artisan storage:link", um eine symbolische Verknüpfung zu erstellen.', + 'name' => 'Das Speicherverzeichnis ist verlinkt', + ], + 'supervisor_programs_are_running' => [ + 'message' => [ + 'cannot_run_on_windows' => 'Dieser Check kann unter Windows nicht ausgeführt werden..', + 'not_running_programs' => 'Die folgenden Programme laufen nicht oder benötigen einen Neustart:' . PHP_EOL . ':programs', + 'shell_exec_not_available' => 'Die Funktion "shell_exec" ist entweder nicht definiert oder deaktiviert, daher können die laufenden Programme nicht abgefragt werden.', + 'supervisor_command_not_available' => 'Der Befehl "supervisorctl" ist auf dem aktuellen System nicht verfügbar.', + ], + 'name' => 'Alle supervisor Programme sind in Betrieb', + ], + 'env_variables_exist' => [ + 'message' => ':empty Umgebungsvariablen existieren nicht.', + 'name' => 'Alle Umgebungsvariablen existieren.', + ], +]; diff --git a/translations/en/checks.php b/translations/en/checks.php index 2915a13..2c32387 100644 --- a/translations/en/checks.php +++ b/translations/en/checks.php @@ -1,113 +1,117 @@ - [ - 'message' => 'The application key is not set. Call "php artisan key:generate" to create and set one.', - 'name' => 'App key is set', - ], - 'composer_with_dev_dependencies_is_up_to_date' => [ - 'message' => 'Your composer dependencies are not up to date. Call "composer install" to update them. :more', - 'name' => 'Composer dependencies (including dev) are up to date', - ], - 'composer_without_dev_dependencies_is_up_to_date' => [ - 'message' => 'Your composer dependencies are not up to date. Call "composer install" to update them. :more', - 'name' => 'Composer dependencies (without dev) are up to date', - ], - 'configuration_is_cached' => [ - 'message' => 'Your configuration should be cached in production for better performance. Call "php artisan config:cache" to create the configuration cache.', - 'name' => 'Configuration is cached', - ], - 'configuration_is_not_cached' => [ - 'message' => 'Your configuration files should not be cached during development. Call "php artisan config:clear" to clear the configuration cache.', - 'name' => 'Configuration is not cached', - ], - 'correct_php_version_is_installed' => [ - 'message' => 'You do not have the required PHP version installed.' . PHP_EOL . 'Required: :required' . PHP_EOL . 'Used: :used', - 'name' => 'The correct PHP version is installed', - ], - 'database_can_be_accessed' => [ - 'message' => 'The database can not be accessed: :error', - 'name' => 'The database can be accessed', - ], - 'debug_mode_is_not_enabled' => [ - 'message' => 'You should not use debug mode in production. Set "APP_DEBUG" in the .env file to "false".', - 'name' => 'Debug mode is not enabled', - ], - 'directories_have_correct_permissions' => [ - 'message' => 'The following directories are not writable:' . PHP_EOL .':directories', - 'name' => 'All directories have the correct permissions', - ], - 'env_file_exists' => [ - 'message' => 'The .env file does not exist. Please copy your .env.example file as .env and adjust accordingly.', - 'name' => 'The environment file exists', - ], - 'example_environment_variables_are_set' => [ - 'message' => 'These environment variables are missing in your .env file, but are defined in your .env.example:' . PHP_EOL . ':variables', - 'name' => 'The example environment variables are set', - ], - 'example_environment_variables_are_up_to_date' => [ - 'message' => 'These environment variables are defined in your .env file, but are missing in your .env.example:' . PHP_EOL . ':variables', - 'name' => 'The example environment variables are up-to-date', - ], - 'locales_are_installed' => [ - 'message' => [ - 'cannot_run_on_windows' => 'This check cannot be run on Windows.', - 'locale_command_not_available' => 'The "locale -a" command is not available on the current OS.', - 'missing_locales' => 'The following locales are missing:' . PHP_EOL . ':locales', - 'shell_exec_not_available' => 'The function "shell_exec" is not defined or disabled, so we cannot check the locales.', - ], - 'name' => 'Required locales are installed', - ], - 'maintenance_mode_not_enabled' => [ - 'message' => 'Maintenance mode is still enabled. Disable it with "php artisan up".', - 'name' => 'Maintenance mode is not enabled', - ], - 'migrations_are_up_to_date' => [ - 'message' => [ - 'need_to_migrate' => 'You need to update your database. Call "php artisan migrate" to run migrations.', - 'unable_to_check' => 'Unable to check for migrations: :reason', - ], - 'name' => 'The migrations are up to date', - ], - 'php_extensions_are_disabled' => [ - 'message' => 'The following extensions are still enabled:' . PHP_EOL . ':extensions', - 'name' => 'Unwanted PHP extensions are disabled', - ], - 'php_extensions_are_installed' => [ - 'message' => 'The following extensions are missing:' . PHP_EOL . ':extensions', - 'name' => 'The required PHP extensions are installed', - ], - 'redis_can_be_accessed' => [ - 'message' => [ - 'not_accessible' => 'The Redis cache can not be accessed: :error', - 'default_cache' => 'The default cache is not reachable.', - 'named_cache' => 'The named cache :name is not reachable.', - ], - 'name' => 'The Redis cache can be accessed', - ], - 'routes_are_cached' => [ - 'message' => 'Your routes should be cached in production for better performance. Call "php artisan route:cache" to create the route cache.', - 'name' => 'Routes are cached', - ], - 'routes_are_not_cached' => [ - 'message' => 'Your routes should not be cached during development. Call "php artisan route:clear" to clear the route cache.', - 'name' => 'Routes are not cached', - ], - 'servers_are_pingable' => [ - 'message' => "The server ':host' (port: :port) is not reachable (timeout after :timeout seconds).", - 'name' => 'Required servers are pingable', - ], - 'storage_directory_is_linked' => [ - 'message' => 'The storage directory is not linked. Use "php artisan storage:link" to create a symbolic link.', - 'name' => 'The storage directory is linked', - ], - 'supervisor_programs_are_running' => [ - 'message' => [ - 'cannot_run_on_windows' => 'This check cannot be run on Windows.', - 'not_running_programs' => 'The following programs are not running or require a restart:' . PHP_EOL . ':programs', - 'shell_exec_not_available' => 'The function "shell_exec" is not defined or disabled, so we cannot check the running programs.', - 'supervisor_command_not_available' => 'The "supervisorctl" command is not available on the current OS.', - ], - 'name' => 'All supervisor programs are running', - ], -]; + [ + 'message' => 'The application key is not set. Call "php artisan key:generate" to create and set one.', + 'name' => 'App key is set', + ], + 'composer_with_dev_dependencies_is_up_to_date' => [ + 'message' => 'Your composer dependencies are not up to date. Call "composer install" to update them. :more', + 'name' => 'Composer dependencies (including dev) are up to date', + ], + 'composer_without_dev_dependencies_is_up_to_date' => [ + 'message' => 'Your composer dependencies are not up to date. Call "composer install" to update them. :more', + 'name' => 'Composer dependencies (without dev) are up to date', + ], + 'configuration_is_cached' => [ + 'message' => 'Your configuration should be cached in production for better performance. Call "php artisan config:cache" to create the configuration cache.', + 'name' => 'Configuration is cached', + ], + 'configuration_is_not_cached' => [ + 'message' => 'Your configuration files should not be cached during development. Call "php artisan config:clear" to clear the configuration cache.', + 'name' => 'Configuration is not cached', + ], + 'correct_php_version_is_installed' => [ + 'message' => 'You do not have the required PHP version installed.' . PHP_EOL . 'Required: :required' . PHP_EOL . 'Used: :used', + 'name' => 'The correct PHP version is installed', + ], + 'database_can_be_accessed' => [ + 'message' => 'The database can not be accessed: :error', + 'name' => 'The database can be accessed', + ], + 'debug_mode_is_not_enabled' => [ + 'message' => 'You should not use debug mode in production. Set "APP_DEBUG" in the .env file to "false".', + 'name' => 'Debug mode is not enabled', + ], + 'directories_have_correct_permissions' => [ + 'message' => 'The following directories are not writable:' . PHP_EOL .':directories', + 'name' => 'All directories have the correct permissions', + ], + 'env_file_exists' => [ + 'message' => 'The .env file does not exist. Please copy your .env.example file as .env and adjust accordingly.', + 'name' => 'The environment file exists', + ], + 'example_environment_variables_are_set' => [ + 'message' => 'These environment variables are missing in your .env file, but are defined in your .env.example:' . PHP_EOL . ':variables', + 'name' => 'The example environment variables are set', + ], + 'example_environment_variables_are_up_to_date' => [ + 'message' => 'These environment variables are defined in your .env file, but are missing in your .env.example:' . PHP_EOL . ':variables', + 'name' => 'The example environment variables are up-to-date', + ], + 'locales_are_installed' => [ + 'message' => [ + 'cannot_run_on_windows' => 'This check cannot be run on Windows.', + 'locale_command_not_available' => 'The "locale -a" command is not available on the current OS.', + 'missing_locales' => 'The following locales are missing:' . PHP_EOL . ':locales', + 'shell_exec_not_available' => 'The function "shell_exec" is not defined or disabled, so we cannot check the locales.', + ], + 'name' => 'Required locales are installed', + ], + 'maintenance_mode_not_enabled' => [ + 'message' => 'Maintenance mode is still enabled. Disable it with "php artisan up".', + 'name' => 'Maintenance mode is not enabled', + ], + 'migrations_are_up_to_date' => [ + 'message' => [ + 'need_to_migrate' => 'You need to update your database. Call "php artisan migrate" to run migrations.', + 'unable_to_check' => 'Unable to check for migrations: :reason', + ], + 'name' => 'The migrations are up to date', + ], + 'php_extensions_are_disabled' => [ + 'message' => 'The following extensions are still enabled:' . PHP_EOL . ':extensions', + 'name' => 'Unwanted PHP extensions are disabled', + ], + 'php_extensions_are_installed' => [ + 'message' => 'The following extensions are missing:' . PHP_EOL . ':extensions', + 'name' => 'The required PHP extensions are installed', + ], + 'redis_can_be_accessed' => [ + 'message' => [ + 'not_accessible' => 'The Redis cache can not be accessed: :error', + 'default_cache' => 'The default cache is not reachable.', + 'named_cache' => 'The named cache :name is not reachable.', + ], + 'name' => 'The Redis cache can be accessed', + ], + 'routes_are_cached' => [ + 'message' => 'Your routes should be cached in production for better performance. Call "php artisan route:cache" to create the route cache.', + 'name' => 'Routes are cached', + ], + 'routes_are_not_cached' => [ + 'message' => 'Your routes should not be cached during development. Call "php artisan route:clear" to clear the route cache.', + 'name' => 'Routes are not cached', + ], + 'servers_are_pingable' => [ + 'message' => "The server ':host' (port: :port) is not reachable (timeout after :timeout seconds).", + 'name' => 'Required servers are pingable', + ], + 'storage_directory_is_linked' => [ + 'message' => 'The storage directory is not linked. Use "php artisan storage:link" to create a symbolic link.', + 'name' => 'The storage directory is linked', + ], + 'supervisor_programs_are_running' => [ + 'message' => [ + 'cannot_run_on_windows' => 'This check cannot be run on Windows.', + 'not_running_programs' => 'The following programs are not running or require a restart:' . PHP_EOL . ':programs', + 'shell_exec_not_available' => 'The function "shell_exec" is not defined or disabled, so we cannot check the running programs.', + 'supervisor_command_not_available' => 'The "supervisorctl" command is not available on the current OS.', + ], + 'name' => 'All supervisor programs are running', + ], + 'env_variables_exist' => [ + 'message' => ':empty environmental variables don\'t exist.', + 'name' => 'All environmental variables exist', + ], +]; From 481e648be6d0c4e87aa1697758d68994b49b8bda Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Sun, 21 Jul 2019 02:37:21 +0200 Subject: [PATCH 2/9] Removes grep -oP dependency --- src/Checks/EnvVariablesExists.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Checks/EnvVariablesExists.php b/src/Checks/EnvVariablesExists.php index 3d91a99..c93b26b 100644 --- a/src/Checks/EnvVariablesExists.php +++ b/src/Checks/EnvVariablesExists.php @@ -64,16 +64,20 @@ public function check(array $config): bool $files = $this->recursiveDirSearch($path, '/.*?.php/'); foreach ($files as $file) { - $values = array_filter( - preg_split("#[\n]+#", shell_exec("tr -d '\n' < $file | grep -oP 'env\(\K[^)]+'")) + preg_match_all( + '#env\((.*?)\)#', + shell_exec("tr -d '\n' < $file"), + $values ); - foreach ($values as $value) { - $result = $this->getResult( - explode(',', str_replace(["'", '"', ' '], '', $value)) - ); + if (is_array($values)) { + foreach ($values[1] as $value) { + $result = $this->getResult( + explode(',', str_replace(["'", '"', ' '], '', $value)) + ); - $this->storeResult($result); + $this->storeResult($result); + } } } } From 71753d3b14c7c3b8954c41bc6daef4a30c0dcea2 Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Sun, 21 Jul 2019 13:40:46 +0200 Subject: [PATCH 3/9] Getting rid of shell_exec --- src/Checks/EnvVariablesExists.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Checks/EnvVariablesExists.php b/src/Checks/EnvVariablesExists.php index c93b26b..33b746d 100644 --- a/src/Checks/EnvVariablesExists.php +++ b/src/Checks/EnvVariablesExists.php @@ -66,7 +66,7 @@ public function check(array $config): bool foreach ($files as $file) { preg_match_all( '#env\((.*?)\)#', - shell_exec("tr -d '\n' < $file"), + str_replace(["\n", "\r"], '', file_get_contents($file)), $values ); From 85dccb425d59aa38b4e7016ed00d980a51cd9a30 Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Sun, 21 Jul 2019 23:58:25 +0200 Subject: [PATCH 4/9] Fix variables with false value showing up as empty and double processing --- phpunit.xml.dist | 2 ++ src/Checks/EnvVariablesExists.php | 43 ++++++++++++++++++++++--------- tests/EnvVarsExistsTest.php | 19 ++++++++++---- translations/de/checks.php | 2 +- translations/en/checks.php | 2 +- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d9b5c81..4554343 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,5 +28,7 @@ + + diff --git a/src/Checks/EnvVariablesExists.php b/src/Checks/EnvVariablesExists.php index 33b746d..88fa211 100644 --- a/src/Checks/EnvVariablesExists.php +++ b/src/Checks/EnvVariablesExists.php @@ -10,12 +10,19 @@ class EnvVariablesExists implements Check { + /** + * Stores processed var names + * + * @var array + */ + private $processed = []; + /** * The empty results of performed scan * * @var array */ - public $empty = 0; + public $undefined = 0; /** * The name of the check. @@ -37,7 +44,7 @@ public function name(array $config): string public function message(array $config): string { return trans('self-diagnosis::checks.env_variables_exist.message', [ - 'empty' => $this->empty, + 'undefined' => $this->undefined, ]); } @@ -61,11 +68,11 @@ public function check(array $config): bool $this->paths = Collection::make(Arr::get($config, 'directories', [])); foreach ($this->paths as $path) { - $files = $this->recursiveDirSearch($path, '/.*?.php/'); + $files = $this->recursiveDirSearch($path, '/.*?.php/'); foreach ($files as $file) { preg_match_all( - '#env\((.*?)\)#', + '# env\((.*?)\)#', str_replace(["\n", "\r"], '', file_get_contents($file)), $values ); @@ -76,26 +83,38 @@ public function check(array $config): bool explode(',', str_replace(["'", '"', ' '], '', $value)) ); + if (!$result) { + continue; + } + $this->storeResult($result); } } } } - return $this->empty === 0; + return $this->undefined === 0; } /** * Get result based on comma separated parsed env() parameters * * @param array $values - * @return object + * @return object|bool */ private function getResult(array $values) { + $envVar = $values[0]; + + if (in_array($envVar, $this->processed)) { + return false; + } + + $this->processed[] = $envVar; + return (object)[ - 'envVar' => $values[0], - 'hasValue' => (bool)env($values[0]), + 'envVar' => $envVar, + 'hasValue' => env($envVar) !== null, 'hasDefault' => isset($values[1]), ]; } @@ -107,14 +126,14 @@ private function getResult(array $values) */ private function storeResult($result) { - if (! $result->hasValue && ! $result->hasDefault) { - $this->empty++; + if (!$result->hasValue && !$result->hasDefault) { + $this->undefined++; } } private function recursiveDirSearch(string $folder, string $pattern): array { - if (! file_exists($folder)) { + if (!file_exists($folder)) { return []; } @@ -127,7 +146,7 @@ private function recursiveDirSearch(string $folder, string $pattern): array $list = []; - foreach($files as $file) { + foreach ($files as $file) { $list = array_merge($list, $file); } diff --git a/tests/EnvVarsExistsTest.php b/tests/EnvVarsExistsTest.php index d732bb1..0941ed4 100644 --- a/tests/EnvVarsExistsTest.php +++ b/tests/EnvVarsExistsTest.php @@ -7,12 +7,21 @@ class EnvVarsExistsTest extends TestCase { - /** @test */ - public function it_checks_if_env_vars_eixsts() + /** @test + * @throws \Exception + */ + public function it_checks_if_env_vars_exists() { env('FILLED'); - env('DEPENDS', 'on_default'); - env('EMPTY'); + env('NOT_FILLED'); + env('FILLED_WITH_FALSE'); + + env('DEPENDING_ON_DEFAULT', 'default'); + env('DEFAULT_IS_FALSE', false); + + env('UNDEFINED'); + // Doubles should be ignored + env('UNDEFINED'); $config = [ 'directories' => [ @@ -23,7 +32,7 @@ public function it_checks_if_env_vars_eixsts() $check = new EnvVariablesExists(); $result = $check->check($config); - $this->assertTrue($check->empty === 1); + $this->assertTrue($check->undefined === 1); $this->assertFalse($result); } } diff --git a/translations/de/checks.php b/translations/de/checks.php index c687ec9..57e2159 100644 --- a/translations/de/checks.php +++ b/translations/de/checks.php @@ -107,7 +107,7 @@ 'name' => 'Alle supervisor Programme sind in Betrieb', ], 'env_variables_exist' => [ - 'message' => ':empty Umgebungsvariablen existieren nicht.', + 'message' => ':undefined Umgebungsvariablen existieren nicht.', 'name' => 'Alle Umgebungsvariablen existieren.', ], ]; diff --git a/translations/en/checks.php b/translations/en/checks.php index 2c32387..4f505e1 100644 --- a/translations/en/checks.php +++ b/translations/en/checks.php @@ -111,7 +111,7 @@ 'name' => 'All supervisor programs are running', ], 'env_variables_exist' => [ - 'message' => ':empty environmental variables don\'t exist.', + 'message' => ':undefined environmental variables don\'t exist.', 'name' => 'All environmental variables exist', ], ]; From 622d205211103d562baf8f6425b823342151f802 Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Mon, 22 Jul 2019 00:21:13 +0200 Subject: [PATCH 5/9] Remove unnecessary public var $paths and add default directories to config --- config/config.php | 7 ++++++- src/Checks/EnvVariablesExists.php | 12 ++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/config/config.php b/config/config.php index 084f22d..33d41c4 100644 --- a/config/config.php +++ b/config/config.php @@ -29,7 +29,12 @@ ], \BeyondCode\SelfDiagnosis\Checks\EnvFileExists::class, \BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class, - \BeyondCode\SelfDiagnosis\Checks\EnvVariablesExists::class, + \BeyondCode\SelfDiagnosis\Checks\EnvVariablesExists::class => [ + 'directories' => [ + config_path(), + app_path(), + ], + ], \BeyondCode\SelfDiagnosis\Checks\LocalesAreInstalled::class => [ 'required_locales' => [ 'en_US', diff --git a/src/Checks/EnvVariablesExists.php b/src/Checks/EnvVariablesExists.php index 88fa211..c9520c3 100644 --- a/src/Checks/EnvVariablesExists.php +++ b/src/Checks/EnvVariablesExists.php @@ -48,14 +48,6 @@ public function message(array $config): string ]); } - /** - * Directories to start recursive search for env()'s from - * Defaults to config_path() - * - * @var string $dir - */ - public $paths; - /** * Perform the actual verification of this check. * @@ -65,9 +57,9 @@ public function message(array $config): string */ public function check(array $config): bool { - $this->paths = Collection::make(Arr::get($config, 'directories', [])); + $paths = Collection::make(Arr::get($config, 'directories', [])); - foreach ($this->paths as $path) { + foreach ($paths as $path) { $files = $this->recursiveDirSearch($path, '/.*?.php/'); foreach ($files as $file) { From 1ed8cdffabfe33ca0d635e2a2409926f4fe951bf Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Mon, 22 Jul 2019 15:14:35 +0200 Subject: [PATCH 6/9] More explanatory class naming and output showing undefined variables list --- config/config.php | 4 +- ...=> UsedEnvironmentVariablesAreDefined.php} | 25 ++++++---- tests/EnvVarsExistsTest.php | 38 --------------- ...UsedEnvironmentVariablesAreDefinedTest.php | 47 +++++++++++++++++++ translations/de/checks.php | 6 +-- translations/en/checks.php | 6 +-- 6 files changed, 72 insertions(+), 54 deletions(-) rename src/Checks/{EnvVariablesExists.php => UsedEnvironmentVariablesAreDefined.php} (83%) delete mode 100644 tests/EnvVarsExistsTest.php create mode 100644 tests/UsedEnvironmentVariablesAreDefinedTest.php diff --git a/config/config.php b/config/config.php index 33d41c4..7175745 100644 --- a/config/config.php +++ b/config/config.php @@ -29,7 +29,7 @@ ], \BeyondCode\SelfDiagnosis\Checks\EnvFileExists::class, \BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class, - \BeyondCode\SelfDiagnosis\Checks\EnvVariablesExists::class => [ + \BeyondCode\SelfDiagnosis\Checks\UsedEnvironmentVariablesAreDefined::class => [ 'directories' => [ config_path(), app_path(), @@ -71,7 +71,7 @@ \BeyondCode\SelfDiagnosis\Checks\ConfigurationIsNotCached::class, \BeyondCode\SelfDiagnosis\Checks\RoutesAreNotCached::class, \BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreUpToDate::class, - \BeyondCode\SelfDiagnosis\Checks\EnvVariablesExists::class, + \BeyondCode\SelfDiagnosis\Checks\UsedEnvironmentVariablesAreDefined::class, ], 'production' => [ \BeyondCode\SelfDiagnosis\Checks\ComposerWithoutDevDependenciesIsUpToDate::class, diff --git a/src/Checks/EnvVariablesExists.php b/src/Checks/UsedEnvironmentVariablesAreDefined.php similarity index 83% rename from src/Checks/EnvVariablesExists.php rename to src/Checks/UsedEnvironmentVariablesAreDefined.php index c9520c3..35484cc 100644 --- a/src/Checks/EnvVariablesExists.php +++ b/src/Checks/UsedEnvironmentVariablesAreDefined.php @@ -8,7 +8,7 @@ use RecursiveIteratorIterator; use RegexIterator; -class EnvVariablesExists implements Check +class UsedEnvironmentVariablesAreDefined implements Check { /** * Stores processed var names @@ -18,11 +18,18 @@ class EnvVariablesExists implements Check private $processed = []; /** - * The empty results of performed scan + * Stores undefined var names * * @var array */ - public $undefined = 0; + public $undefined = []; + + /** + * The amount of undefined .env variables + * + * @var integer + */ + public $amount = 0; /** * The name of the check. @@ -32,7 +39,7 @@ class EnvVariablesExists implements Check */ public function name(array $config): string { - return trans('self-diagnosis::checks.env_variables_exist.name'); + return trans('self-diagnosis::checks.used_env_variables_are_defined.name'); } /** @@ -43,8 +50,9 @@ public function name(array $config): string */ public function message(array $config): string { - return trans('self-diagnosis::checks.env_variables_exist.message', [ - 'undefined' => $this->undefined, + return trans('self-diagnosis::checks.used_env_variables_are_defined.message', [ + 'amount' => $this->amount, + 'undefined' => implode(PHP_EOL, $this->undefined), ]); } @@ -85,7 +93,7 @@ public function check(array $config): bool } } - return $this->undefined === 0; + return $this->amount === 0; } /** @@ -119,7 +127,8 @@ private function getResult(array $values) private function storeResult($result) { if (!$result->hasValue && !$result->hasDefault) { - $this->undefined++; + $this->undefined[] = $result->envVar; + $this->amount++; } } diff --git a/tests/EnvVarsExistsTest.php b/tests/EnvVarsExistsTest.php deleted file mode 100644 index 0941ed4..0000000 --- a/tests/EnvVarsExistsTest.php +++ /dev/null @@ -1,38 +0,0 @@ - [ - __DIR__ - ], - ]; - - $check = new EnvVariablesExists(); - $result = $check->check($config); - - $this->assertTrue($check->undefined === 1); - $this->assertFalse($result); - } -} diff --git a/tests/UsedEnvironmentVariablesAreDefinedTest.php b/tests/UsedEnvironmentVariablesAreDefinedTest.php new file mode 100644 index 0000000..7686343 --- /dev/null +++ b/tests/UsedEnvironmentVariablesAreDefinedTest.php @@ -0,0 +1,47 @@ + [ + __DIR__ + ], + ]; + + $check = new UsedEnvironmentVariablesAreDefined(); + + $this->assertFalse($check->check($config)); + $this->assertTrue($check->amount === 1); + $this->assertTrue(in_array('UNDEFINED', $check->undefined)); + $this->assertSame("1 used environmental variables are undefined: \nUNDEFINED", $check->message($config)); + } +} diff --git a/translations/de/checks.php b/translations/de/checks.php index 57e2159..44e48c4 100644 --- a/translations/de/checks.php +++ b/translations/de/checks.php @@ -106,8 +106,8 @@ ], 'name' => 'Alle supervisor Programme sind in Betrieb', ], - 'env_variables_exist' => [ - 'message' => ':undefined Umgebungsvariablen existieren nicht.', - 'name' => 'Alle Umgebungsvariablen existieren.', + 'used_env_variables_are_defined' => [ + 'message' => ':amount verwendete Umgebungsvariablen sind undefiniert:'.PHP_EOL.':undefined', + 'name' => 'Alle verwendete Umgebungsvariablen sind definiert.', ], ]; diff --git a/translations/en/checks.php b/translations/en/checks.php index 4f505e1..7f92e7d 100644 --- a/translations/en/checks.php +++ b/translations/en/checks.php @@ -110,8 +110,8 @@ ], 'name' => 'All supervisor programs are running', ], - 'env_variables_exist' => [ - 'message' => ':undefined environmental variables don\'t exist.', - 'name' => 'All environmental variables exist', + 'used_env_variables_are_defined' => [ + 'message' => ':amount used environmental variables are undefined: ' . PHP_EOL . ':undefined', + 'name' => 'All used environmental variables are defined', ], ]; From 85247dddfb75ece5bb42b6d773e4e5c434860da8 Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Mon, 22 Jul 2019 18:58:37 +0200 Subject: [PATCH 7/9] Adds getenv() support --- phpunit.xml.dist | 1 + .../UsedEnvironmentVariablesAreDefined.php | 22 ++++++++++--------- ...UsedEnvironmentVariablesAreDefinedTest.php | 12 ++++++++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4554343..55b72ff 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,6 +28,7 @@ + diff --git a/src/Checks/UsedEnvironmentVariablesAreDefined.php b/src/Checks/UsedEnvironmentVariablesAreDefined.php index 35484cc..d331f9f 100644 --- a/src/Checks/UsedEnvironmentVariablesAreDefined.php +++ b/src/Checks/UsedEnvironmentVariablesAreDefined.php @@ -72,23 +72,25 @@ public function check(array $config): bool foreach ($files as $file) { preg_match_all( - '# env\((.*?)\)#', + '# env\((.*?)\)| getenv\((.*?)\)#', str_replace(["\n", "\r"], '', file_get_contents($file)), $values ); - if (is_array($values)) { - foreach ($values[1] as $value) { - $result = $this->getResult( - explode(',', str_replace(["'", '"', ' '], '', $value)) - ); + $values = array_filter( + array_merge($values[1], $values[2]) + ); - if (!$result) { - continue; - } + foreach ($values as $value) { + $result = $this->getResult( + explode(',', str_replace(["'", '"', ' '], '', $value)) + ); - $this->storeResult($result); + if (!$result) { + continue; } + + $this->storeResult($result); } } } diff --git a/tests/UsedEnvironmentVariablesAreDefinedTest.php b/tests/UsedEnvironmentVariablesAreDefinedTest.php index 7686343..2ed62fa 100644 --- a/tests/UsedEnvironmentVariablesAreDefinedTest.php +++ b/tests/UsedEnvironmentVariablesAreDefinedTest.php @@ -23,13 +23,17 @@ public function it_checks_if_used_env_vars_are_defined() env('FILLED'); env('NOT_FILLED'); env('FILLED_WITH_FALSE'); + getenv('GET_FILLED'); env('DEPENDING_ON_DEFAULT', 'default'); env('DEFAULT_IS_FALSE', false); + getenv('GET_DEPENDING_ON_DEFAULT', 'default'); env('UNDEFINED'); + getenv('GET_UNDEFINED'); // Doubles should be ignored env('UNDEFINED'); + getenv('GET_UNDEFINED'); $config = [ 'directories' => [ @@ -40,8 +44,12 @@ public function it_checks_if_used_env_vars_are_defined() $check = new UsedEnvironmentVariablesAreDefined(); $this->assertFalse($check->check($config)); - $this->assertTrue($check->amount === 1); + $this->assertTrue($check->amount === 2); $this->assertTrue(in_array('UNDEFINED', $check->undefined)); - $this->assertSame("1 used environmental variables are undefined: \nUNDEFINED", $check->message($config)); + $this->assertTrue(in_array('GET_UNDEFINED', $check->undefined)); + $this->assertSame( + "2 used environmental variables are undefined: \nUNDEFINED\nGET_UNDEFINED", + $check->message($config) + ); } } From 23da5f91630903f64a43a13fca6f1a9d9ffb70c3 Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Mon, 22 Jul 2019 20:39:32 +0200 Subject: [PATCH 8/9] Updated comments --- src/Checks/UsedEnvironmentVariablesAreDefined.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Checks/UsedEnvironmentVariablesAreDefined.php b/src/Checks/UsedEnvironmentVariablesAreDefined.php index d331f9f..e26d9a5 100644 --- a/src/Checks/UsedEnvironmentVariablesAreDefined.php +++ b/src/Checks/UsedEnvironmentVariablesAreDefined.php @@ -99,7 +99,7 @@ public function check(array $config): bool } /** - * Get result based on comma separated parsed env() parameters + * Get result based on comma separated env() or getenv() parameters * * @param array $values * @return object|bool @@ -122,7 +122,7 @@ private function getResult(array $values) } /** - * Store result and optional runtime output + * Store result based on getResult's return value * * @param $result */ @@ -134,6 +134,13 @@ private function storeResult($result) } } + /** + * Recursively search folder(s) for files matching pattern + * + * @param string $folder + * @param string $pattern + * @return array + */ private function recursiveDirSearch(string $folder, string $pattern): array { if (!file_exists($folder)) { From eeeceff6a0ad73792f62da61aea844adacaa92ff Mon Sep 17 00:00:00 2001 From: Maarten Tolhuijs Date: Wed, 24 Jul 2019 20:31:56 +0200 Subject: [PATCH 9/9] Refactoring use of array_merge and test --- src/Checks/UsedEnvironmentVariablesAreDefined.php | 8 +++++--- tests/UsedEnvironmentVariablesAreDefinedTest.php | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Checks/UsedEnvironmentVariablesAreDefined.php b/src/Checks/UsedEnvironmentVariablesAreDefined.php index e26d9a5..14fb226 100644 --- a/src/Checks/UsedEnvironmentVariablesAreDefined.php +++ b/src/Checks/UsedEnvironmentVariablesAreDefined.php @@ -108,7 +108,7 @@ private function getResult(array $values) { $envVar = $values[0]; - if (in_array($envVar, $this->processed)) { + if (in_array($envVar, $this->processed, true)) { return false; } @@ -154,12 +154,14 @@ private function recursiveDirSearch(string $folder, string $pattern): array $pattern, RegexIterator::GET_MATCH ); - $list = []; + $list = [[]]; foreach ($files as $file) { - $list = array_merge($list, $file); + $list[] = $file; } + $list = array_merge(...$list); + return $list; } } diff --git a/tests/UsedEnvironmentVariablesAreDefinedTest.php b/tests/UsedEnvironmentVariablesAreDefinedTest.php index 2ed62fa..b1fbac1 100644 --- a/tests/UsedEnvironmentVariablesAreDefinedTest.php +++ b/tests/UsedEnvironmentVariablesAreDefinedTest.php @@ -44,9 +44,9 @@ public function it_checks_if_used_env_vars_are_defined() $check = new UsedEnvironmentVariablesAreDefined(); $this->assertFalse($check->check($config)); - $this->assertTrue($check->amount === 2); - $this->assertTrue(in_array('UNDEFINED', $check->undefined)); - $this->assertTrue(in_array('GET_UNDEFINED', $check->undefined)); + $this->assertSame($check->amount, 2); + $this->assertContains('UNDEFINED', $check->undefined); + $this->assertContains('GET_UNDEFINED', $check->undefined); $this->assertSame( "2 used environmental variables are undefined: \nUNDEFINED\nGET_UNDEFINED", $check->message($config)