From 48f01921e12695ed5d2c3fd0eccbbd51c0f62626 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 30 Sep 2018 09:04:00 +0200 Subject: [PATCH] Used modern PHP features in CustomizableAppCOnfig --- composer.json | 48 ++++-- .../src/Model/CustomizableAppConfig.php | 154 +++++------------- 2 files changed, 73 insertions(+), 129 deletions(-) diff --git a/composer.json b/composer.json index f6671aa2..80c25442 100644 --- a/composer.json +++ b/composer.json @@ -89,30 +89,44 @@ "@cs", "@stan", "@test", - "@func-test", "@infect" ], + "cs": "phpcs", - "cs-fix": "phpcbf", - "serve": "php -S 0.0.0.0:8000 -t public/", - "test": "phpunit --coverage-php build/coverage-unit.cov", - "pretty-test": "phpunit --coverage-html build/coverage", - "func-test": "phpunit -c phpunit-func.xml --coverage-php build/coverage-func.cov", - "complete-pretty-test": [ - "@test", - "@func-test", + "cs:fix": "phpcbf", + "stan": "phpstan analyse module/*/src/ --level=6 -c phpstan.neon", + + "test": [ + "@test:unit", + "@test:func" + ], + "test:unit": "phpunit --coverage-php build/coverage-unit.cov", + "test:func": "phpunit -c phpunit-func.xml --coverage-php build/coverage-func.cov", + + "test:pretty": [ + "@test:unit", + "@test:func", "phpcov merge build --html build/html" ], - "stan": "phpstan analyse module/*/src/ --level=6 -c phpstan.neon", + "test:unit:pretty": "phpunit --coverage-html build/coverage", + "infect": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2", - "infect-show": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2 --show-mutations", - "expressive": "expressive" + "infect:show": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2 --show-mutations" + }, + "scripts-descriptions": { + "check": "Alias for \"cs\", \"stan\", \"test\" and \"infect\"", + "cs": "Checks coding styles", + "cs:fix": "Fixes coding styles, when possible", + "stan": "Inspects code with phpstan", + "test": "Runs all test suites", + "test:unit": "Runs unit test suites", + "test:func": "Runs functional test suites (covering entity repositories)", + "test:pretty": "Runs all test suites and generates an HTML code coverage report", + "test:unit:pretty": "Runs unit test suites and generates an HTML code coverage report", + "infect": "Checks unit tests quality applying mutation testing", + "infect:show": "Checks unit tests quality applying mutation testing and shows applied mutators" }, "config": { - "process-timeout": 0, - "sort-packages": true, - "platform": { - "php": "7.1.8" - } + "sort-packages": true } } diff --git a/module/Installer/src/Model/CustomizableAppConfig.php b/module/Installer/src/Model/CustomizableAppConfig.php index e5c1eda2..06aeaf4f 100644 --- a/module/Installer/src/Model/CustomizableAppConfig.php +++ b/module/Installer/src/Model/CustomizableAppConfig.php @@ -12,191 +12,126 @@ final class CustomizableAppConfig implements ArraySerializableInterface /** * @var array */ - private $database; + private $database = []; /** * @var array */ - private $urlShortener; + private $urlShortener = []; /** * @var array */ - private $language; + private $language = []; /** * @var array */ - private $app; + private $app = []; /** - * @var string + * @var string|null */ private $importedInstallationPath; - /** - * @return array - */ - public function getDatabase() + public function getDatabase(): array { return $this->database; } - /** - * @param array $database - * @return $this - */ - public function setDatabase(array $database) + public function setDatabase(array $database): self { $this->database = $database; return $this; } - /** - * @return bool - */ - public function hasDatabase() + public function hasDatabase(): bool { return ! empty($this->database); } - /** - * @return array - */ - public function getUrlShortener() + public function getUrlShortener(): array { return $this->urlShortener; } - /** - * @param array $urlShortener - * @return $this - */ - public function setUrlShortener(array $urlShortener) + public function setUrlShortener(array $urlShortener): self { $this->urlShortener = $urlShortener; return $this; } - /** - * @return bool - */ - public function hasUrlShortener() + public function hasUrlShortener(): bool { return ! empty($this->urlShortener); } - /** - * @return array - */ - public function getLanguage() + public function getLanguage(): array { return $this->language; } - /** - * @param array $language - * @return $this - */ - public function setLanguage(array $language) + public function setLanguage(array $language): self { $this->language = $language; return $this; } - /** - * @return bool - */ - public function hasLanguage() + public function hasLanguage(): bool { return ! empty($this->language); } - /** - * @return array - */ - public function getApp() + public function getApp(): array { return $this->app; } - /** - * @param array $app - * @return $this - */ - public function setApp(array $app) + public function setApp(array $app): self { $this->app = $app; return $this; } - /** - * @return bool - */ - public function hasApp() + public function hasApp(): bool { return ! empty($this->app); } - /** - * @return string - */ - public function getImportedInstallationPath() + public function getImportedInstallationPath(): ?string { return $this->importedInstallationPath; } - /** - * @param string $importedInstallationPath - * @return $this|self - */ - public function setImportedInstallationPath($importedInstallationPath) + public function setImportedInstallationPath(string $importedInstallationPath): self { $this->importedInstallationPath = $importedInstallationPath; return $this; } - /** - * @return bool - */ - public function hasImportedInstallationPath() + public function hasImportedInstallationPath(): bool { return $this->importedInstallationPath !== null; } - /** - * Exchange internal values from provided array - * - * @param array $array - * @return void - */ - public function exchangeArray(array $array) + public function exchangeArray(array $array): void { - if (isset($array['app_options'], $array['app_options']['secret_key'])) { - $this->setApp([ - 'SECRET' => $array['app_options']['secret_key'], - ]); - } + $this->setApp([ + 'SECRET' => $array['app_options']['secret_key'] ?? null, + ]); - if (isset($array['entity_manager'], $array['entity_manager']['connection'])) { - $this->deserializeDatabase($array['entity_manager']['connection']); - } + $this->deserializeDatabase($array['entity_manager']['connection'] ?? []); - if (isset($array['translator'], $array['translator']['locale'], $array['cli'], $array['cli']['locale'])) { - $this->setLanguage([ - 'DEFAULT' => $array['translator']['locale'], - 'CLI' => $array['cli']['locale'], - ]); - } + $this->setLanguage([ + 'DEFAULT' => $array['translator']['locale'] ?? null, + 'CLI' => $array['cli']['locale'] ?? null, + ]); - if (isset($array['url_shortener'])) { - $urlShortener = $array['url_shortener']; - $this->setUrlShortener([ - 'SCHEMA' => $urlShortener['domain']['schema'], - 'HOSTNAME' => $urlShortener['domain']['hostname'], - 'CHARS' => $urlShortener['shortcode_chars'], - 'VALIDATE_URL' => $urlShortener['validate_url'] ?? true, - ]); - } + $this->setUrlShortener([ + 'SCHEMA' => $array['url_shortener']['domain']['schema'] ?? null, + 'HOSTNAME' => $array['url_shortener']['domain']['hostname'] ?? null, + 'CHARS' => $array['url_shortener']['shortcode_chars'] ?? null, + 'VALIDATE_URL' => $array['url_shortener']['validate_url'] ?? true, + ]); } - private function deserializeDatabase(array $conn) + private function deserializeDatabase(array $conn): void { if (! isset($conn['driver'])) { return; @@ -205,22 +140,17 @@ final class CustomizableAppConfig implements ArraySerializableInterface $params = ['DRIVER' => $driver]; if ($driver !== 'pdo_sqlite') { - $params['USER'] = $conn['user']; - $params['PASSWORD'] = $conn['password']; - $params['NAME'] = $conn['dbname']; - $params['HOST'] = $conn['host']; - $params['PORT'] = $conn['port']; + $params['USER'] = $conn['user'] ?? null; + $params['PASSWORD'] = $conn['password'] ?? null; + $params['NAME'] = $conn['dbname'] ?? null; + $params['HOST'] = $conn['host'] ?? null; + $params['PORT'] = $conn['port'] ?? null; } $this->setDatabase($params); } - /** - * Return an array representation of the object - * - * @return array - */ - public function getArrayCopy() + public function getArrayCopy(): array { $config = [ 'app_options' => [