Skip to content

Commit

Permalink
strong typing and no globals
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Aug 23, 2022
1 parent 7484c46 commit ef6e86d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
55 changes: 28 additions & 27 deletions src/AcquiaDrupalEnvironmentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AcquiaDrupalEnvironmentDetector {
/**
* Is AH env.
*/
public static function isAhEnv() {
public static function isAhEnv(): bool {
return (bool) self::getAhEnv();
}

Expand All @@ -19,17 +19,17 @@ public static function isAhEnv() {
*
* Roughly duplicates the detection logic implemented by the ACSF module.
*
* @param mixed $ah_group
* @param string|null $ah_group
* The Acquia Hosting site / group name (e.g. my_subscription).
* @param mixed $ah_env
* @param string|null $ah_env
* The Acquia Hosting environment name (e.g. 01dev).
*
* @return bool
* TRUE if this is an ACSF environment, FALSE otherwise.
*
* @see https://git.drupalcode.org/project/acsf/blob/8.x-2.62/acsf_init/lib/sites/default/acsf.settings.php#L14
*/
public static function isAcsfEnv($ah_group = NULL, $ah_env = NULL) {
public static function isAcsfEnv(string $ah_group = NULL, string $ah_env = NULL): bool {
if (is_null($ah_group)) {
$ah_group = self::getAhGroup();
}
Expand All @@ -54,7 +54,7 @@ public static function isAcsfEnv($ah_group = NULL, $ah_env = NULL) {
* @return bool
* TRUE if prod, FALSE otherwise.
*/
public static function isAhProdEnv($ah_env = NULL) {
public static function isAhProdEnv(string $ah_env = NULL): bool {
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
Expand All @@ -68,9 +68,9 @@ public static function isAhProdEnv($ah_env = NULL) {
* Environment machine name.
*
* @return bool
* TRUE if stage, FALSE otherwise.
* TRUE if 'stage', FALSE otherwise.
*/
public static function isAhStageEnv($ah_env = NULL) {
public static function isAhStageEnv(string $ah_env = NULL): bool {
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
Expand All @@ -83,10 +83,10 @@ public static function isAhStageEnv($ah_env = NULL) {
* @param string|null $ah_env
* Environment machine name.
*
* @return false|int
* @return bool
* TRUE if dev, FALSE otherwise.
*/
public static function isAhDevEnv($ah_env = NULL) {
public static function isAhDevEnv(string $ah_env = NULL): bool {
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
Expand All @@ -99,10 +99,10 @@ public static function isAhDevEnv($ah_env = NULL) {
* @param string|null $ah_env
* Environment machine name.
*
* @return false|int
* @return bool
* TRUE if ODE, FALSE otherwise.
*/
public static function isAhOdeEnv($ah_env = NULL) {
public static function isAhOdeEnv(string $ah_env = NULL): bool {
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
Expand All @@ -118,7 +118,7 @@ public static function isAhOdeEnv($ah_env = NULL) {
* @return bool
* TRUE if IDE, FALSE otherwise.
*/
public static function isAhIdeEnv($ah_env = NULL) {
public static function isAhIdeEnv(string $ah_env = NULL): bool {
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
Expand All @@ -130,8 +130,8 @@ public static function isAhIdeEnv($ah_env = NULL) {
*
* The devcloud realm includes Acquia Cloud Professional (ACP).
*/
public static function isAhDevCloud() {
return self::getAhRealm() == 'devcloud';
public static function isAhDevCloud(): bool {
return self::getAhRealm() === 'devcloud';
}

/**
Expand All @@ -140,7 +140,7 @@ public static function isAhDevCloud() {
* @return string
* Site group (usually a customer name).
*/
public static function getAhGroup() {
public static function getAhGroup(): string {
return getenv('AH_SITE_GROUP');
}

Expand All @@ -150,7 +150,7 @@ public static function getAhGroup() {
* @return string
* Environment name (e.g. dev, stage, prod).
*/
public static function getAhEnv() {
public static function getAhEnv(): string {
return getenv('AH_SITE_ENVIRONMENT');
}

Expand All @@ -160,21 +160,21 @@ public static function getAhEnv() {
* @return string
* Realm name (e.g. prod, gardens).
*/
public static function getAhRealm() {
public static function getAhRealm(): string {
return getenv('AH_REALM');
}

/**
* Get AH non production.
*/
public static function getAhNonProduction() {
public static function getAhNonProduction(): string {
return getenv('AH_NON_PRODUCTION');
}

/**
* Get AH application UUID.
*/
public static function getAhApplicationUuid() {
public static function getAhApplicationUuid(): string {
return getenv('AH_APPLICATION_UUID');
}

Expand All @@ -186,7 +186,7 @@ public static function getAhApplicationUuid() {
*
* @see https://docs.acquia.com/acquia-cloud/manage/files/about/
*/
public static function getAhFilesRoot() {
public static function getAhFilesRoot(): string {
return FilePaths::ahFilesRoot(self::getAhGroup(), self::getAhEnv());
}

Expand All @@ -196,8 +196,9 @@ public static function getAhFilesRoot() {
* @return string|null
* ACSF db name.
*/
public static function getAcsfDbName() {
return isset($GLOBALS['gardens_site_settings']) && self::isAcsfEnv() ? $GLOBALS['gardens_site_settings']['conf']['acsf_db_name'] : NULL;
public static function getAcsfDbName(): ?string {
$gardens_site_settings = getenv('gardens_site_settings');
return is_array($gardens_site_settings) && self::isAcsfEnv() ? $gardens_site_settings['conf']['acsf_db_name'] : NULL;
}

/**
Expand All @@ -214,7 +215,7 @@ public static function getAcsfDbName() {
* @return string|null
* Site name.
*/
public static function getSiteName($site_path) {
public static function getSiteName(string $site_path): ?string {
if (self::isAcsfEnv()) {
return self::getAcsfDbName();
}
Expand All @@ -225,28 +226,28 @@ public static function getSiteName($site_path) {
/**
* Is this a Lando environment using the Acquia recipe.
*/
public static function isAcquiaLandoEnv() {
public static function isAcquiaLandoEnv(): bool {
return getenv('AH_SITE_ENVIRONMENT') === 'LANDO';
}

/**
* Is this a Lando environment.
*/
public static function isLandoEnv() {
public static function isLandoEnv(): bool {
return getenv('LANDO') === 'ON';
}

/**
* Get Lando info.
*/
public static function getLandoInfo() {
public static function getLandoInfo(): string {
return getenv('LANDO_INFO');
}

/**
* If this isn't a Cloud environment, assume it's local.
*/
public static function isLocalEnv() {
public static function isLocalEnv(): bool {
return !self::isAhEnv() || self::isAcquiaLandoEnv();
}

Expand Down
20 changes: 10 additions & 10 deletions src/EnvironmentNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EnvironmentNames {
* @return bool
* TRUE if prod, FALSE otherwise.
*/
public static function isAhProdEnv($ah_env) {
public static function isAhProdEnv(string $ah_env): bool {
// ACE prod is 'prod'; ACSF can be '01live', '02live', ...
return $ah_env === 'prod' || preg_match('/^\d*live$/', $ah_env);
}
Expand All @@ -34,9 +34,9 @@ public static function isAhProdEnv($ah_env) {
* Environment machine name.
*
* @return bool
* TRUE if stage, FALSE otherwise.
* TRUE if 'stage', FALSE otherwise.
*/
public static function isAhStageEnv($ah_env) {
public static function isAhStageEnv(string $ah_env): bool {
// ACE staging is 'test', 'stg', or 'stage'; ACSF is '01test', '02test', ...
return preg_match('/^\d*test$/', $ah_env) || $ah_env === 'stg' || $ah_env === 'stage';
}
Expand All @@ -47,12 +47,12 @@ public static function isAhStageEnv($ah_env) {
* @param string $ah_env
* Environment machine name.
*
* @return false|int
* @return bool
* TRUE if dev, FALSE otherwise.
*/
public static function isAhDevEnv($ah_env) {
public static function isAhDevEnv(string $ah_env): bool {
// ACE dev is 'dev', 'dev1', ...; ACSF dev is '01dev', '02dev', ...
return (preg_match('/^\d*dev\d*$/', $ah_env));
return (bool) preg_match('/^\d*dev\d*$/', $ah_env);
}

/**
Expand All @@ -61,12 +61,12 @@ public static function isAhDevEnv($ah_env) {
* @param string $ah_env
* Environment machine name.
*
* @return false|int
* @return bool
* TRUE if ODE, FALSE otherwise.
*/
public static function isAhOdeEnv($ah_env) {
public static function isAhOdeEnv(string $ah_env): bool {
// CDEs (formerly 'ODEs') can be 'ode1', 'ode2', ...
return (preg_match('/^ode\d*$/', $ah_env));
return (bool) preg_match('/^ode\d*$/', $ah_env);
}

/**
Expand All @@ -78,7 +78,7 @@ public static function isAhOdeEnv($ah_env) {
* @return bool
* TRUE if IDE, FALSE otherwise.
*/
public static function isAhIdeEnv($ah_env) {
public static function isAhIdeEnv(string $ah_env): bool {
return strtolower($ah_env) === 'ide';
}

Expand Down

0 comments on commit ef6e86d

Please sign in to comment.