Skip to content

Commit

Permalink
fix(src) phpstan pass
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Aug 17, 2023
1 parent 00ce9c5 commit 451ae85
Show file tree
Hide file tree
Showing 27 changed files with 375 additions and 342 deletions.
1 change: 0 additions & 1 deletion src/Command/DevInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Codeception\CustomCommandInterface;
use Codeception\Exception\ConfigurationException;
use Codeception\Exception\ExtensionException;
use lucatume\WPBrowser\Extension\ServiceExtensionInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Command/GenerateWPUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Codeception\Configuration;
use Codeception\CustomCommandInterface;
use lucatume\WPBrowser\Exceptions\InvalidArgumentException;
use lucatume\WPBrowser\Lib\Generator\WPUnit;
use lucatume\WPBrowser\Lib\Generator\WPUnit as WPUnitGenerator;
use lucatume\WPBrowser\Module\WPLoader;
use lucatume\WPBrowser\TestCase\WPTestCase;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/RunAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function execute(InputInterface $input, OutputInterface $output): int

foreach ($this->getSuites() as $suite) {
try {
$cwd = getcwd();
$cwd = getcwd() ?: null;
$process = new Process([$codeceptBin, 'codeception:run', $suite, ...$runOptions], $cwd);
$process->setTimeout(null);
$process->start();
Expand Down
56 changes: 52 additions & 4 deletions src/Command/ServiceExtensionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Codeception\Configuration;
use Codeception\Exception\ConfigurationException;
use lucatume\WPBrowser\Exceptions\InvalidArgumentException;
use lucatume\WPBrowser\Extension\ServiceExtension;

trait ServiceExtensionsTrait
Expand All @@ -16,6 +17,10 @@ trait ServiceExtensionsTrait
*/
protected function buildServiceExtension(string $serviceExtension): ServiceExtension
{
if (!is_a($serviceExtension, ServiceExtension::class, true)) {
$message = "The class {$serviceExtension} does not implement the " . ServiceExtension::class . " interface.";
throw new InvalidArgumentException($message);
}
$config = $this->getServiceExtensionConfig($serviceExtension);
return new $serviceExtension($config, []);
}
Expand All @@ -27,18 +32,61 @@ protected function buildServiceExtension(string $serviceExtension): ServiceExten
protected function getServiceExtensions(): array
{
$config = Configuration::config();
$enabledExtensions = $config['extensions']['enabled'] ?? [];

if (!(isset($config['extensions']) && is_array($config['extensions']))) {
return [];
}

$extensions = $config['extensions'];

if (!(isset($extensions['enabled']) && is_array($extensions['enabled']))) {
return [];
}

$enabledExtensions = $config['extensions']['enabled'];

if (!is_array($enabledExtensions)) {
return [];
}

return array_filter(
$enabledExtensions,
static fn(string $extension) => is_a($extension, ServiceExtension::class, true),
static fn($extension) => is_string($extension) && is_a($extension, ServiceExtension::class, true),
);
}

/**
* @return array<string,mixed>
* @throws ConfigurationException
*/
protected function getServiceExtensionConfig(string $serviceExtension): mixed
protected function getServiceExtensionConfig(string $serviceExtension): array
{
return Configuration::config()['extensions']['config'][$serviceExtension] ?? [];
$codeceptionConfig = Configuration::config();

if (!(isset($codeceptionConfig['extensions']) && is_array($codeceptionConfig['extensions']))) {
return [];
}

if (!(
isset($codeceptionConfig['extensions']['config'])
&& is_array($codeceptionConfig['extensions']['config']))
) {
return [];
}

if (!(
isset($codeceptionConfig['extensions']['config'][$serviceExtension])
&& is_array($codeceptionConfig['extensions']['config'][$serviceExtension]))
) {
return [];
}

$extensionConfig = $codeceptionConfig['extensions']['config'][$serviceExtension];

if (!is_array($extensionConfig)) {
return [];
}

return $extensionConfig;
}
}
8 changes: 4 additions & 4 deletions src/Extension/DockerComposeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function stop(OutputInterface $output): void
}

/**
* @param array<string,mixed> $config
* @param array<int|string,mixed> $config
*
* @throws ExtensionException
*/
Expand All @@ -109,7 +109,7 @@ protected function getComposeFile(array $config): string
}

/**
* @param array<string,mixed> $config
* @param array<int|string,mixed> $config
*
* @throws ExtensionException
*/
Expand Down Expand Up @@ -137,7 +137,7 @@ public function getPrettyName(): string
/**
* @return array{
* status: string,
* config: string|mixed[]
* config: mixed
* }
* @throws ExtensionException
*/
Expand Down Expand Up @@ -170,7 +170,7 @@ public static function getRunningFile(): string
}

/**
* @param array<string,mixed> $config
* @param array<int|string,mixed> $config
*
* @return string[]
* @throws ExtensionException
Expand Down
3 changes: 3 additions & 0 deletions src/Extension/ServiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ abstract public function stop(OutputInterface $output): void;

abstract public function getPrettyName(): string;

/**
* @return array<string,mixed>
*/
abstract public function getInfo(): array;
}
49 changes: 3 additions & 46 deletions src/Lib/Generator/WPUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Codeception\Util\Shared\Namespaces;
use Codeception\Util\Template;
use Exception;
use lucatume\WPBrowser\Compat\Compatibility;
use lucatume\WPBrowser\Exceptions\InvalidArgumentException;

/**
* Class WPUnit
Expand All @@ -27,9 +27,8 @@ class WPUnit
/**
* @var array{namespace: string, actor: string}
*/
protected array $settings;

protected string $name;
private array $settings;
private string $name;

/**
* @var string
Expand Down Expand Up @@ -76,8 +75,6 @@ public function test_factory() :void
*/
public function __construct(array $settings, string $name, protected string $baseClass)
{
$this->ensureSettings();
$this->ensureSettingsAreAllStrings();
$this->settings = $settings;
$this->name = $this->removeSuffix($name, 'Test');
}
Expand Down Expand Up @@ -133,44 +130,4 @@ protected function getTester(): string

return ltrim($testerFrag);
}


/**
* Checks the template settings.
*
* @return bool Whether the template settings are correct or not.
*/
protected function ensureSettings(): bool
{
if (empty(static::$requiredSettings)) {
return true;
}

foreach (static::$requiredSettings as $requiredSetting) {
if (!isset($this->settings[$requiredSetting])) {
throw new BadMethodCallException('Required template setting [{' . $requiredSetting . '}] is missing.');
}
}

return true;
}

/**
* Checks all settings are strings.
*/
protected function ensureSettingsAreAllStrings(): bool
{
if (empty(static::$requiredSettings)) {
return true;
}

foreach (static::$requiredSettings as $requiredSetting) {
if (!is_string($this->settings[$requiredSetting])) {
$message = 'Required template setting [{' . $requiredSetting . '}] is not a string.';
throw new BadMethodCallException($message);
}
}

return true;
}
}
4 changes: 2 additions & 2 deletions src/ManagedProcess/ChromeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
if (!(file_exists($binary) && is_executable($binary))) {
throw new RuntimeException(
"ChromeDriver binary $binary does not exist.",
ManagedProcessinterface::ERR_BINARY_NOT_FOUND
ManagedProcessInterface::ERR_BINARY_NOT_FOUND
);
}

Expand Down Expand Up @@ -72,7 +72,7 @@ private function confirmStart(Process $process): void

throw new RuntimeException(
'Could not start ChromeDriver: ' . $process->getOutput(),
ManagedProcessinterface::ERR_START
ManagedProcessInterface::ERR_START
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace lucatume\WPBrowser\ManagedProcess;

interface ManagedProcessinterface
interface ManagedProcessInterface
{
public const ERR_START = 1;
public const ERR_NO_STARTED = 2;
Expand All @@ -19,7 +19,7 @@ public function getPort(): int;

public function stop(): ?int;

public function getPid(): int;
public function getPid(): ?int;

public static function getPidFile(): string;
}
42 changes: 24 additions & 18 deletions src/ManagedProcess/ManagedProcessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
use lucatume\WPBrowser\Exceptions\RuntimeException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Throwable;

/**
* @property string $prettyName
*/
trait ManagedProcessTrait
{
private ?Process $process = null;
private ?int $pid = null;

private function writePidFile(): void
{
$pid = $this->process->getPid();
$process = $this->checkIsRunning();
$pid = $process->getPid();

if (!is_numeric($pid) || (int)$pid < 1) {
$error = new RuntimeException(
"Could not start $this->prettyName: " . $this->process->getErrorOutput(),
ManagedProcessinterface::ERR_PID
"Could not start $this->prettyName: " . $process->getErrorOutput(),
ManagedProcessInterface::ERR_PID
);
$this->stop();
throw $error;
Expand All @@ -29,20 +32,20 @@ private function writePidFile(): void
if (file_put_contents($pidFile, $pid, LOCK_EX) === false) {
throw new RuntimeException(
"Could not write PID file '$pidFile'.",
ManagedProcessinterface::ERR_PID_FILE
ManagedProcessInterface::ERR_PID_FILE
);
}
}

public function stop(): ?int
{
$this->checkStarted();
$exitCode = $this->process->stop();
$process = $this->checkStarted();
$exitCode = $process->stop();

if (is_file(static::getPidFile()) && !unlink(static::getPidFile())) {
throw new RuntimeException(
"Could not remove PID file '{static::getPidFile(}'.",
ManagedProcessinterface::ERR_PID_FILE_DELETE
ManagedProcessInterface::ERR_PID_FILE_DELETE
);
}

Expand All @@ -54,14 +57,16 @@ public static function getPidFile(): string
return codecept_output_dir(self::PID_FILE_NAME);
}

private function checkStarted(): void
private function checkStarted(): Process
{
if ($this->process === null) {
throw new RuntimeException(
"{$this->prettyName} not started.",
ManagedProcessinterface::ERR_NO_STARTED
ManagedProcessInterface::ERR_NO_STARTED
);
}

return $this->process;
}

public function getPort(): int
Expand All @@ -87,25 +92,26 @@ public function start(): void
}
throw new RuntimeException(
"Could not start $this->prettyName: " . $t->getMessage(),
ManagedProcessinterface::ERR_START,
ManagedProcessInterface::ERR_START,
$t
);
}
}

public function getPid(): int
public function getPid(): ?int
{
$this->checkIsRunning();
return $this->pid;
return $this->checkIsRunning()->getPid();
}

private function checkIsRunning(): void
private function checkIsRunning(): Process
{
if (!($this->process instanceof Process) && $this->process->isRunning()) {
if (!($this->process instanceof Process && $this->process->isRunning())) {
throw new RuntimeException(
"{$this->prettyName} is not running.",
ManagedProcessinterface::ERR_NOT_RUNNING
"$this->prettyName is not running.",
ManagedProcessInterface::ERR_NOT_RUNNING
);
}

return $this->process;
}
}
Loading

0 comments on commit 451ae85

Please sign in to comment.