Conditions | 18 |
Paths | 241 |
Total Lines | 82 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | public function traitement() |
||
19 | { |
||
20 | if ($this->getContainer()->get('kernel')->getEnvironment() == 'prod') { |
||
21 | $this->output->writeln("<error>Cette commande ne peut être lancé en environnement de production</error>"); |
||
22 | exit; |
||
|
|||
23 | } |
||
24 | $meta = $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); |
||
25 | // On récupère la liste des entites en fonction des nom de tables supposés. |
||
26 | $namespaces = []; |
||
27 | foreach ($meta as $m) { |
||
28 | if ($m->isMappedSuperclass) { |
||
29 | continue; |
||
30 | } |
||
31 | if (count($m->subClasses)) { |
||
32 | $namespaces[$m->getName()] = [ |
||
33 | 'nomTable' => $m->getTableName(), |
||
34 | ]; |
||
35 | continue; |
||
36 | } |
||
37 | //echo "\n\nOn format\n"; |
||
38 | $listeDesColonnes = array_flip($m->columnNames); |
||
39 | $listeDesChamps = []; |
||
40 | foreach ($listeDesColonnes as $column => $field) { |
||
41 | $listeDesChamps[$column] = [ |
||
42 | 'field' => $field, |
||
43 | 'type' => $m->getFieldMapping($field)['type'], |
||
44 | ]; |
||
45 | } |
||
46 | $mapping = $m->getAssociationMappings(); |
||
47 | foreach ($mapping as $clefJointure => $detail) { |
||
48 | if (!array_key_exists("sourceToTargetKeyColumns", $detail)) { |
||
49 | continue; |
||
50 | } |
||
51 | $column = array_keys($detail['sourceToTargetKeyColumns'])[0]; |
||
52 | $listeDesChamps[$column] = [ |
||
53 | 'field' => $detail['fieldName'], |
||
54 | 'type' => 'integer', |
||
55 | 'sourceToTargetKeyColumns' => $detail['sourceToTargetKeyColumns'][$column], |
||
56 | 'targetEntity' => $detail['targetEntity'], |
||
57 | ]; |
||
58 | } |
||
59 | $namespaces[$m->getName()] = [ |
||
60 | 'nomTable' => $m->getTableName(), |
||
61 | 'listeDesChamps' => $listeDesChamps, |
||
62 | 'discriminatorValue' => $m->discriminatorValue, |
||
63 | 'discriminatorColumn' => !empty($m->discriminatorValue) ? $m->discriminatorColumn['name'] : null, |
||
64 | ]; |
||
65 | } |
||
66 | ksort($namespaces); |
||
67 | $contenu = ""; |
||
68 | foreach ($namespaces as $namespace => $data) { |
||
69 | if (empty($data['listeDesChamps'])) { |
||
70 | continue; |
||
71 | } |
||
72 | $export = []; |
||
73 | $listeDesChamps = $data['listeDesChamps']; |
||
74 | $nomTable = $data['nomTable']; |
||
75 | $where = (!empty($data['discriminatorValue']) ? $data['discriminatorColumn']."='".$data['discriminatorValue']."'" : "1=1"); |
||
76 | $resultats = $this->getConnection()->fetchAll("SELECT ".implode(",", array_keys($listeDesChamps))." FROM ".$nomTable." WHERE ".$where); |
||
77 | if (empty($resultats)) { |
||
78 | continue; |
||
79 | } |
||
80 | foreach ($resultats as $key => $row) { |
||
81 | $dataExport = []; |
||
82 | foreach ($row as $element => $value) { |
||
83 | if ($element == 'id') { |
||
84 | continue; |
||
85 | } |
||
86 | $field = $listeDesChamps[$element]['field']; |
||
87 | if (!empty($listeDesChamps[$element]['targetEntity'])) { |
||
88 | $value = empty($value) ? null : strtolower("@".$namespaces[$listeDesChamps[$element]['targetEntity']]['nomTable'])."_".$value; |
||
89 | $dataExport[$listeDesChamps[$element]['field']] = $value; |
||
90 | continue; |
||
91 | } |
||
92 | $dataExport[$field] = $this->formatValue($listeDesChamps[$element]['type'], $value); |
||
93 | } |
||
94 | $export[strtolower($nomTable).'_'.$row['id']] = $dataExport; |
||
95 | } |
||
96 | $contenu .= Yaml::dump([$namespace => $export,], 2, 4)."\n"; |
||
97 | } |
||
98 | echo $contenu; |
||
99 | } |
||
100 | |||
128 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.