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

Open
wants to merge 23 commits into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
$configFactory = new InternalConfigFactory();
$configFactory->withRules([
'header_comment' => false,
'method_argument_space' => [
'attribute_placement' => 'same_line'
]
]);

return $configFactory
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 @@ -13,51 +13,30 @@
use Ibexa\Contracts\Core\Collection\ArrayMap;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'app:action-configuration-create'
)]
final class ActionConfigurationCreateCommand extends Command
final readonly class ActionConfigurationCreateCommand
{
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 ActionConfigurationServiceInterface $actionConfigurationService,
private PermissionResolver $permissionResolver,
private UserService $userService,
private ActionServiceInterface $actionService,
private ActionTypeRegistryInterface $actionTypeRegistry
) {
$this->actionConfigurationService = $actionConfigurationService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->actionService = $actionService;
$this->actionTypeRegistry = $actionTypeRegistry;

parent::__construct();
}

protected function configure(): void
{
$this->addArgument('user', InputArgument::OPTIONAL, 'Login of the user executing the actions', 'admin');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$user = $input->getArgument('user');
public function __invoke(
#[Argument(name: 'user', description: 'Login of the user executing the actions')] string $user,
OutputInterface $output
): int {
$user = $user;
$this->permissionResolver->setCurrentUserReference($this->userService->loadUserByLogin($user));

$refineTextActionType = $this->actionTypeRegistry->getActionType('refine_text');
Expand Down
52 changes: 14 additions & 38 deletions code_samples/ai_actions/src/Command/AddMissingAltTextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,33 @@
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
use Ibexa\Core\FieldType\Image\Value;
use Ibexa\Core\IO\IOBinarydataHandler;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'app:add-alt-text',
)]
final class AddMissingAltTextCommand extends Command
final readonly class AddMissingAltTextCommand
{
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 ContentService $contentService,
private PermissionResolver $permissionResolver,
private UserService $userService,
private FieldTypeService $fieldTypeService,
private ActionServiceInterface $actionService,
private IOBinarydataHandler $binaryDataHandler
) {
$this->contentService = $contentService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->fieldTypeService = $fieldTypeService;
$this->actionService = $actionService;
$this->binaryDataHandler = $binaryDataHandler;

parent::__construct();
}

protected function configure(): void
{
$this->addArgument('user', InputArgument::OPTIONAL, 'Login of the user executing the actions', 'admin');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->setUser($input->getArgument('user'));
public function __invoke(
#[Argument(name: 'user', description: 'Login of the user executing the actions')] string $user,
OutputInterface $output
): int {
$this->setUser($user);

$modifiedImages = $this->getModifiedImages();
$output->writeln(sprintf('Found %d modified image in the last 24h', $modifiedImages->getTotalCount()));
Expand Down
51 changes: 12 additions & 39 deletions code_samples/api/commerce/src/Command/CartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,26 @@
use Ibexa\Core\Repository\Permission\PermissionResolver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'doc:cart'
)]
final class CartCommand extends Command
final readonly class CartCommand
{
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 PermissionResolver $permissionResolver,
private UserService $userService,
private CartServiceInterface $cartService,
private CurrencyServiceInterface $currencyService,
private ProductServiceInterface $productService,
private OrderServiceInterface $orderService,
private ReorderService $reorderService,
private 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();
}

protected function execute(InputInterface $input, OutputInterface $output): int
public function __invoke(OutputInterface $output): int
{
$this->permissionResolver->setCurrentUserReference(
$this->userService->loadUserByLogin('admin')
Expand Down Expand Up @@ -117,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->cartService->emptyCart($cart);

// Validate a cart
$violationList = $this->cartService->validateCart($cart); // Symfony\Component\Validator\ConstraintViolationListInterface
$violationList = $this->cartService->validateCart($cart);

// Add product to a cart
$product = $this->productService->getProduct('desk1');
Expand Down Expand Up @@ -163,6 +136,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// Merge the carts into the target cart and delete the merged carts
$reorderCart = $this->cartService->mergeCarts($reorderCart, true, $existingCart);

return self::SUCCESS;
return Command::SUCCESS;
}
}
Loading
Loading