Skip to content

Commit 48517d3

Browse files
committed
Declare duplicate conditionally to avoid coverage collection failures
1 parent 3d8bee2 commit 48517d3

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

src/AbstractTerminableCommandAfterSymfony7_3.php

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,102 +9,104 @@
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111

12-
abstract class AbstractTerminableCommand extends Command implements SignalableCommandInterface
13-
{
14-
private const REQUEST_TO_TERMINATE = 143;
12+
if (! class_exists(AbstractTerminableCommand::class)) {
13+
abstract class AbstractTerminableCommand extends Command implements SignalableCommandInterface
14+
{
15+
private const REQUEST_TO_TERMINATE = 143;
1516

16-
/** @var int */
17-
private $sleepDuration;
17+
/** @var int */
18+
private $sleepDuration;
1819

19-
/** @var bool */
20-
private $signalShutdownRequested;
20+
/** @var bool */
21+
private $signalShutdownRequested;
2122

22-
public function __construct(?string $name = null)
23-
{
24-
$this->sleepDuration = 0;
25-
$this->signalShutdownRequested = false;
23+
public function __construct(?string $name = null)
24+
{
25+
$this->sleepDuration = 0;
26+
$this->signalShutdownRequested = false;
2627

27-
parent::__construct($name);
28-
}
28+
parent::__construct($name);
29+
}
2930

30-
final protected function execute(InputInterface $input, OutputInterface $output): int
31-
{
32-
$output->writeln('Starting ' . ($this->getName() ?? static::class), OutputInterface::VERBOSITY_VERBOSE);
31+
final protected function execute(InputInterface $input, OutputInterface $output): int
32+
{
33+
$output->writeln('Starting ' . ($this->getName() ?? static::class), OutputInterface::VERBOSITY_VERBOSE);
3334

34-
if ($this->signalShutdownRequested) {
35-
$output->writeln('Signal received, skipping execution', OutputInterface::VERBOSITY_NORMAL);
35+
if ($this->signalShutdownRequested) {
36+
$output->writeln('Signal received, skipping execution', OutputInterface::VERBOSITY_NORMAL);
3637

37-
return self::REQUEST_TO_TERMINATE;
38-
}
38+
return self::REQUEST_TO_TERMINATE;
39+
}
3940

40-
$exitCode = $this->commandBody($input, $output);
41+
$exitCode = $this->commandBody($input, $output);
4142

42-
$this->sleep($output);
43+
$this->sleep($output);
4344

44-
/** @psalm-suppress DocblockTypeContradiction */
45-
if ($this->signalShutdownRequested) {
46-
$output->writeln('Signal received, terminating with exit code ' . self::REQUEST_TO_TERMINATE, OutputInterface::VERBOSITY_NORMAL);
45+
/** @psalm-suppress DocblockTypeContradiction */
46+
if ($this->signalShutdownRequested) {
47+
$output->writeln('Signal received, terminating with exit code ' . self::REQUEST_TO_TERMINATE, OutputInterface::VERBOSITY_NORMAL);
4748

48-
return self::REQUEST_TO_TERMINATE;
49+
return self::REQUEST_TO_TERMINATE;
50+
}
51+
52+
return $exitCode;
4953
}
5054

51-
return $exitCode;
52-
}
55+
abstract protected function commandBody(InputInterface $input, OutputInterface $output): int;
5356

54-
abstract protected function commandBody(InputInterface $input, OutputInterface $output): int;
57+
public function handleSignal(int $signal, $previousExitCode = 0): false
58+
{
59+
switch ($signal) {
60+
// Shutdown signals
61+
case SIGTERM:
62+
case SIGINT:
63+
$this->signalShutdownRequested = true;
64+
}
5565

56-
public function handleSignal(int $signal, $previousExitCode = 0): false
57-
{
58-
switch ($signal) {
59-
// Shutdown signals
60-
case SIGTERM:
61-
case SIGINT:
62-
$this->signalShutdownRequested = true;
66+
return false;
6367
}
6468

65-
return false;
66-
}
69+
/**
70+
* @return list<int>
71+
*/
72+
public function getSubscribedSignals(): array
73+
{
74+
return [
75+
SIGTERM,
76+
SIGINT,
77+
];
78+
}
6779

68-
/**
69-
* @return list<int>
70-
*/
71-
public function getSubscribedSignals(): array
72-
{
73-
return [
74-
SIGTERM,
75-
SIGINT,
76-
];
77-
}
80+
protected function getSleepDuration(): int
81+
{
82+
return $this->sleepDuration;
83+
}
7884

79-
protected function getSleepDuration(): int
80-
{
81-
return $this->sleepDuration;
82-
}
85+
protected function setSleepDuration(int $sleepDuration): void
86+
{
87+
if ($sleepDuration < 0) {
88+
throw new \InvalidArgumentException('Invalid timeout provided to ' . __METHOD__);
89+
}
8390

84-
protected function setSleepDuration(int $sleepDuration): void
85-
{
86-
if ($sleepDuration < 0) {
87-
throw new \InvalidArgumentException('Invalid timeout provided to ' . __METHOD__);
91+
$this->sleepDuration = $sleepDuration;
8892
}
8993

90-
$this->sleepDuration = $sleepDuration;
91-
}
94+
private function sleep(OutputInterface $output): void
95+
{
96+
if (0 === $this->sleepDuration) {
97+
return;
98+
}
9299

93-
private function sleep(OutputInterface $output): void
94-
{
95-
if (0 === $this->sleepDuration) {
96-
return;
97-
}
100+
$sleepCountDown = $this->sleepDuration;
98101

99-
$sleepCountDown = $this->sleepDuration;
102+
while (! $this->signalShutdownRequested && --$sleepCountDown) {
103+
sleep(1);
104+
}
100105

101-
while (! $this->signalShutdownRequested && --$sleepCountDown) {
102-
sleep(1);
106+
$output->writeln(
107+
sprintf('Slept %d second(s)', $this->sleepDuration - $sleepCountDown),
108+
OutputInterface::VERBOSITY_DEBUG
109+
);
103110
}
104-
105-
$output->writeln(
106-
sprintf('Slept %d second(s)', $this->sleepDuration - $sleepCountDown),
107-
OutputInterface::VERBOSITY_DEBUG
108-
);
109111
}
110112
}

0 commit comments

Comments
 (0)