Skip to content

Commit

Permalink
Make phpstan happy again
Browse files Browse the repository at this point in the history
  • Loading branch information
dinamic committed Jul 16, 2024
1 parent 6c3c74c commit 0edcaee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
use Testcontainer\Registry;
use Testcontainer\Wait\WaitForNothing;
use Testcontainer\Wait\WaitInterface;
use UnexpectedValueException;

/**
* @phpstan-type ContainerInspect array{0: array{NetworkSettings: array{IPAddress: string}}}
* @phpstan-type ContainerInspectSingleNetwork array<int, array{'NetworkSettings': array{'IPAddress': string}}>
* @phpstan-type ContainerInspectMultipleNetworks array<int, array{'NetworkSettings': array{'Networks': array<string, array{'IPAddress': string}>}}>
* @phpstan-type ContainerInspect ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks
*/
class Container
{
Expand Down Expand Up @@ -165,7 +168,7 @@ public function run(bool $wait = true): self
$inspect = new Process(['docker', 'inspect', $this->id]);
$inspect->mustRun();

/** @var ContainerInspect $inspectedData */
/** @var ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks $inspectedData */
$inspectedData = json_decode($inspect->getOutput(), true, 512, JSON_THROW_ON_ERROR);

$this->inspectedData = $inspectedData;
Expand Down Expand Up @@ -256,10 +259,20 @@ public function logs(): string

public function getAddress(): string
{
if ($this->network !== null && !empty($this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'])) {
return $this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'];
if (is_string($this->network)) {
$containerAddress = $this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'] ?? null;

if (is_string($containerAddress)) {
return $containerAddress;
}
}

$containerAddress = $this->inspectedData[0]['NetworkSettings']['IPAddress'] ?? null;

if (is_string($containerAddress)) {
return $containerAddress;
}

return $this->inspectedData[0]['NetworkSettings']['IPAddress'];
throw new UnexpectedValueException('Unable to find container IP address');
}
}
2 changes: 1 addition & 1 deletion src/Wait/WaitForTcpPortOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Testcontainer\Exception\ContainerNotReadyException;

/**
* @phpstan-type ContainerInspect array<int, array{'NetworkSettings': array{'IPAddress': string}}>
* @phpstan-import-type ContainerInspect from \Testcontainer\Container\Container
*/
final class WaitForTcpPortOpen implements WaitInterface
{
Expand Down

0 comments on commit 0edcaee

Please sign in to comment.