fix: SA: Fix "incorrect case" rule.

SA stands for Static Analysis.
This commit is contained in:
Pol Dellaiera
2021-11-16 10:29:41 +01:00
parent 196cddab09
commit a7b96f1756
4 changed files with 42 additions and 58 deletions

View File

@@ -45,25 +45,25 @@ class CreateFieldsOnGroupCommand extends Command
{
const ARG_PATH = 'path';
const ARG_DELETE = 'delete';
/**
* @var CustomFieldProvider
*/
private $customFieldProvider;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var ValidatorInterface
*/
private $validator;
private $availableLanguages;
private $customizablesEntities;
/**
* CreateFieldsOnGroupCommand constructor.
*
@@ -87,7 +87,7 @@ class CreateFieldsOnGroupCommand extends Command
$this->customizablesEntities = $customizablesEntities;
parent::__construct();
}
protected function configure()
{
$this->setName('chill:custom_fields:populate_group')
@@ -111,7 +111,7 @@ class CreateFieldsOnGroupCommand extends Command
$em->remove($field);
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
@@ -120,18 +120,18 @@ class CreateFieldsOnGroupCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelperSet()->get('question');
$em = $this->entityManager;
$customFieldsGroups = $em
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
->findAll();
if (count($customFieldsGroups) === 0) {
$output->writeln('<error>There aren\'t any CustomFieldsGroup recorded'
. ' Please create at least one.</error>');
}
}
$table = new Table($output);
$table
->setHeaders(array_merge(
@@ -141,7 +141,7 @@ class CreateFieldsOnGroupCommand extends Command
->setRows($this->_prepareRows($customFieldsGroups))
->render()
;
$question = new Question(
"Enter the customfieldGroup's id on which the custom fields should be added: ");
$question->setNormalizer(
@@ -151,24 +151,23 @@ class CreateFieldsOnGroupCommand extends Command
return $customFieldsGroup;
}
}
throw new \RunTimeException('The id does not match an existing '
. 'CustomFieldsGroup');
throw new \RuntimeException('The id does not match an existing CustomFieldsGroup');
}
);
$customFieldsGroup = $helper->ask($input, $output, $question);
if ($input->getOption(self::ARG_DELETE)) {
$this->deleteFieldsForCFGroup($customFieldsGroup);
}
$fieldsInput = $this->_parse($input->getArgument(self::ARG_PATH),
$fieldsInput = $this->_parse($input->getArgument(self::ARG_PATH),
$output);
$fields = $this->_addFields($customFieldsGroup, $fieldsInput, $output);
}
private function _prepareRows ($customFieldsGroups)
private function _prepareRows ($customFieldsGroups)
{
$rows = array();
$languages = $this->availableLanguages;
@@ -177,8 +176,8 @@ class CreateFieldsOnGroupCommand extends Command
foreach ($this->customizablesEntities as $entry) {
$customizableEntities[$entry['class']] = $entry['name'];
}
array_walk($customFieldsGroups,
array_walk($customFieldsGroups,
function(CustomFieldsGroup $customFieldGroup, $key)
use ($languages, &$rows, $customizableEntities) {
//set id and entity
@@ -186,7 +185,7 @@ class CreateFieldsOnGroupCommand extends Command
$customFieldGroup->getId(),
$customizableEntities[$customFieldGroup->getEntity()]
);
foreach ($languages as $lang) {
//todo replace with service to find lang when available
$row[] = (isset($customFieldGroup->getName()[$lang])) ?
@@ -196,42 +195,42 @@ class CreateFieldsOnGroupCommand extends Command
$rows[] = $row;
}
);
return $rows;
}
private function _parse($path, OutputInterface $output)
{
$parser = new Parser();
if (!file_exists($path)) {
throw new \RunTimeException("file does not exist");
throw new \RuntimeException("file does not exist");
}
try {
$values = $parser->parse(file_get_contents($path));
} catch (ParseException $ex) {
throw new \RunTimeException("The yaml file is not valid", 0, $ex);
throw new \RuntimeException("The yaml file is not valid", 0, $ex);
}
return $values;
}
private function _addFields(CustomFieldsGroup $group, $values, OutputInterface $output)
{
$em = $this->entityManager;
$languages = $this->availableLanguages;
foreach($values['fields'] as $slug => $field) {
//check the cf type exists
$cfType = $this->customFieldProvider->getCustomFieldByType($field['type']);
if ($cfType === NULL) {
throw new \RunTimeException('the type '.$field['type'].' '
throw new \RuntimeException('the type '.$field['type'].' '
. 'does not exists');
}
$cf = new CustomField();
$cf->setSlug($slug)
->setName($field['name'])
@@ -239,7 +238,7 @@ class CreateFieldsOnGroupCommand extends Command
->setOrdering($field['ordering'])
->setType($field['type'])
->setCustomFieldsGroup($group);
//add to table
$names = array();
foreach ($languages as $lang) {
@@ -248,17 +247,17 @@ class CreateFieldsOnGroupCommand extends Command
$cf->getName()[$lang] :
'Not available in this language';
}
if ($this->validator->validate($cf)) {
$em->persist($cf);
$output->writeln("<info>Adding Custom Field of type "
.$cf->getType()."\t with slug ".$cf->getSlug().
"\t and names : ".implode($names, ', ')."</info>");
} else {
throw new \RunTimeException("Error in field ".$slug);
throw new \RuntimeException("Error in field ".$slug);
}
}
$em->flush();
}
}