Skip to content

Commit

Permalink
[rector] Add rector on player
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Jan 21, 2025
1 parent d9945c4 commit aebbfe2
Show file tree
Hide file tree
Showing 115 changed files with 805 additions and 674 deletions.
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ box_image = blackfire/php-internal:8.3-v1.0.62
BOX_BIN=bin/tools/box-$(box_version).phar
PHAR_DIST=bin/blackfire-player.phar
BASE_PHP=@docker run --rm -e "PHP_CS_FIXER_IGNORE_ENV=1" -u `id -u`:`id -g` -v "$(HOME)/.composer:/.composer" -v "$(HOME)/.phive:/.phive" -v "$(PWD):/app" -e HOME=/ -i
PHP= $(BASE_PHP) $(php_image)
PHP_TTY=$(BASE_PHP) -t $(php_image)
BOX=@docker run --rm -v $(PWD):/app -w /app $(box_image)
ifdef CI
PHP= $(BASE_PHP) $(php_image)
else
PHP= $(BASE_PHP) -t $(php_image)
endif

ifdef BUILDKITE
define section_start
Expand Down Expand Up @@ -68,6 +71,12 @@ php-cs-fix: bin/tools/php-cs-fixer vendor/autoload.php ## Analyze and fix PHP co
@$(PHP) php -dmemory_limit=-1 ./bin/tools/php-cs-fixer fix --config=.php-cs-fixer.dist.php
.PHONY: php-cs-fix

rector-fix: vendor/bin/rector vendor/autoload.php ## Analyze and fix PHP code with rector
@$(PHP) php -dmemory_limit=-1 ./vendor/bin/rector
.PHONY: rector-fix

vendor/bin/rector: vendor/autoload.php

phpstan: bin/tools/phpstan vendor/autoload.php ## Analyze PHP code with phpstan
@$(call section_start, $@, "Running PHPStan", false)

Expand All @@ -77,7 +86,7 @@ phpstan: bin/tools/phpstan vendor/autoload.php ## Analyze PHP code with phpstan
.PHONY: phpstan

shell: ## Starts a shell in container
@$(PHP_TTY) bash
@$(PHP) bash

package-test: $(BOX_BIN) vendor/autoload.php ## Tests the phar release
@# The box.no-git.json configuration file disables git placeholder, avoiding git calls during packaging
Expand Down
10 changes: 6 additions & 4 deletions Player/BuildApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ public function updateBuild(Build $build, string $jsonView): void
// as we might run the player in concurrent mode using fibers, we need to mark the fiber suspended to allow
// asynchronous response processing
foreach ($this->blackfireHttpClient->stream($response, 0.01) as $chunk) {
if ($chunk->isTimeout()) {
if (\Fiber::getCurrent()) {
\Fiber::suspend();
}
if (!$chunk->isTimeout()) {
continue;
}
if (null === \Fiber::getCurrent()) {
continue;
}
\Fiber::suspend();
}
}
}
6 changes: 3 additions & 3 deletions Player/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function renderThrowable(\Throwable $e, OutputInterface $output): void
$lines = ['[ERROR]'];

$terminal = new Terminal();
$width = $terminal->getWidth() ? $terminal->getWidth() - 1 : \PHP_INT_MAX;
$width = 0 !== $terminal->getWidth() ? $terminal->getWidth() - 1 : \PHP_INT_MAX;

foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
Expand All @@ -56,7 +56,7 @@ public function renderThrowable(\Throwable $e, OutputInterface $output): void
$output->writeln($formatter->formatBlock($lines, 'error', true), OutputInterface::VERBOSITY_QUIET);
$output->writeln('', OutputInterface::VERBOSITY_QUIET);

if (!\Phar::running() && $output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
if ('' !== \Phar::running() && $output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
parent::renderThrowable($e, $output);
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ private function splitStringByWidth(string $string, int $width): array
}
}

$lines[] = \count($lines) ? str_pad($line, $width) : $line;
$lines[] = [] !== $lines ? str_pad($line, $width) : $line;

mb_convert_variables($encoding, 'utf8', $lines);

Expand Down
2 changes: 1 addition & 1 deletion Player/Console/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
final class ConsoleLogger extends BaseConsoleLogger
{
private const ERROR_LEVELS = [
private const array ERROR_LEVELS = [
LogLevel::EMERGENCY => 1,
LogLevel::ALERT => 1,
LogLevel::CRITICAL => 1,
Expand Down
2 changes: 1 addition & 1 deletion Player/Console/OutputErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function install(SymfonyApplication $application): void

$application->setDispatcher($dispatcher);

$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event): void {
if ($event->getInput()->hasOption('json') && $event->getInput()->getOption('json')) {
$extra = ['errors' => []];

Expand Down
42 changes: 12 additions & 30 deletions Player/Console/PlayerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,14 @@
*/
final class PlayerCommand extends Command
{
public const EXIT_CODE_EXPECTATION_ERROR = 64;
public const EXIT_CODE_SCENARIO_ERROR = 65;
public const EXIT_CODE_SCENARIO_ERROR_NON_FATAL = 66;
public const EXIT_CODE_BLACKFIRE_NETWORK_ERROR = 67;

private HttpClientInterface|null $blackfireHttpClient;
private BlackfireSdkAdapterInterface|null $blackfireSdkAdapter;
private string $transactionId;
public const int EXIT_CODE_EXPECTATION_ERROR = 64;
public const int EXIT_CODE_SCENARIO_ERROR = 65;
public const int EXIT_CODE_SCENARIO_ERROR_NON_FATAL = 66;
public const int EXIT_CODE_BLACKFIRE_NETWORK_ERROR = 67;

public function __construct(
HttpClientInterface|null $blackfireHttpClient, BlackfireSdkAdapterInterface|null $blackfireSdkAdapter, string $transactionId)
private HttpClientInterface|null $blackfireHttpClient, private BlackfireSdkAdapterInterface|null $blackfireSdkAdapter, private readonly string $transactionId)
{
$this->blackfireHttpClient = $blackfireHttpClient;
$this->blackfireSdkAdapter = $blackfireSdkAdapter;
$this->transactionId = $transactionId;

parent::__construct();
}

Expand Down Expand Up @@ -134,7 +126,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

// The Blackfire SDK Adapter is always null in production. We only inject one for testing purpose.
if (!$this->blackfireSdkAdapter) {
if (null === $this->blackfireSdkAdapter) {
$clientConfiguration = null;
if (null !== $configFile = $input->getOption('config')) {
$clientConfiguration = ClientConfiguration::createFromFile($configFile);
Expand All @@ -156,7 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'endpoint' => $endpoint,
] = $this->getConfig($this->blackfireSdkAdapter);

if (!$this->blackfireHttpClient) {
if (null === $this->blackfireHttpClient) {
$errorMessagePattern = 'Missing required "%s" configuration. Either configure it using "%s" environment variable or in your .blackfire.ini file';

if (!$clientId) {
Expand Down Expand Up @@ -189,7 +181,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
stream_copy_to_stream($stdin, $copy);
$input->setArgument('file', $copy);
} else {
$extension = pathinfo($input->getArgument('file'), \PATHINFO_EXTENSION);
$extension = pathinfo((string) $input->getArgument('file'), \PATHINFO_EXTENSION);
if ('yml' === $extension || 'yaml' === $extension) {
$sandbox = true;
}
Expand All @@ -213,7 +205,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->getEnvOrDefault('BLACKFIRE_BASIC_AUTH_PASSWORD'),
]);

if (!empty($authBasic) && $input->getOption('endpoint')) {
if ([] !== $authBasic && $input->getOption('endpoint')) {
$httpClient = ScopingHttpClient::forBaseUri($httpClient, $input->getOption('endpoint'), [
...$httpClientOptions,
'auth_basic' => $authBasic,
Expand Down Expand Up @@ -245,7 +237,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);

if ($input->getOption('step')) {
$player->addExtension(new InteractiveStepByStepExtension($this->getHelper('question'), $input, $output, $variableResolver), 2048);
$player->addExtension(new InteractiveStepByStepExtension($this->getHelper('question'), $input, $output), 2048);
}

$player->addExtension(new TmpDirExtension($filesystem));
Expand Down Expand Up @@ -346,18 +338,8 @@ private function createReport(ScenarioSetResult $results): array
private function getEnvOrDefault(string $envVar, string|null $default = null): string|null
{
$env = getenv($envVar);
if (!$env) {
$env = $default;
}

return $env;
}

private function getEnvOrThrow(string $envVar): string
{
$env = getenv($envVar);
if (!$env) {
throw new \InvalidArgumentException(\sprintf('Missing required "%s" environment variable', $envVar));
if ('' === (string) $env) {
return $default;
}

return $env;
Expand Down
2 changes: 1 addition & 1 deletion Player/Console/ScenarioHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getVariables(InputInterface $input): array
}

foreach ($input->getOption('variable') as $variable) {
[$key, $value] = explode('=', $variable, 2);
[$key, $value] = explode('=', (string) $variable, 2);
$variables[$key] = $this->escapeValue($value);
}

Expand Down
6 changes: 3 additions & 3 deletions Player/Console/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
final class ValidateCommand extends Command
{
public const EXIT_CODE_FAILURE = 64;
public const int EXIT_CODE_FAILURE = 64;

protected function configure(): void
{
Expand Down Expand Up @@ -81,15 +81,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]));
} elseif ($result->isSuccess()) {
$output->writeln('<info>The scenarios are valid.</>');
if ($missingVariables = $result->getMissingVariables()) {
if ([] !== $missingVariables = $result->getMissingVariables()) {
$io = new SymfonyStyle($input, $output);
$io->note(array_merge(['You need to define the following variables using the `--variable` option:'], $missingVariables));
}
} else {
$output->writeln('<info>The scenarios are not valid:</>');

foreach ($result->getErrors() as $error) {
$output->writeln(" - $error");
$output->writeln(' - '.$error);
}

return self::EXIT_CODE_FAILURE;
Expand Down
6 changes: 1 addition & 5 deletions Player/Exception/ExpectationFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
*/
class ExpectationFailureException extends LogicException
{
private array $results;

public function __construct(string|null $message = null, array $results = [], int $code = 0, \Exception|null $previous = null)
public function __construct(string|null $message = null, private readonly array $results = [], int $code = 0, \Exception|null $previous = null)
{
parent::__construct($message, $code, $previous);

$this->results = $results;
}

public function getResults(): array
Expand Down
11 changes: 7 additions & 4 deletions Player/ExpressionLanguage/ExtractResultsVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
namespace Blackfire\Player\ExpressionLanguage;

use Symfony\Component\ExpressionLanguage\Node;
use Symfony\Component\ExpressionLanguage\Node\FunctionNode;
use Symfony\Component\ExpressionLanguage\Node\GetAttrNode;
use Symfony\Component\ExpressionLanguage\Node\NameNode;
use Symfony\Component\ExpressionLanguage\ParsedExpression;

/**
* @internal
*/
class ExtractResultsVisitor
{
private const IGNORED_FUNCTIONS = [
private const array IGNORED_FUNCTIONS = [
'constant',
'link',
'css',
Expand Down Expand Up @@ -50,9 +53,9 @@ private function visit(Node\Node $node, array $variables, Node\Node|null $parent
$subExpressions = array_merge(...$subExpressions);

if (
$node instanceof Node\NameNode && (!$parentNode instanceof Node\GetAttrNode)
|| $node instanceof Node\GetAttrNode
|| $node instanceof Node\FunctionNode && !\in_array($node->attributes['name'], self::IGNORED_FUNCTIONS, true)
$node instanceof NameNode && (!$parentNode instanceof GetAttrNode)
|| $node instanceof GetAttrNode
|| $node instanceof FunctionNode && !\in_array($node->attributes['name'], self::IGNORED_FUNCTIONS, true)
) {
$subExpressions[] = [
'expression' => $node->dump(),
Expand Down
Loading

0 comments on commit aebbfe2

Please sign in to comment.