Skip to content

Commit

Permalink
style: apply strict php-cs-fixer config
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed May 10, 2024
1 parent ea3ec34 commit 17e6954
Show file tree
Hide file tree
Showing 159 changed files with 1,678 additions and 1,413 deletions.
4 changes: 2 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
],
'native_function_invocation' => [
'include' => [
NativeFunctionInvocationFixer::SET_INTERNAL
NativeFunctionInvocationFixer::SET_INTERNAL,
],
'scope' => 'namespaced',
'strict' => false,
'exclude' => ['@compiler_optimized']
'exclude' => ['@compiler_optimized'],
],
]);

Expand Down
82 changes: 39 additions & 43 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Buggregator\Trap\Socket\SocketStream;
use Buggregator\Trap\Support\Timer;
use Buggregator\Trap\Traffic\Inspector;
use Fiber;

/**
* @internal
Expand All @@ -28,7 +27,7 @@ final class Application implements Processable, Cancellable, Destroyable
/** @var Server[] */
private array $servers = [];

/** @var Fiber[] Any tasks in fibers */
/** @var \Fiber[] Any tasks in fibers */
private array $fibers = [];

private readonly Buffer $buffer;
Expand Down Expand Up @@ -135,63 +134,28 @@ public function destroy(): void
public function cancel(): void
{
$this->cancelled = true;
$this->fibers[] = new Fiber(
function () {
$this->fibers[] = new \Fiber(
function (): void {
foreach ($this->servers as $server) {
$server->cancel();
}
}
);
}

/**
* @param Sender[] $senders
*/
private function sendBuffer(array $senders = []): void
{
$data = $this->buffer->getAndClean();

foreach ($senders as $sender) {
$this->fibers[] = new Fiber(
static fn() => $sender->send($data)
);
}
}

private function createServer(SocketServer $config, Inspector $inspector): Server
{
$logger = $this->logger;
$clientInflector = static function (Client $client, int $id) use ($inspector, $logger): Client {
$logger->debug('Client %d connected', $id);
$inspector->addStream(SocketStream::create($client, $id));
return $client;
};

return Server::init(
$config->port,
payloadSize: 524_288,
clientInflector: $clientInflector,
logger: $this->logger,
);
}

/**
* @param SocketServer $config
* @param Inspector $inspector
* @return Fiber
*/
public function prepareServerFiber(SocketServer $config, Inspector $inspector, Logger $logger): Fiber
public function prepareServerFiber(SocketServer $config, Inspector $inspector, Logger $logger): \Fiber
{
return $this->fibers[] = new Fiber(function () use ($config, $inspector, $logger) {
return $this->fibers[] = new \Fiber(function () use ($config, $inspector, $logger): void {
do {
try {
$this->processors[] = $this->servers[$config->port] = $this->createServer($config, $inspector);

return;
} catch (\Throwable) {
$logger->error("Can't create TCP socket on port $config->port.");
(new Timer(1.0))->wait();
}
} while (!$this->cancelled);
} while (! $this->cancelled);
});
}

Expand Down Expand Up @@ -219,4 +183,36 @@ public function configureFrontend(int $port): void
$config = $this->container->get(FrontendConfig::class);
$this->prepareServerFiber(new SocketServer(port: $config->port), $inspector, $this->logger);
}

/**
* @param Sender[] $senders
*/
private function sendBuffer(array $senders = []): void
{
$data = $this->buffer->getAndClean();

foreach ($senders as $sender) {
$this->fibers[] = new \Fiber(
static fn () => $sender->send($data)
);
}
}

private function createServer(SocketServer $config, Inspector $inspector): Server
{
$logger = $this->logger;
$clientInflector = static function (Client $client, int $id) use ($inspector, $logger): Client {
$logger->debug('Client %d connected', $id);
$inspector->addStream(SocketStream::create($client, $id));

return $client;
};

return Server::init(
$config->port,
payloadSize: 524_288,
clientInflector: $clientInflector,
logger: $this->logger,
);
}
}
13 changes: 7 additions & 6 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
*/
final class Bootstrap
{
private function __construct(
private Container $container,
) {}

public static function init(Container $container = new Container()): self
{
return new self($container);
Expand Down Expand Up @@ -47,14 +43,19 @@ public function withConfig(
];

// XML config file
$xml === null or $args['xml'] = $this->readXml($xml);
null === $xml or $args['xml'] = $this->readXml($xml);

// Register bindings
$this->container->bind(ConfigLoader::class, $args);

return $this;
}

private function __construct(
private Container $container,
) {
}

private function readXml(string $fileOrContent): string
{
// Load content
Expand All @@ -63,7 +64,7 @@ private function readXml(string $fileOrContent): string
} else {
\file_exists($fileOrContent) or throw new \InvalidArgumentException('Config file not found.');
$xml = \file_get_contents($fileOrContent);
$xml === false and throw new \RuntimeException('Failed to read config file.');
false === $xml and throw new \RuntimeException('Failed to read config file.');
}

// Validate Schema
Expand Down
35 changes: 20 additions & 15 deletions src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function if(bool|callable $condition): self
}

$this->haveToSend = $condition;

return $this;
}

Expand All @@ -62,11 +63,12 @@ public function stackTrace(): self
/**
* Set max depth for the dump.
*
* @param int<0, max> $depth If 0 - no limit.
* @param int<0, max> $depth if 0 - no limit
*/
public function depth(int $depth): self
{
$this->depth = $depth;

return $this;
}

Expand All @@ -75,8 +77,8 @@ public function depth(int $depth): self
* The counter isn't incremented if the dump is not sent (any other condition is not met).
* It might be useful for debugging in loops, recursive or just multiple function calls.
*
* @param positive-int $times Zero means no limit.
* @param bool $fullStack If true, the counter is incremented for each stack trace, not for the line.
* @param positive-int $times zero means no limit
* @param bool $fullStack if true, the counter is incremented for each stack trace, not for the line
*/
public function times(int $times, bool $fullStack = false): self
{
Expand All @@ -86,6 +88,7 @@ public function times(int $times, bool $fullStack = false): self
? $this->staticState->stackTrace
: $this->staticState->stackTrace[0]
));

return $this;
}

Expand Down Expand Up @@ -148,23 +151,18 @@ public function return(int|string $key = 0): mixed
* ```php
* trap()->context(['foo bar', => 42, 'baz' => 69]);
* ```
*
* @param mixed ...$values
*/
public function context(mixed ...$values): self
{
if (\array_keys($values) === [0] && \is_array($values[0])) {
$this->staticState->dataContext = \array_merge($this->staticState->dataContext, $values[0]);

return $this;
}

$this->staticState->dataContext = \array_merge($this->staticState->dataContext, $values);
return $this;
}

public function __destruct()
{
$this->haveToSend() and $this->sendDump();
return $this;
}

private function sendDump(): void
Expand All @@ -175,7 +173,7 @@ private function sendDump(): void

try {
// Set default values if not set
if (!isset($_SERVER['VAR_DUMPER_FORMAT'], $_SERVER['VAR_DUMPER_SERVER'])) {
if (! isset($_SERVER['VAR_DUMPER_FORMAT'], $_SERVER['VAR_DUMPER_SERVER'])) {
$_SERVER['VAR_DUMPER_FORMAT'] = 'server';
// todo use the config file in the future
$_SERVER['VAR_DUMPER_SERVER'] = '127.0.0.1:9912';
Expand All @@ -184,6 +182,7 @@ private function sendDump(): void
// Dump single value
if (\array_keys($this->values) === [0]) {
VarDumper::dump($this->values[0], depth: $this->depth);

return;
}

Expand All @@ -193,7 +192,7 @@ private function sendDump(): void
* @var mixed $value
*/
foreach ($this->values as $key => $value) {
/** @psalm-suppress TooManyArguments */
/* @psalm-suppress TooManyArguments */
VarDumper::dump($value, label: $key, depth: $this->depth);
}
} finally {
Expand All @@ -209,15 +208,21 @@ private function __construct(

private function haveToSend(): bool
{
if (!$this->haveToSend || $this->values === []) {
if (! $this->haveToSend || [] === $this->values) {
return false;
}

if ($this->times > 0) {
\assert($this->timesCounterKey !== '');
if (0 < $this->times) {
\assert('' !== $this->timesCounterKey);

return Counter::checkAndIncrement($this->timesCounterKey, $this->times);
}

return true;
}

public function __destruct()
{
$this->haveToSend() and $this->sendDump();
}
}
Loading

0 comments on commit 17e6954

Please sign in to comment.