diff --git a/.codeclimate.yml b/.codeclimate.yml index df102ca..184634f 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -1,2 +1,9 @@ +plugins: + phpcodesniffer: + enabled: true + config: + standard: "PSR1,PSR2" + phpmd: + enabled: false exclude_patterns: - "tests/" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e935dbe..494370c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,6 +36,7 @@ jobs: - run: composer diagnose || true - run: composer install - run: composer audit + - run: composer run lint - run: composer run test - name: Test & publish code coverage if: ${{ matrix.php == '8.2' && matrix.os == 'ubuntu-latest' }} diff --git a/composer.json b/composer.json index bf5a8df..24f4323 100644 --- a/composer.json +++ b/composer.json @@ -27,16 +27,17 @@ "phpstan/extension-installer": "^1.3", "phpstan/phpstan-deprecation-rules": "^1.1", "roave/security-advisories": "dev-master", - "symfony/phpunit-bridge": "^6.3" + "symfony/phpunit-bridge": "^6.3", + "squizlabs/php_codesniffer": "^3.7" }, "autoload": { "psr-4": { - "Davereid\\DrupalEnvironment\\": "src/" + "DrupalEnvironment\\": "src/" } }, "autoload-dev": { "psr-4": { - "Davereid\\DrupalEnvironment\\Tests\\": "tests/src/" + "DrupalEnvironment\\Tests\\": "tests/src/" } }, "extra": { @@ -45,10 +46,14 @@ } }, "scripts": { + "lint": [ + "@phpcs", + "@phpstan" + ], "test": [ - "@phpstan", "@phpunit" ], + "phpcs": "vendor/bin/phpcs --standard=PSR1,PSR2 src tests", "phpstan": "vendor/bin/phpstan analyse src tests --level 5", "phpunit": "vendor/bin/phpunit tests/src --verbose --coverage-text" }, diff --git a/src/AcquiaEnvironment.php b/src/Acquia.php similarity index 83% rename from src/AcquiaEnvironment.php rename to src/Acquia.php index 248dee0..49f1581 100644 --- a/src/AcquiaEnvironment.php +++ b/src/Acquia.php @@ -1,6 +1,6 @@ 'Production', @@ -148,7 +183,7 @@ public static function getIndicatorConfig(): ?array { 'fg_color' => '#990055', ]; } - if (Environment::isLocal()) { + if (static::isLocal()) { return [ 'name' => 'Local', 'bg_color' => '#ffffff', @@ -157,7 +192,6 @@ public static function getIndicatorConfig(): ?array { } // Unknown environment condition. - return NULL; + return null; } - } diff --git a/src/Environment.php b/src/Environment.php index a4c4a7d..f7feca0 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -1,12 +1,10 @@ AcquiaEnvironment::class, - 'isPantheon' => PantheonEnvironment::class, - 'isGitHubWorkflow' => GitHubWorkflowEnvironment::class, - 'isGitLabCi' => GitLabCiEnvironment::class, - 'isCircleCi' => CircleCiEnvironment::class, + 'isAcquia' => Acquia::class, + 'isPantheon' => Pantheon::class, + 'isTugboat' => Tugboat::class, + 'isGitHubWorkflow' => GitHubWorkflow::class, + 'isGitLabCi' => GitLabCi::class, + 'isCircleCi' => CircleCi::class, null => DefaultEnvironment::class, ]; @@ -41,14 +41,13 @@ class Environment * @return string * The class name. */ - public static function getEnvironmentClass() + public static function getEnvironmentClass(): string { static $class; if (!isset($class)) { if ($class = static::get('DRUPAL_ENVIRONMENT_CLASS')) { // Do nothing. The class was assigned in the if. - } - else { + } else { // Intentionally re-assigning the class variable here so that a match // breaks the foreach loop, or we fall back to the default class. foreach (static::CLASSES as $class) { @@ -131,19 +130,6 @@ public static function isLando(): bool return (bool)static::get('LANDO_INFO'); } - /** - * Determine if this is a Tugboat environment. - * - * @return bool - * TRUE if this is a Tugboat environment. - * - * @todo Should this be its own environment class? - */ - public static function isTugboat(): bool - { - return static::getEnvironment() === 'tugboat'; - } - /** * Determines whether the current request is a command-line one. * @@ -190,8 +176,7 @@ public static function getComposerFilename(): string */ public static function getComposerLockFilename(): string { - $composer_filename = static::getComposerFilename(); - return pathinfo($composer_filename, PATHINFO_FILENAME) . '.lock'; + $filename = static::getComposerFilename(); + return pathinfo($filename, PATHINFO_FILENAME) . '.lock'; } - } diff --git a/src/GitHubWorkflowEnvironment.php b/src/GitHubWorkflow.php similarity index 78% rename from src/GitHubWorkflowEnvironment.php rename to src/GitHubWorkflow.php index bdf81d3..0c9ccfe 100644 --- a/src/GitHubWorkflowEnvironment.php +++ b/src/GitHubWorkflow.php @@ -1,13 +1,15 @@ assertTrue(Environment::commandExists('php')); $this->assertFalse(Environment::commandExists('invalid-command')); } @@ -26,28 +25,32 @@ public function testCommandExists(): void { * * @dataProvider providerEnvironment */ - public function testEnvironment(array $variables, array $method_tests): void { + public function testEnvironment(array $variables, array $method_tests): void + { $variables += [ - 'ENVIRONMENT' => NULL, - 'PANTHEON_ENVIRONMENT' => NULL, + 'ENVIRONMENT' => null, + 'PANTHEON_ENVIRONMENT' => null, // When running under CI, we need to ensure these are reset. - 'CI' => NULL, - 'GITLAB_CI' => NULL, - 'GITHUB_WORKFLOW' => NULL, + 'CI' => null, + 'GITLAB_CI' => null, + 'GITHUB_WORKFLOW' => null, ]; - $originals = []; - $this->setEnvironmentVariables($variables, $originals); + $this->setEnvironmentVariables($variables); foreach ($method_tests as $name => $expected) { $this->assertSame($expected, Environment::$name(), "Asserting Environment::$name"); } - $this->setEnvironmentVariables($originals); } /** + * Set environment variables manually for testing. + * * @param array $variables + * The variable values to set keyed by name. * @param array|null $originals + * If provided will be populated with the original variable values keyed by name. */ - protected function setEnvironmentVariables(array $variables, ?array &$originals = NULL): void { + protected function setEnvironmentVariables(array $variables, ?array &$originals = null): void + { foreach ($variables as $name => $value) { if (isset($originals)) { $originals[$name] = getenv($name) ?: null; @@ -59,25 +62,27 @@ protected function setEnvironmentVariables(array $variables, ?array &$originals /** * Data provider for ::testEnvironment. */ - public function providerEnvironment(): array { + public function providerEnvironment(): array + { return [ 'default-state' => [ [], [ 'getEnvironment' => '', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => FALSE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, 'isCli' => (PHP_SAPI === 'cli'), - 'isLocal' => FALSE, - 'getIndicatorConfig' => NULL, + 'isLocal' => false, + 'getIndicatorConfig' => null, 'getComposerFilename' => 'composer.json', 'getComposerLockFilename' => 'composer.lock', ], @@ -88,17 +93,18 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'prod', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => TRUE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => FALSE, - 'isLocal' => FALSE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => true, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Production', 'bg_color' => '#ffffff', @@ -112,18 +118,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => '', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => FALSE, - 'isLocal' => FALSE, - 'getIndicatorConfig' => NULL, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => false, + 'getIndicatorConfig' => null, ], ], 'pantheon-live' => [ @@ -132,18 +139,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'live', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => TRUE, - 'isProduction' => TRUE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isMultidev' => FALSE, - 'isCi' => FALSE, - 'isLocal' => FALSE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => true, + 'isProduction' => true, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isMultidev' => false, + 'isCi' => false, + 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Production', 'bg_color' => '#ffffff', @@ -157,18 +165,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'test', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => TRUE, - 'isProduction' => FALSE, - 'isStaging' => TRUE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isMultidev' => FALSE, - 'isCi' => FALSE, - 'isLocal' => FALSE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => true, + 'isProduction' => false, + 'isStaging' => true, + 'isDevelopment' => false, + 'isPreview' => false, + 'isMultidev' => false, + 'isCi' => false, + 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Staging', 'bg_color' => '#ffffff', @@ -182,18 +191,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'dev', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => TRUE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => TRUE, - 'isPreview' => FALSE, - 'isMultidev' => FALSE, - 'isCi' => FALSE, - 'isLocal' => FALSE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => true, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => true, + 'isPreview' => false, + 'isMultidev' => false, + 'isCi' => false, + 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Development', 'bg_color' => '#ffffff', @@ -207,18 +217,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'pr-1', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => TRUE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => TRUE, - 'isMultidev' => TRUE, - 'isCi' => FALSE, - 'isLocal' => FALSE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => true, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => true, + 'isMultidev' => true, + 'isCi' => false, + 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Preview', 'bg_color' => '#ffffff', @@ -232,19 +243,20 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'ci', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => TRUE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isMultidev' => FALSE, - 'isCi' => TRUE, - 'isLocal' => FALSE, - 'getIndicatorConfig' => NULL, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => true, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isMultidev' => false, + 'isCi' => true, + 'isLocal' => false, + 'getIndicatorConfig' => null, ], ], 'pantheon-local' => [ @@ -253,18 +265,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'local', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => TRUE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isMultidev' => FALSE, - 'isCi' => FALSE, - 'isLocal' => TRUE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => true, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isMultidev' => false, + 'isCi' => false, + 'isLocal' => true, 'getIndicatorConfig' => [ 'name' => 'Local', 'bg_color' => '#ffffff', @@ -272,6 +285,31 @@ public function providerEnvironment(): array { ], ], ], + 'tugboat' => [ + [ + 'TUGBOAT_PREVIEW_NAME' => 'phpunit', + ], + [ + 'getEnvironment' => 'phpunit', + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => true, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => true, + 'isCi' => false, + 'isLocal' => false, + 'getIndicatorConfig' => [ + 'name' => 'Preview', + 'bg_color' => '#ffffff', + 'fg_color' => '#990055', + ], + ], + ], 'circleci' => [ [ 'CI' => 'true', @@ -279,18 +317,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'ci', - 'isAcquia' => FALSE, - 'isCircleCi' => TRUE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => TRUE, - 'isLocal' => FALSE, - 'getIndicatorConfig' => NULL, + 'isAcquia' => false, + 'isCircleCi' => true, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => true, + 'isLocal' => false, + 'getIndicatorConfig' => null, ], ], 'github' => [ @@ -300,18 +339,19 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'ci', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => TRUE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => TRUE, - 'isLocal' => FALSE, - 'getIndicatorConfig' => NULL, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => true, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => true, + 'isLocal' => false, + 'getIndicatorConfig' => null, ], ], 'gitlab' => [ @@ -321,37 +361,39 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => 'ci', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => TRUE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => TRUE, - 'isLocal' => FALSE, - 'getIndicatorConfig' => NULL, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => true, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => true, + 'isLocal' => false, + 'getIndicatorConfig' => null, ], ], 'ddev' => [ [ - 'IS_DDEV_PROJECT' => TRUE, + 'IS_DDEV_PROJECT' => true, ], [ 'getEnvironment' => '', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => FALSE, - 'isLocal' => TRUE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => true, 'getIndicatorConfig' => [ 'name' => 'Local', 'bg_color' => '#ffffff', @@ -365,17 +407,18 @@ public function providerEnvironment(): array { ], [ 'getEnvironment' => '', - 'isAcquia' => FALSE, - 'isCircleCi' => FALSE, - 'isGitHubWorkflow' => FALSE, - 'isGitLabCi' => FALSE, - 'isPantheon' => FALSE, - 'isProduction' => FALSE, - 'isStaging' => FALSE, - 'isDevelopment' => FALSE, - 'isPreview' => FALSE, - 'isCi' => FALSE, - 'isLocal' => TRUE, + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => true, 'getIndicatorConfig' => [ 'name' => 'Local', 'bg_color' => '#ffffff', @@ -394,5 +437,4 @@ public function providerEnvironment(): array { ] ]; } - }