Make phpstan happy again

This commit is contained in:
Nikola Petkanski
2024-07-16 03:11:34 +03:00
parent 6c3c74c817
commit 0edcaeec72
2 changed files with 19 additions and 6 deletions
+18 -5
View File
@@ -9,9 +9,12 @@ use Testcontainer\Exception\ContainerNotReadyException;
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
{
@@ -165,7 +168,7 @@ class Container
$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;
@@ -256,10 +259,20 @@ class Container
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;
}
}
return $this->inspectedData[0]['NetworkSettings']['IPAddress'];
$containerAddress = $this->inspectedData[0]['NetworkSettings']['IPAddress'] ?? null;
if (is_string($containerAddress)) {
return $containerAddress;
}
throw new UnexpectedValueException('Unable to find container IP address');
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ use Symfony\Component\Process\Process;
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
{