DX: apply rector rules up to php8.0

This commit is contained in:
2023-04-15 01:05:37 +02:00
parent d8870e906f
commit dde3002100
714 changed files with 2348 additions and 9263 deletions

View File

@@ -17,13 +17,10 @@ use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
final class ContextManager implements ContextManagerInterface
{
/**
* @var DocGeneratorContextInterface[]|iterable
* @param \Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface[] $contexts
*/
private iterable $contexts;
public function __construct(iterable $contexts)
public function __construct(private iterable $contexts)
{
$this->contexts = $contexts;
}
public function getContextByDocGeneratorTemplate(DocGeneratorTemplate $docGeneratorTemplate): DocGeneratorContextInterface

View File

@@ -21,6 +21,6 @@ class UnexpectedTypeException extends LogicException
{
public function __construct($value, string $expectedType)
{
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, is_object($value) ? get_class($value) : gettype($value)));
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, get_debug_type($value)));
}
}

View File

@@ -22,11 +22,8 @@ use Symfony\Component\Routing\Annotation\Route;
class AdminDocGeneratorTemplateController extends CRUDController
{
private ContextManager $contextManager;
public function __construct(ContextManager $contextManager)
public function __construct(private ContextManager $contextManager)
{
$this->contextManager = $contextManager;
}
public function generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = [])

View File

@@ -43,32 +43,8 @@ use const JSON_PRETTY_PRINT;
final class DocGeneratorTemplateController extends AbstractController
{
private ContextManager $contextManager;
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private EntityManagerInterface $entityManager;
private GeneratorInterface $generator;
private MessageBusInterface $messageBus;
private PaginatorFactory $paginatorFactory;
public function __construct(
ContextManager $contextManager,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
GeneratorInterface $generator,
MessageBusInterface $messageBus,
PaginatorFactory $paginatorFactory,
EntityManagerInterface $entityManager
) {
$this->contextManager = $contextManager;
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->generator = $generator;
$this->messageBus = $messageBus;
$this->paginatorFactory = $paginatorFactory;
$this->entityManager = $entityManager;
public function __construct(private ContextManager $contextManager, private DocGeneratorTemplateRepository $docGeneratorTemplateRepository, private GeneratorInterface $generator, private MessageBusInterface $messageBus, private PaginatorFactory $paginatorFactory, private EntityManagerInterface $entityManager)
{
}
/**

View File

@@ -24,11 +24,8 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class DocGeneratorTemplateType extends AbstractType
{
private ContextManager $contextManager;
public function __construct(ContextManager $contextManager)
public function __construct(private ContextManager $contextManager)
{
$this->contextManager = $contextManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)

View File

@@ -20,12 +20,9 @@ use Throwable;
*/
class TemplateException extends RuntimeException
{
private array $errors;
public function __construct(array $errors, $code = 0, ?Throwable $previous = null)
public function __construct(private array $errors, $code = 0, ?Throwable $previous = null)
{
parent::__construct('Error while generating document from template', $code, $previous);
$this->errors = $errors;
}
public function getErrors(): array

View File

@@ -24,19 +24,13 @@ use Throwable;
final class RelatorioDriver implements DriverInterface
{
private HttpClientInterface $client;
private LoggerInterface $logger;
private string $url;
public function __construct(
HttpClientInterface $client,
private HttpClientInterface $client,
ParameterBagInterface $parameterBag,
LoggerInterface $logger
private LoggerInterface $logger
) {
$this->client = $client;
$this->logger = $logger;
$this->url = $parameterBag->get('chill_doc_generator')['driver']['relatorio']['url'];
}

View File

@@ -18,14 +18,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class AdminMenuBuilder implements LocalMenuBuilderInterface
{
private Security $security;
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator, Security $security)
public function __construct(private TranslatorInterface $translator, private Security $security)
{
$this->translator = $translator;
$this->security = $security;
}
public function buildMenu($menuId, MenuItem $menu, array $parameters)

View File

@@ -21,12 +21,9 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
{
private EntityRepository $repository;
private RequestStack $requestStack;
public function __construct(EntityManagerInterface $entityManager, RequestStack $requestStack)
public function __construct(EntityManagerInterface $entityManager, private RequestStack $requestStack)
{
$this->repository = $entityManager->getRepository(DocGeneratorTemplate::class);
$this->requestStack = $requestStack;
}
public function countByEntity(string $entity): int

View File

@@ -19,17 +19,8 @@ use function is_array;
class NormalizeNullValueHelper
{
private ?string $discriminatorType = null;
private ?string $discriminatorValue = null;
private NormalizerInterface $normalizer;
public function __construct(NormalizerInterface $normalizer, $discriminatorType = null, $discriminatorValue = null)
public function __construct(private NormalizerInterface $normalizer, private ?string $discriminatorType = null, private ?string $discriminatorValue = null)
{
$this->normalizer = $normalizer;
$this->discriminatorType = $discriminatorType;
$this->discriminatorValue = $discriminatorValue;
}
public function normalize(array $attributes, string $format = 'docgen', ?array $context = [], ?ClassMetadata $classMetadata = null)
@@ -45,27 +36,13 @@ class NormalizeNullValueHelper
if (is_numeric($key)) {
$data[$class] = '';
} else {
switch ($class) {
case 'array':
case 'bool':
case 'double':
case 'float':
case 'int':
case 'resource':
case 'string':
case 'null':
$data[$key] = '';
break;
default:
$data[$key] = $this->normalizer->normalize(null, $format, array_merge(
$this->getContextForAttribute($key, $context, $classMetadata),
['docgen:expects' => $class]
));
break;
}
$data[$key] = match ($class) {
'array', 'bool', 'double', 'float', 'int', 'resource', 'string', 'null' => '',
default => $this->normalizer->normalize(null, $format, array_merge(
$this->getContextForAttribute($key, $context, $classMetadata),
['docgen:expects' => $class]
)),
};
}
}

View File

@@ -40,19 +40,13 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
{
use NormalizerAwareTrait;
private ClassMetadataFactoryInterface $classMetadataFactory;
private PropertyAccessor $propertyAccess;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(
ClassMetadataFactoryInterface $classMetadataFactory,
TranslatableStringHelperInterface $translatableStringHelper
private ClassMetadataFactoryInterface $classMetadataFactory,
private TranslatableStringHelperInterface $translatableStringHelper
) {
$this->classMetadataFactory = $classMetadataFactory;
$this->propertyAccess = PropertyAccess::createPropertyAccessor();
$this->translatableStringHelper = $translatableStringHelper;
}
public function normalize($object, $format = null, array $context = [])
@@ -66,7 +60,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
if (!$this->classMetadataFactory->hasMetadataFor($classMetadataKey)) {
throw new LogicException(sprintf(
'This object does not have metadata: %s. Add groups on this entity to allow to serialize with the format %s and groups %s',
is_object($object) ? get_class($object) : '(todo' /*$context['docgen:expects'],*/ ,
is_object($object) ? $object::class : '(todo' /*$context['docgen:expects'],*/ ,
$format,
implode(', ', ($context['groups'] ?? []))
));
@@ -199,10 +193,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
return $normalizer->normalize($keys, $format, $context, $metadata);
}
/**
* @param mixed $format
*/
private function normalizeNullOutputValue($format, array $context, AttributeMetadata $attribute, ReflectionClass $reflection)
private function normalizeNullOutputValue(mixed $format, array $context, AttributeMetadata $attribute, ReflectionClass $reflection)
{
$type = $this->getExpectedType($attribute, $reflection);

View File

@@ -19,11 +19,8 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class BaseContextData
{
private NormalizerInterface $normalizer;
public function __construct(NormalizerInterface $normalizer)
public function __construct(private NormalizerInterface $normalizer)
{
$this->normalizer = $normalizer;
}
public function getData(?User $user = null): array

View File

@@ -25,30 +25,10 @@ use Symfony\Component\HttpFoundation\File\File;
class Generator implements GeneratorInterface
{
private ContextManagerInterface $contextManager;
private DriverInterface $driver;
private EntityManagerInterface $entityManager;
private LoggerInterface $logger;
private StoredObjectManagerInterface $storedObjectManager;
private const LOG_PREFIX = '[docgen generator] ';
public function __construct(
ContextManagerInterface $contextManager,
DriverInterface $driver,
EntityManagerInterface $entityManager,
LoggerInterface $logger,
StoredObjectManagerInterface $storedObjectManager
) {
$this->contextManager = $contextManager;
$this->driver = $driver;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->storedObjectManager = $storedObjectManager;
public function __construct(private ContextManagerInterface $contextManager, private DriverInterface $driver, private EntityManagerInterface $entityManager, private LoggerInterface $logger, private StoredObjectManagerInterface $storedObjectManager)
{
}
/**

View File

@@ -17,13 +17,10 @@ use Throwable;
class GeneratorException extends RuntimeException
{
/**
* @var list<string>
* @param string[] $errors
*/
private array $errors;
public function __construct(array $errors = [], ?Throwable $previous = null)
public function __construct(private array $errors = [], ?Throwable $previous = null)
{
$this->errors = $errors;
parent::__construct(
'Could not generate the document',
15252,
@@ -31,9 +28,6 @@ class GeneratorException extends RuntimeException
);
}
/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;

View File

@@ -26,47 +26,10 @@ use Symfony\Contracts\Translation\TranslatorInterface;
final class OnGenerationFails implements EventSubscriberInterface
{
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private EntityManagerInterface $entityManager;
private LoggerInterface $logger;
private MailerInterface $mailer;
private StoredObjectRepository $storedObjectRepository;
private TranslatorInterface $translator;
private UserRepositoryInterface $userRepository;
public const LOG_PREFIX = '[docgen failed] ';
/**
* @param DocGeneratorTemplateRepository $docGeneratorTemplateRepository
* @param EntityManagerInterface $entityManager
* @param LoggerInterface $logger
* @param MailerInterface $mailer
* @param StoredObjectRepository $storedObjectRepository
* @param TranslatorInterface $translator
* @param UserRepositoryInterface $userRepository
*/
public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
EntityManagerInterface $entityManager,
LoggerInterface $logger,
MailerInterface $mailer,
StoredObjectRepository $storedObjectRepository,
TranslatorInterface $translator,
UserRepositoryInterface $userRepository
) {
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->mailer = $mailer;
$this->storedObjectRepository = $storedObjectRepository;
$this->translator = $translator;
$this->userRepository = $userRepository;
public function __construct(private DocGeneratorTemplateRepository $docGeneratorTemplateRepository, private EntityManagerInterface $entityManager, private LoggerInterface $logger, private MailerInterface $mailer, private StoredObjectRepository $storedObjectRepository, private TranslatorInterface $translator, private UserRepositoryInterface $userRepository)
{
}
@@ -95,7 +58,7 @@ final class OnGenerationFails implements EventSubscriberInterface
'entity_id' => $message->getEntityId(),
'template_id' => $message->getTemplateId(),
'creator_id' => $message->getCreatorId(),
'throwable_class' => get_class($event->getThrowable()),
'throwable_class' => $event->getThrowable()::class,
]);
$this->markObjectAsFailed($message);

View File

@@ -26,36 +26,12 @@ use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
*/
class RequestGenerationHandler implements MessageHandlerInterface
{
private StoredObjectRepository $storedObjectRepository;
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private EntityManagerInterface $entityManager;
private Generator $generator;
private LoggerInterface $logger;
private UserRepositoryInterface $userRepository;
public const AUTHORIZED_TRIALS = 5;
private const LOG_PREFIX = '[docgen message handler] ';
public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
EntityManagerInterface $entityManager,
Generator $generator,
LoggerInterface $logger,
StoredObjectRepository $storedObjectRepository,
UserRepositoryInterface $userRepository
) {
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->entityManager = $entityManager;
$this->generator = $generator;
$this->logger = $logger;
$this->storedObjectRepository = $storedObjectRepository;
$this->userRepository = $userRepository;
public function __construct(private DocGeneratorTemplateRepository $docGeneratorTemplateRepository, private EntityManagerInterface $entityManager, private Generator $generator, private LoggerInterface $logger, private StoredObjectRepository $storedObjectRepository, private UserRepositoryInterface $userRepository)
{
}
public function __invoke(RequestGenerationMessage $message)

View File

@@ -21,26 +21,20 @@ class RequestGenerationMessage
private int $templateId;
private int $entityId;
private int $destinationStoredObjectId;
private array $contextGenerationData;
private \DateTimeImmutable $createdAt;
public function __construct(
User $creator,
DocGeneratorTemplate $template,
int $entityId,
private int $entityId,
StoredObject $destinationStoredObject,
array $contextGenerationData
private array $contextGenerationData
) {
$this->creatorId = $creator->getId();
$this->templateId = $template->getId();
$this->entityId = $entityId;
$this->destinationStoredObjectId = $destinationStoredObject->getId();
$this->contextGenerationData = $contextGenerationData;
$this->createdAt = new \DateTimeImmutable('now');
}

View File

@@ -116,11 +116,8 @@ final class DocGenEncoderTest extends TestCase
/**
* @dataProvider generateEncodeData
*
* @param mixed $expected
* @param mixed $data
*/
public function testEncode($expected, $data, string $msg)
public function testEncode(mixed $expected, mixed $data, string $msg)
{
$generated = $this->encoder->encode($data, 'docgen');
$this->assertEquals($expected, $generated, $msg);