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

@@ -13,25 +13,18 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FormatterInterface;
use LogicException;
use RuntimeException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_slice;
use function call_user_func;
use function count;
use function is_array;
/**
* Command to get the report with curl:
* curl --user "center a_social:password" "http://localhost:8000/fr/exports/generate/count_person?export[filters][person_gender_filter][enabled]=&export[filters][person_nationality_filter][enabled]=&export[filters][person_nationality_filter][form][nationalities]=&export[aggregators][person_nationality_aggregator][order]=1&export[aggregators][person_nationality_aggregator][form][group_by_level]=country&export[submit]=&export[_token]=RHpjHl389GrK-bd6iY5NsEqrD5UKOTHH40QKE9J1edU" --globoff.
*
* @deprecated this formatter is not used any more.
* @deprecated this formatter is not used any more
*/
class CSVFormatter implements FormatterInterface
{
@@ -65,13 +58,11 @@ class CSVFormatter implements FormatterInterface
/**
* @uses appendAggregatorForm
*
* @param mixed $exportAlias
*/
public function buildForm(FormBuilderInterface $builder, $exportAlias, array $aggregatorAliases)
{
$aggregators = $this->exportManager->getAggregators($aggregatorAliases);
$nb = count($aggregatorAliases);
$nb = \count($aggregatorAliases);
foreach ($aggregators as $alias => $aggregator) {
$builderAggregator = $builder->create($alias, FormType::class, [
@@ -101,7 +92,7 @@ class CSVFormatter implements FormatterInterface
continue;
}
if (is_array($statement)) {
if (\is_array($statement)) {
$descriptions[] = $this->translator->trans(
$statement[0],
$statement[1],
@@ -142,7 +133,7 @@ class CSVFormatter implements FormatterInterface
$response = new Response();
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
//$response->headers->set('Content-Disposition','attachment; filename="export.csv"');
// $response->headers->set('Content-Disposition','attachment; filename="export.csv"');
$response->setContent($this->generateContent());
@@ -172,10 +163,8 @@ class CSVFormatter implements FormatterInterface
// gather data in an array
foreach ($keys as $key) {
$values = array_map(static function ($row) use ($key, $alias) {
if (!array_key_exists($key, $row)) {
throw new LogicException("the key '" . $key . "' is declared by "
. "the aggregator with alias '" . $alias . "' but is not "
. 'present in results');
if (!\array_key_exists($key, $row)) {
throw new \LogicException("the key '".$key."' is declared by the aggregator with alias '".$alias."' but is not ".'present in results');
}
return $row[$key];
@@ -199,10 +188,8 @@ class CSVFormatter implements FormatterInterface
foreach ($keys as $key) {
$values = array_map(static function ($row) use ($key, $export) {
if (!array_key_exists($key, $row)) {
throw new LogicException("the key '" . $key . "' is declared by "
. "the export with title '" . $export->getTitle() . "' but is not "
. 'present in results');
if (!\array_key_exists($key, $row)) {
throw new \LogicException("the key '".$key."' is declared by the export with title '".$export->getTitle()."' but is not ".'present in results');
}
return $row[$key];
@@ -220,9 +207,9 @@ class CSVFormatter implements FormatterInterface
protected function generateContent()
{
$line = null;
$rowKeysNb = count($this->getRowHeaders());
$columnKeysNb = count($this->getColumnHeaders());
$resultsKeysNb = count($this->export->getQueryKeys($this->exportData));
$rowKeysNb = \count($this->getRowHeaders());
$columnKeysNb = \count($this->getColumnHeaders());
$resultsKeysNb = \count($this->export->getQueryKeys($this->exportData));
$results = $this->getOrderedResults();
/** @var string[] $columnHeaders the column headers associations */
$columnHeaders = [];
@@ -233,9 +220,9 @@ class CSVFormatter implements FormatterInterface
// create a file pointer connected to the output stream
$output = fopen('php://output', 'wb');
//title
// title
fputcsv($output, [$this->translator->trans($this->export->getTitle())]);
//blank line
// blank line
fputcsv($output, ['']);
// add filtering description
@@ -247,32 +234,32 @@ class CSVFormatter implements FormatterInterface
// iterate on result to : 1. populate row headers, 2. populate column headers, 3. add result
foreach ($results as $row) {
$rowHeaders = array_slice($row, 0, $rowKeysNb);
$rowHeaders = \array_slice($row, 0, $rowKeysNb);
//first line : we create line and adding them row headers
// first line : we create line and adding them row headers
if (!isset($line)) {
$line = array_slice($row, 0, $rowKeysNb);
$line = \array_slice($row, 0, $rowKeysNb);
}
// do we have to create a new line ? if the rows are equals, continue on the line, else create a next line
if (array_slice($line, 0, $rowKeysNb) !== $rowHeaders) {
if (\array_slice($line, 0, $rowKeysNb) !== $rowHeaders) {
$contentData[] = $line;
$line = array_slice($row, 0, $rowKeysNb);
$line = \array_slice($row, 0, $rowKeysNb);
}
// add the column headers
/** @var string[] $columns the column for this row */
$columns = array_slice($row, $rowKeysNb, $columnKeysNb);
$columns = \array_slice($row, $rowKeysNb, $columnKeysNb);
$columnPosition = $this->findColumnPosition($columnHeaders, $columns);
//fill with blank at the position given by the columnPosition + nbRowHeaders
// fill with blank at the position given by the columnPosition + nbRowHeaders
for ($i = 0; $i < $columnPosition; ++$i) {
if (!isset($line[$rowKeysNb + $i])) {
$line[$rowKeysNb + $i] = '';
}
}
$resultData = array_slice($row, $resultsKeysNb * -1);
$resultData = \array_slice($row, $resultsKeysNb * -1);
foreach ($resultData as $data) {
$line[] = $data;
@@ -282,7 +269,7 @@ class CSVFormatter implements FormatterInterface
// we add the last line
$contentData[] = $line;
//column title headers
// column title headers
for ($i = 0; $i < $columnKeysNb; ++$i) {
$line = array_fill(0, $rowKeysNb, '');
@@ -293,22 +280,22 @@ class CSVFormatter implements FormatterInterface
$content[] = $line;
}
//row title headers
// row title headers
$headerLine = [];
foreach ($this->getRowHeaders() as $headerKey) {
$headerLine[] = array_key_exists('_header', $this->labels[$headerKey]) ?
$headerLine[] = \array_key_exists('_header', $this->labels[$headerKey]) ?
$this->labels[$headerKey]['_header'] : '';
}
foreach ($this->export->getQueryKeys($this->exportData) as $key) {
$headerLine[] = array_key_exists('_header', $this->labels[$key]) ?
$headerLine[] = \array_key_exists('_header', $this->labels[$key]) ?
$this->labels[$key]['_header'] : '';
}
fputcsv($output, $headerLine);
unset($headerLine); //free memory
unset($headerLine); // free memory
//generate CSV
// generate CSV
foreach ($content as $line) {
fputcsv($output, $line);
}
@@ -390,7 +377,7 @@ class CSVFormatter implements FormatterInterface
++$i;
}
//we didn't find it, adding the column
// we didn't find it, adding the column
$columnHeaders[] = $columnToFind;
return $i++;
@@ -410,12 +397,12 @@ class CSVFormatter implements FormatterInterface
$line = [];
foreach ($headers as $key) {
$line[] = call_user_func($labels[$key], $row[$key]);
$line[] = \call_user_func($labels[$key], $row[$key]);
}
//append result
// append result
foreach ($resultsKeys as $key) {
$line[] = call_user_func($labels[$key], $row[$key]);
$line[] = \call_user_func($labels[$key], $row[$key]);
}
$r[] = $line;
@@ -429,19 +416,17 @@ class CSVFormatter implements FormatterInterface
/**
* @param string $position may be 'c' (column) or 'r' (row)
*
* @throws RuntimeException
*
* @return string[]
*
* @throws \RuntimeException
*/
private function getPositionnalHeaders($position)
{
$headers = [];
foreach ($this->formatterData as $alias => $data) {
if (!array_key_exists($alias, $this->aggregatorsData)) {
throw new RuntimeException('the formatter wants to use the '
. "aggregator with alias {$alias}, but the export do not "
. 'contains data about it');
if (!\array_key_exists($alias, $this->aggregatorsData)) {
throw new \RuntimeException('the formatter wants to use the '."aggregator with alias {$alias}, but the export do not ".'contains data about it');
}
$aggregator = $this->aggregators[$alias];