fix: SA: Fix "...an unused use..." rule.

Also fix a critical bug in `ListPerson.php`.

SA stands for Static Analysis.
This commit is contained in:
Pol Dellaiera
2021-11-16 15:06:43 +01:00
parent 8bebe1f904
commit 7462babbeb
4 changed files with 83 additions and 268 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Export\Export;
use Chill\MainBundle\Export\ListInterface;
@@ -24,47 +26,29 @@ use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
/**
* Render a list of peoples
*
* @author julien
* Render a list of people.
*/
class ListPerson implements ListInterface, ExportElementValidatedInterface
{
/**
*
* @var EntityManagerInterface
*/
protected $entityManager;
protected EntityManagerInterface $entityManager;
/**
*
* @var TranslatorInterface
*/
protected $translator;
protected TranslatorInterface $translator;
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
protected TranslatableStringHelper $translatableStringHelper;
/**
*
* @var CustomFieldProvider
*/
protected $customFieldProvider;
protected CustomFieldProvider $customFieldProvider;
protected $fields = array(
protected array $fields = [
'id', 'firstName', 'lastName', 'birthdate',
'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber',
'mobilenumber', 'contactInfo', 'countryOfBirth', 'nationality',
'address_street_address_1', 'address_street_address_2',
'address_valid_from', 'address_postcode_label', 'address_postcode_code',
'address_country_name', 'address_country_code', 'address_isnoaddress'
);
];
private $slugs = [];
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator,
@@ -77,11 +61,6 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
$this->customFieldProvider = $customFieldProvider;
}
/**
* {@inheritDoc}
*
* @param FormBuilderInterface $builder
*/
public function buildForm(FormBuilderInterface $builder)
{
$choices = array_combine($this->fields, $this->fields);
@@ -99,13 +78,13 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
'expanded' => true,
'choices' => $choices,
'label' => 'Fields to include in export',
'choice_attr' => function($val, $key, $index) {
'choice_attr' => static function(string $val): array {
// add a 'data-display-target' for address fields
if (substr($val, 0, 8) === 'address_') {
return ['data-display-target' => 'address_date'];
} else {
return [];
}
return [];
},
'constraints' => [new Callback(array(
'callback' => function($selected, ExecutionContextInterface $context) {
@@ -133,9 +112,10 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
public function validateForm($data, ExecutionContextInterface $context)
{
// get the field starting with address_
$addressFields = array_filter(function($el) {
return substr($el, 0, 8) === 'address_';
}, $this->fields);
$addressFields = array_filter(
$this->fields,
static fn(string $el): bool => substr($el, 0, 8) === 'address_'
);
// check if there is one field starting with address in data
if (count(array_intersect($data['fields'], $addressFields)) > 0) {
@@ -168,41 +148,23 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
->getResult();
}
/**
* {@inheritDoc}
*
* @return type
*/
public function getAllowedFormattersTypes()
{
return array(FormatterInterface::TYPE_LIST);
}
/**
* {@inheritDoc}
*
* @return string
*/
public function getDescription()
{
return "Create a list of people according to various filters.";
}
/**
* {@inheritDoc}
*
* @param type $key
* @param array $values
* @param type $data
* @return type
*/
public function getLabels($key, array $values, $data)
{
switch ($key) {
case 'birthdate':
// for birthdate, we have to transform the string into a date
// to format the date correctly.
return function($value) {
return static function($value) {
if ($value === '_header') { return 'birthdate'; }
if (empty($value))
@@ -257,31 +219,33 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
return $this->translatableStringHelper->localize(json_decode($value, true));
};
case 'address_isnoaddress':
return function($value) use ($key) {
if ($value === '_header') { return 'address.address_homeless'; }
if ($value) {
return 'X';
} else {
return '';
return static function(?string $value): string {
if ($value === '_header') {
return 'address.address_homeless';
}
if (null !== $value) {
return 'X';
}
return '';
};
default:
// for fields which are associated with person
if (in_array($key, $this->fields)) {
return function($value) use ($key) {
return static function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); }
return $value;
};
} else {
return $this->getLabelForCustomField($key, $values, $data);
}
return $this->getLabelForCustomField($key, $values, $data);
}
}
private function getLabelForCustomField($key, array $values, $data)
{
// for fields which are custom fields
@@ -292,45 +256,39 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
$defaultFunction = function($value) use ($cf) {
if ($value === '_header') {
return $this->translatableStringHelper->localize($cf->getName());
return $this->translatableStringHelper->localize($cf->getName());
}
return $this->customFieldProvider
->getCustomFieldByType($cf->getType())
->render(json_decode($value, true), $cf, 'csv');
};
if ($cfType instanceof CustomFieldChoice and $cfType->isMultiple($cf)) {
return function($value) use ($cf, $cfType, $key) {
$slugChoice = $this->extractInfosFromSlug($key)['additionnalInfos']['choiceSlug'];
$decoded = \json_decode($value, true);
if ($value === '_header') {
$label = $cfType->getChoices($cf)[$slugChoice];
return $this->translatableStringHelper->localize($cf->getName())
.' | '.$label;
}
if ($slugChoice === '_other' and $cfType->isChecked($cf, $choiceSlug, $decoded)) {
return $cfType->extractOtherValue($cf, $decoded);
} else {
return $cfType->isChecked($cf, $slugChoice, $decoded);
}
return $cfType->isChecked($cf, $slugChoice, $decoded);
};
} else {
return $defaultFunction;
}
return $defaultFunction;
}
/**
* {@inheritDoc}
*
* @param type $data
* @return type
*/
public function getQueryKeys($data)
{
$fields = array();
@@ -340,78 +298,55 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
$fields[] = $key;
}
}
// add the key from slugs and return
return \array_merge($fields, \array_keys($this->slugs));
}
/**
* clean a slug to be usable by DQL
*
* @param string $slugsanitize
* @param string $type the type of the customfield, if required (currently only for choices)
* @return string
* Clean a slug to be usable by DQL.
*/
private function slugToDQL($slug, $type = "default", array $additionalInfos = [])
private function slugToDQL(string $slug, string $type = "default", array $additionalInfos = []): string
{
$uid = 'slug_'.\uniqid();
$uid = 'slug_' . \uniqid('', true);
$this->slugs[$uid] = [
'slug' => $slug,
'type' => $type,
'additionnalInfos' => $additionalInfos
];
return $uid;
}
private function DQLToSlug($cleanedSlug)
{
{
return $this->slugs[$cleanedSlug]['slug'];
}
/**
*
* @param type $cleanedSlug
* @return an array with keys = 'slug', 'type', 'additionnalInfo'
* @return array An array with keys = 'slug', 'type', 'additionnalInfo'
*/
private function extractInfosFromSlug($slug)
private function extractInfosFromSlug($slug): array
{
return $this->slugs[$slug];
}
/**
* {@inheritDoc}
*
*/
public function getResult($query, $data)
{
return $query->getQuery()->getResult(Query::HYDRATE_SCALAR);
}
/**
* {@inheritDoc}
*
* @return string
*/
public function getTitle()
{
return "List peoples";
}
/**
* {@inheritDoc}
*
*/
public function getType()
{
return Declarations::PERSON_TYPE;
}
/**
* {@inheritDoc}
*
*/
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
{
$centers = array_map(function($el) { return $el['center']; }, $acl);
@@ -459,7 +394,7 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
foreach($cfType->getChoices($cf) as $choiceSlug => $label) {
$slug = $this->slugToDQL($cf->getSlug(), 'choice', [ 'choiceSlug' => $choiceSlug ]);
$qb->addSelect(
sprintf('GET_JSON_FIELD_BY_KEY(person.cFData, :slug%s) AS %s',
sprintf('GET_JSON_FIELD_BY_KEY(person.cFData, :slug%s) AS %s',
$slug, $slug));
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
}
@@ -483,19 +418,11 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
return $qb;
}
/**
*
* {@inheritDoc}
*/
public function requiredRole()
{
return new Role(PersonVoter::LISTS);
}
/**
*
* {@inheritDoc}
*/
public function supportsModifiers()
{
return array(Declarations::PERSON_TYPE, Declarations::PERSON_IMPLIED_IN);