Skip to content

Commit

Permalink
Merge pull request #663 from tienvx/phpstan-max-mock-server-env-config
Browse files Browse the repository at this point in the history
refactor: PHPStan max level > Fix 'expects string, string|null given'
  • Loading branch information
tienvx authored Oct 1, 2024
2 parents 74c3599 + a2a4217 commit 86ec0ef
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/PhpPact/Standalone/MockService/MockServerEnvConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ class MockServerEnvConfig extends MockServerConfig
*/
public function __construct()
{
if ($host = $this->parseEnv('PACT_MOCK_SERVER_HOST', false)) {
if ($host = $this->parseEnv('PACT_MOCK_SERVER_HOST')) {
$this->setHost($host);
}

if ($port = $this->parseEnv('PACT_MOCK_SERVER_PORT', false)) {
if ($port = $this->parseEnv('PACT_MOCK_SERVER_PORT')) {
$this->setPort((int) $port);
}

$this->setConsumer($this->parseEnv('PACT_CONSUMER_NAME'));
$this->setProvider($this->parseEnv('PACT_PROVIDER_NAME'));
$this->setPactDir($this->parseEnv('PACT_OUTPUT_DIR', false));
$this->setConsumer($this->parseRequiredEnv('PACT_CONSUMER_NAME'));
$this->setProvider($this->parseRequiredEnv('PACT_PROVIDER_NAME'));
$this->setPactDir($this->parseEnv('PACT_OUTPUT_DIR'));

if ($logDir = $this->parseEnv('PACT_LOG', false)) {
if ($logDir = $this->parseEnv('PACT_LOG')) {
$this->setLog($logDir);
}

if ($logLevel = $this->parseEnv('PACT_LOGLEVEL', false)) {
if ($logLevel = $this->parseEnv('PACT_LOGLEVEL')) {
$this->setLogLevel($logLevel);
}

$version = $this->parseEnv('PACT_SPECIFICATION_VERSION', false);
$version = $this->parseEnv('PACT_SPECIFICATION_VERSION');
if (!$version) {
$version = static::DEFAULT_SPECIFICATION_VERSION;
}
Expand All @@ -47,15 +47,22 @@ public function __construct()
*
* @throws MissingEnvVariableException
*/
private function parseEnv(string $variableName, bool $required = true): ?string
private function parseEnv(string $variableName): ?string
{
$result = \getenv($variableName);

if (is_bool($result)) {
$result = null;
}

if ($required === true && $result === null) {
return $result;
}

private function parseRequiredEnv(string $variableName): string
{
$result = $this->parseEnv($variableName);

if ($result === null) {
throw new MissingEnvVariableException($variableName);
}

Expand Down

0 comments on commit 86ec0ef

Please sign in to comment.