Skip to content

[v5] Add Rector CI job and run automated refactoring #2755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: 5.0
Choose a base branch
from
3 changes: 3 additions & 0 deletions .github/workflows/code_samples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Run PHPStan analysis
run: composer phpstan

- name: Run rector
run: vendor/bin/rector process --dry-run --ansi

code-samples-inclusion-check:
name: Check code samples inclusion
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

final class TranscribeAudioAction extends Action
{
private Audio $audio;

public function __construct(Audio $audio)
public function __construct(private readonly Audio $audio)
{
$this->audio = $audio;
}

public function getParameters(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
use Ibexa\Contracts\ConnectorAi\DataType;
use Ibexa\Contracts\Core\Exception\InvalidArgumentException;

final class TranscribeAudioActionType implements ActionTypeInterface
final readonly class TranscribeAudioActionType implements ActionTypeInterface
{
public const IDENTIFIER = 'transcribe_audio';

/** @var iterable<\Ibexa\Contracts\ConnectorAi\Action\ActionHandlerInterface> */
private iterable $actionHandlers;
public const string IDENTIFIER = 'transcribe_audio';

/** @param iterable<\Ibexa\Contracts\ConnectorAi\Action\ActionHandlerInterface> $actionHandlers*/
public function __construct(iterable $actionHandlers)
public function __construct(private iterable $actionHandlers)
{
$this->actionHandlers = $actionHandlers;
}

public function getIdentifier(): string
Expand Down
6 changes: 1 addition & 5 deletions code_samples/ai_actions/src/AI/DataType/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
*/
final class Audio implements DataType
{
/** @var non-empty-array<string> */
private array $base64;

/**
* @param non-empty-array<string> $base64
*/
public function __construct(array $base64)
public function __construct(private array $base64)
{
$this->base64 = $base64;
}

public function getBase64(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@
use Ibexa\Contracts\ConnectorAi\ActionResponseInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class LLaVaTextToTextActionHandler implements ActionHandlerInterface
final readonly class LLaVaTextToTextActionHandler implements ActionHandlerInterface
{
private HttpClientInterface $client;
public const string IDENTIFIER = 'LLaVATextToText';

private string $host;

public const IDENTIFIER = 'LLaVATextToText';

public function __construct(HttpClientInterface $client, string $host = 'http://localhost:8080')
public function __construct(private HttpClientInterface $client, private string $host = 'http://localhost:8080')
{
$this->client = $client;
$this->host = $host;
}

public function supports(ActionInterface $action): bool
Expand Down Expand Up @@ -63,7 +57,7 @@ public function handle(ActionInterface $action, array $context = []): ActionResp
]
);

$output = strip_tags(json_decode($response->getContent(), true)['choices'][0]['message']['content']);
$output = strip_tags((string) json_decode($response->getContent(), true)['choices'][0]['message']['content']);

return new TextResponse(new Text([$output]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

final class WhisperAudioToTextActionHandler implements ActionHandlerInterface
{
private const TIMESTAMP_FORMAT = '/^\[\d{2}:\d{2}\.\d{3} --> \d{2}:\d{2}\.\d{3}]\s*/';
private const string TIMESTAMP_FORMAT = '/^\[\d{2}:\d{2}\.\d{3} --> \d{2}:\d{2}\.\d{3}]\s*/';

public function supports(ActionInterface $action): bool
{
Expand All @@ -33,7 +33,7 @@ public function handle(ActionInterface $action, array $context = []): ActionResp

$language = $action->getRuntimeContext()?->get('languageCode');
if ($language !== null) {
$arguments[] = sprintf('--language=%s', substr($language, 0, 2));
$arguments[] = sprintf('--language=%s', substr((string) $language, 0, 2));
}

$arguments[] = '--output_format=txt';
Expand Down Expand Up @@ -72,9 +72,7 @@ private function removeTimestamps(string $text): string
{
$lines = explode(PHP_EOL, $text);

$processedLines = array_map(static function (string $line): string {
return preg_replace(self::TIMESTAMP_FORMAT, '', $line) ?? '';
}, $lines);
$processedLines = array_map(static fn (string $line): string => preg_replace(self::TIMESTAMP_FORMAT, '', $line) ?? '', $lines);

return implode(PHP_EOL, $processedLines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

final class TranscribeAudio extends BaseParser
{
public const AUDIO_KEY = 'Audio';
public const BASE64_KEY = 'base64';
public const string AUDIO_KEY = 'Audio';
public const string BASE64_KEY = 'base64';

/** @param array<mixed> $data */
public function parse(array $data, ParsingDispatcher $parsingDispatcher): TranscribeAudioAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

final class AudioText extends ValueObjectVisitor
{
private const OBJECT_IDENTIFIER = 'AudioText';
private const string OBJECT_IDENTIFIER = 'AudioText';

/**
* @param \App\AI\REST\Value\AudioText $data
*/
public function visit(Visitor $visitor, Generator $generator, $data): void
{
$mediaType = 'ai.' . self::OBJECT_IDENTIFIER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@
use App\AI\DataType\Audio;
use Ibexa\Contracts\ConnectorAi\Action\RuntimeContext;

final class TranscribeAudioAction
final readonly class TranscribeAudioAction
{
private Audio $input;

private RuntimeContext $runtimeContext;

public function __construct(
Audio $input,
RuntimeContext $runtimeContext
) {
$this->input = $input;
$this->runtimeContext = $runtimeContext;
public function __construct(private Audio $input, private RuntimeContext $runtimeContext)
{
}

public function getInput(): Audio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,13 @@
)]
final class ActionConfigurationCreateCommand extends Command
{
private ActionConfigurationServiceInterface $actionConfigurationService;

private PermissionResolver $permissionResolver;

private UserService $userService;

private ActionServiceInterface $actionService;

private ActionTypeRegistryInterface $actionTypeRegistry;

public function __construct(
ActionConfigurationServiceInterface $actionConfigurationService,
PermissionResolver $permissionResolver,
UserService $userService,
ActionServiceInterface $actionService,
ActionTypeRegistryInterface $actionTypeRegistry
private readonly ActionConfigurationServiceInterface $actionConfigurationService,
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly ActionServiceInterface $actionService,
private readonly ActionTypeRegistryInterface $actionTypeRegistry
) {
$this->actionConfigurationService = $actionConfigurationService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->actionService = $actionService;
$this->actionTypeRegistry = $actionTypeRegistry;

parent::__construct();
}

Expand Down
33 changes: 7 additions & 26 deletions code_samples/ai_actions/src/Command/AddMissingAltTextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,16 @@
)]
final class AddMissingAltTextCommand extends Command
{
private const IMAGE_FIELD_IDENTIFIER = 'image';

private ContentService $contentService;

private PermissionResolver $permissionResolver;

private UserService $userService;

private FieldTypeService $fieldTypeService;

private ActionServiceInterface $actionService;

private IOBinarydataHandler $binaryDataHandler;
private const string IMAGE_FIELD_IDENTIFIER = 'image';

public function __construct(
ContentService $contentService,
PermissionResolver $permissionResolver,
UserService $userService,
FieldTypeService $fieldTypeService,
ActionServiceInterface $actionService,
IOBinarydataHandler $binaryDataHandler
private readonly ContentService $contentService,
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly FieldTypeService $fieldTypeService,
private readonly ActionServiceInterface $actionService,
private readonly IOBinarydataHandler $binaryDataHandler
) {
$this->contentService = $contentService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->fieldTypeService = $fieldTypeService;
$this->actionService = $actionService;
$this->binaryDataHandler = $binaryDataHandler;

parent::__construct();
}

Expand Down
41 changes: 8 additions & 33 deletions code_samples/api/commerce/src/Command/CartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,16 @@
)]
final class CartCommand extends Command
{
private PermissionResolver $permissionResolver;

private UserService $userService;

private CartServiceInterface $cartService;

private CurrencyServiceInterface $currencyService;

private ProductServiceInterface $productService;

private OrderServiceInterface $orderService;

private ReorderService $reorderService;

private CartResolverInterface $cartResolver;

public function __construct(
PermissionResolver $permissionResolver,
UserService $userService,
CartServiceInterface $cartService,
CurrencyServiceInterface $currencyService,
ProductServiceInterface $productService,
OrderServiceInterface $orderService,
ReorderService $reorderService,
CartResolverInterface $cartResolver
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly CartServiceInterface $cartService,
private readonly CurrencyServiceInterface $currencyService,
private readonly ProductServiceInterface $productService,
private readonly OrderServiceInterface $orderService,
private readonly ReorderService $reorderService,
private readonly CartResolverInterface $cartResolver
) {
$this->cartService = $cartService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->currencyService = $currencyService;
$this->productService = $productService;
$this->orderService = $orderService;
$this->reorderService = $reorderService;
$this->cartResolver = $cartResolver;

parent::__construct();
}

Expand Down
16 changes: 3 additions & 13 deletions code_samples/api/commerce/src/Command/OrderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,11 @@
)]
final class OrderCommand extends Command
{
private PermissionResolver $permissionResolver;

private UserService $userService;

private OrderServiceInterface $orderService;

public function __construct(
PermissionResolver $permissionResolver,
UserService $userService,
OrderServiceInterface $orderService
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly OrderServiceInterface $orderService
) {
$this->orderService = $orderService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;

parent::__construct();
}

Expand Down
26 changes: 5 additions & 21 deletions code_samples/api/commerce/src/Command/PaymentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,13 @@
)]
final class PaymentCommand extends Command
{
private PermissionResolver $permissionResolver;

private UserService $userService;

private PaymentServiceInterface $paymentService;

private OrderServiceInterface $orderService;

private PaymentMethodServiceInterface $paymentMethodService;

public function __construct(
PermissionResolver $permissionResolver,
UserService $userService,
PaymentServiceInterface $paymentService,
OrderServiceInterface $orderService,
PaymentMethodServiceInterface $paymentMethodService,
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly PaymentServiceInterface $paymentService,
private readonly OrderServiceInterface $orderService,
private readonly PaymentMethodServiceInterface $paymentMethodService,
) {
$this->paymentService = $paymentService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->orderService = $orderService;
$this->paymentMethodService = $paymentMethodService;

parent::__construct();
}

Expand Down
16 changes: 3 additions & 13 deletions code_samples/api/commerce/src/Command/PaymentMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,11 @@
)]
final class PaymentMethodCommand extends Command
{
private PermissionResolver $permissionResolver;

private UserService $userService;

private PaymentMethodServiceInterface $paymentMethodService;

public function __construct(
PermissionResolver $permissionResolver,
UserService $userService,
PaymentMethodServiceInterface $paymentMethodService
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly PaymentMethodServiceInterface $paymentMethodService
) {
$this->paymentMethodService = $paymentMethodService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;

parent::__construct();
}

Expand Down
Loading
Loading