apply more cs rules for php-cs

This commit is contained in:
2023-10-17 13:27:03 +02:00
parent 0b0cbed9db
commit bc2041cbdd
1485 changed files with 8169 additions and 9620 deletions

View File

@@ -14,21 +14,11 @@ namespace Chill\MainBundle\Export;
use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Doctrine\ORM\QueryBuilder;
use Generator;
use LogicException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use UnexpectedValueException;
use function array_key_exists;
use function count;
use function get_class;
use function gettype;
use function in_array;
/**
* Collects all agregators, filters and export from
@@ -74,19 +64,19 @@ class ExportManager
iterable $exports,
iterable $aggregators,
iterable $filters
//iterable $formatters,
//iterable $exportElementProvider
// iterable $formatters,
// iterable $exportElementProvider
) {
$this->user = $tokenStorage->getToken()->getUser();
$this->exports = iterator_to_array($exports);
$this->aggregators = iterator_to_array($aggregators);
$this->filters = iterator_to_array($filters);
// NOTE: PHP crashes on the next line (exit error code 11). This is desactivated until further investigation
//$this->formatters = iterator_to_array($formatters);
// $this->formatters = iterator_to_array($formatters);
//foreach ($exportElementProvider as $prefix => $provider) {
// foreach ($exportElementProvider as $prefix => $provider) {
// $this->addExportElementsProvider($provider, $prefix);
//}
// }
}
/**
@@ -101,7 +91,7 @@ class ExportManager
*
* @return FilterInterface[] a \Generator that contains filters. The key is the filter's alias
*/
public function &getFiltersApplyingOn(DirectExportInterface|ExportInterface $export, ?array $centers = null): iterable
public function &getFiltersApplyingOn(DirectExportInterface|ExportInterface $export, array $centers = null): iterable
{
if ($export instanceof DirectExportInterface) {
return;
@@ -109,7 +99,7 @@ class ExportManager
foreach ($this->filters as $alias => $filter) {
if (
in_array($filter->applyOn(), $export->supportsModifiers(), true)
\in_array($filter->applyOn(), $export->supportsModifiers(), true)
&& $this->isGrantedForElement($filter, $export, $centers)
) {
yield $alias => $filter;
@@ -122,9 +112,9 @@ class ExportManager
*
* @internal This class check the interface implemented by export, and, if ´ListInterface´ is used, return an empty array
*
* @return null|iterable<string, AggregatorInterface> a \Generator that contains aggretagors. The key is the filter's alias
* @return iterable<string, AggregatorInterface>|null a \Generator that contains aggretagors. The key is the filter's alias
*/
public function &getAggregatorsApplyingOn(DirectExportInterface|ExportInterface $export, ?array $centers = null): ?iterable
public function &getAggregatorsApplyingOn(DirectExportInterface|ExportInterface $export, array $centers = null): ?iterable
{
if ($export instanceof ListInterface || $export instanceof DirectExportInterface) {
return;
@@ -132,7 +122,7 @@ class ExportManager
foreach ($this->aggregators as $alias => $aggregator) {
if (
in_array($aggregator->applyOn(), $export->supportsModifiers(), true)
\in_array($aggregator->applyOn(), $export->supportsModifiers(), true)
&& $this->isGrantedForElement($aggregator, $export, $centers)
) {
yield $alias => $aggregator;
@@ -143,7 +133,7 @@ class ExportManager
public function addExportElementsProvider(ExportElementsProviderInterface $provider, string $prefix): void
{
foreach ($provider->getExportElements() as $suffix => $element) {
$alias = $prefix . '_' . $suffix;
$alias = $prefix.'_'.$suffix;
if ($element instanceof ExportInterface) {
$this->exports[$alias] = $element;
@@ -154,8 +144,7 @@ class ExportManager
} elseif ($element instanceof FormatterInterface) {
$this->addFormatter($element, $alias);
} else {
throw new LogicException('This element ' . $element::class . ' '
. 'is not an instance of export element');
throw new \LogicException('This element '.$element::class.' is not an instance of export element');
}
}
}
@@ -194,37 +183,27 @@ class ExportManager
if ($query instanceof \Doctrine\ORM\NativeQuery) {
// throw an error if the export require other modifier, which is
// not allowed when the export return a `NativeQuery`
if (count($export->supportsModifiers()) > 0) {
throw new LogicException("The export with alias `{$exportAlias}` return "
. 'a `\\Doctrine\\ORM\\NativeQuery` and supports modifiers, which is not '
. 'allowed. Either the method `supportsModifiers` should return an empty '
. 'array, or return a `Doctrine\\ORM\\QueryBuilder`');
if (\count($export->supportsModifiers()) > 0) {
throw new \LogicException("The export with alias `{$exportAlias}` return ".'a `\\Doctrine\\ORM\\NativeQuery` and supports modifiers, which is not allowed. Either the method `supportsModifiers` should return an empty array, or return a `Doctrine\\ORM\\QueryBuilder`');
}
} elseif ($query instanceof QueryBuilder) {
//handle filters
// handle filters
$this->handleFilters($export, $query, $data[ExportType::FILTER_KEY], $centers);
//handle aggregators
// handle aggregators
$this->handleAggregators($export, $query, $data[ExportType::AGGREGATOR_KEY], $centers);
$this->logger->notice('[export] will execute this qb in export', [
'dql' => $query->getDQL(),
]);
} else {
throw new UnexpectedValueException('The method `intiateQuery` should return '
. 'a `\\Doctrine\\ORM\\NativeQuery` or a `Doctrine\\ORM\\QueryBuilder` '
. 'object.');
throw new \UnexpectedValueException('The method `intiateQuery` should return a `\\Doctrine\\ORM\\NativeQuery` or a `Doctrine\\ORM\\QueryBuilder` object.');
}
$result = $export->getResult($query, $data[ExportType::EXPORT_KEY]);
if (!is_iterable($result)) {
throw new UnexpectedValueException(
sprintf(
'The result of the export should be an iterable, %s given',
gettype($result)
)
);
throw new \UnexpectedValueException(sprintf('The result of the export should be an iterable, %s given', \gettype($result)));
}
/** @var FormatterInterface $formatter */
@@ -259,14 +238,14 @@ class ExportManager
/**
* @param string $alias
*
* @throws RuntimeException if the aggregator is not known
*
* @return AggregatorInterface
*
* @throws \RuntimeException if the aggregator is not known
*/
public function getAggregator($alias)
{
if (!array_key_exists($alias, $this->aggregators)) {
throw new RuntimeException("The aggregator with alias {$alias} is not known.");
if (!\array_key_exists($alias, $this->aggregators)) {
throw new \RuntimeException("The aggregator with alias {$alias} is not known.");
}
return $this->aggregators[$alias];
@@ -283,7 +262,7 @@ class ExportManager
}
/**
* Get the types for known exports
* Get the types for known exports.
*
* @return list<string> the existing type for known exports
*/
@@ -292,7 +271,7 @@ class ExportManager
$existingTypes = [];
foreach ($this->exports as $export) {
if (!in_array($export->getType(), $existingTypes, true)) {
if (!\in_array($export->getType(), $existingTypes, true)) {
$existingTypes[] = $export->getType();
}
}
@@ -305,12 +284,12 @@ class ExportManager
*
* @param string $alias
*
* @throws RuntimeException
* @throws \RuntimeException
*/
public function getExport($alias): DirectExportInterface|ExportInterface
{
if (!array_key_exists($alias, $this->exports)) {
throw new RuntimeException("The export with alias {$alias} is not known.");
if (!\array_key_exists($alias, $this->exports)) {
throw new \RuntimeException("The export with alias {$alias} is not known.");
}
return $this->exports[$alias];
@@ -339,7 +318,6 @@ class ExportManager
/**
* Get all exports grouped in an array.
*
*
* @return array<string, array<string, ExportInterface|DirectExportInterface>> where keys are the groups's name and value is an array of exports
*/
public function getExportsGrouped(bool $whereUserIsGranted = true): array
@@ -358,12 +336,12 @@ class ExportManager
}
/**
* @throws RuntimeException if the filter is not known
* @throws \RuntimeException if the filter is not known
*/
public function getFilter(string $alias): FilterInterface
{
if (!array_key_exists($alias, $this->filters)) {
throw new RuntimeException("The filter with alias {$alias} is not known.");
if (!\array_key_exists($alias, $this->filters)) {
throw new \RuntimeException("The filter with alias {$alias} is not known.");
}
return $this->filters[$alias];
@@ -373,6 +351,7 @@ class ExportManager
* get all filters.
*
* @param array<string> $aliases
*
* @return iterable<string, FilterInterface> $aliases
*/
public function getFilters(array $aliases): iterable
@@ -384,8 +363,8 @@ class ExportManager
public function getFormatter(string $alias): FormatterInterface
{
if (!array_key_exists($alias, $this->formatters)) {
throw new RuntimeException("The formatter with alias {$alias} is not known.");
if (!\array_key_exists($alias, $this->formatters)) {
throw new \RuntimeException("The formatter with alias {$alias} is not known.");
}
return $this->formatters[$alias];
@@ -395,11 +374,12 @@ class ExportManager
* get the formatter alias from the form export data.
*
* @param array $data the data from the export form
*
* @string the formatter alias|null
*/
public function getFormatterAlias(array $data): ?string
{
if (array_key_exists(ExportType::PICK_FORMATTER_KEY, $data)) {
if (\array_key_exists(ExportType::PICK_FORMATTER_KEY, $data)) {
return $data[ExportType::PICK_FORMATTER_KEY]['alias'];
}
@@ -414,7 +394,7 @@ class ExportManager
public function getFormattersByTypes(array $types): iterable
{
foreach ($this->formatters as $alias => $formatter) {
if (in_array($formatter->getType(), $types, true)) {
if (\in_array($formatter->getType(), $types, true)) {
yield $alias => $formatter;
}
}
@@ -450,21 +430,18 @@ class ExportManager
/**
* Return true if the current user has access to the ExportElement for every
* center, false if the user hasn't access to element for at least one center.
*
*/
public function isGrantedForElement(
DirectExportInterface|ExportInterface|ModifierInterface $element,
\Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export = null,
?array $centers = null
DirectExportInterface|ExportInterface $export = null,
array $centers = null
): bool {
if ($element instanceof ExportInterface || $element instanceof DirectExportInterface) {
$role = $element->requiredRole();
} else {
if (null === $element->addRole()) {
if (null === $export) {
throw new LogicException('The export should not be null: as the '
. 'ModifierInstance element is not an export, we should '
. 'be aware of the export to determine which role is required');
throw new \LogicException('The export should not be null: as the ModifierInstance element is not an export, we should be aware of the export to determine which role is required');
}
$role = $export->requiredRole();
} else {
@@ -482,7 +459,7 @@ class ExportManager
foreach ($centers as $center) {
if (false === $this->authorizationChecker->isGranted($role, $center)) {
//debugging
// debugging
$this->logger->debug('user has no access to element', [
'method' => __METHOD__,
'type' => $element::class,
@@ -538,9 +515,8 @@ class ExportManager
$aggregators = $this->retrieveUsedAggregators($data);
foreach ($aggregators as $alias => $aggregator) {
if ($this->isGrantedForElement($aggregator, $export, $center) === false) {
throw new UnauthorizedHttpException('You are not authorized to '
. 'use the aggregator' . $aggregator->getTitle());
if (false === $this->isGrantedForElement($aggregator, $export, $center)) {
throw new UnauthorizedHttpException('You are not authorized to use the aggregator'.$aggregator->getTitle());
}
$formData = $data[$alias];
@@ -553,8 +529,9 @@ class ExportManager
*
* This function check the acl.
*
* @param mixed $data the data under the initial 'filters' data
* @param mixed $data the data under the initial 'filters' data
* @param \Chill\MainBundle\Entity\Center[] $centers the picked centers
*
* @throw UnauthorizedHttpException if the user is not authorized
*/
private function handleFilters(
@@ -566,14 +543,13 @@ class ExportManager
$filters = $this->retrieveUsedFilters($data);
foreach ($filters as $alias => $filter) {
if ($this->isGrantedForElement($filter, $export, $centers) === false) {
throw new UnauthorizedHttpException('You are not authorized to '
. 'use the filter ' . $filter->getTitle());
if (false === $this->isGrantedForElement($filter, $export, $centers)) {
throw new UnauthorizedHttpException('You are not authorized to use the filter '.$filter->getTitle());
}
$formData = $data[$alias];
$this->logger->debug('alter query by filter ' . $alias, [
$this->logger->debug('alter query by filter '.$alias, [
'class' => self::class, 'function' => __FUNCTION__,
]);
$filter->alterQuery($qb, $formData['form']);
@@ -608,7 +584,7 @@ class ExportManager
$usedTypes = [];
foreach ($this->retrieveUsedAggregators($data) as $alias => $aggregator) {
if (!in_array($aggregator->applyOn(), $usedTypes, true)) {
if (!\in_array($aggregator->applyOn(), $usedTypes, true)) {
$usedTypes[] = $aggregator->applyOn();
}
}
@@ -651,7 +627,7 @@ class ExportManager
if (true === $filterData['enabled']) {
$filter = $this->getFilter($alias);
if (!in_array($filter->applyOn(), $usedTypes, true)) {
if (!\in_array($filter->applyOn(), $usedTypes, true)) {
$usedTypes[] = $filter->applyOn();
}
}
@@ -663,7 +639,6 @@ class ExportManager
/**
* parse the data to retrieve the used filters and aggregators.
*
*
* @return string[]
*/
private function retrieveUsedModifiers(mixed $data)
@@ -678,7 +653,7 @@ class ExportManager
);
$this->logger->debug(
'Required types are ' . implode(', ', $usedTypes),
'Required types are '.implode(', ', $usedTypes),
['class' => self::class, 'function' => __FUNCTION__]
);