Conditions | 29 |
Paths | 9792 |
Total Lines | 112 |
Code Lines | 82 |
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 |
||
32 | public function traitement() |
||
33 | { |
||
34 | // On commence par déterminer le mode sélection des données. |
||
35 | $modeSelection = $this->input->getOption("mode"); |
||
36 | $this->modeSelection = !in_array($modeSelection, ['trigger', 'date']) ? "" : $modeSelection; |
||
37 | $this->force = $this->input->getOption("force"); |
||
38 | $this->nombreElementsRestant = $this->input->getOption("nombreElements"); |
||
39 | $this->nombreElementsParPaquet = $this->input->getOption("nombreParPaquet"); |
||
40 | // Gestion de la configuration pour le mode trigger |
||
41 | if ($this->modeSelection == "trigger" && (int)$this->input->getOption("nombreDeMinutes")) { |
||
42 | $nombreDeMinutes = (int)$this->input->getOption("nombreDeMinutes"); |
||
43 | $dateMin = (new \DateTime())->sub(new \DateInterval("PT".$nombreDeMinutes."M")); |
||
44 | $dateMax = new \DateTime(); |
||
45 | $this->dateMin = $dateMin->format("Y-m-d H:i:s"); |
||
46 | $this->dateMax = $dateMax->format("Y-m-d H:i:s"); |
||
47 | } |
||
48 | if ($this->modeSelection == "date") { |
||
49 | $jourMinimum = \DateTime::createFromFormat('Y-m-d', $this->input->getOption("jourMinimum")); |
||
50 | $jourMaximum = \DateTime::createFromFormat('Y-m-d', $this->input->getOption("jourMaximum")); |
||
51 | // On s'assure que les dates soit dans le bon sens. |
||
52 | if (!empty($jourMinimum) && !empty($jourMaximum) && $jourMaximum->format('Y-m-d') < $jourMinimum->format('Y-m-d')) { |
||
53 | $tmp = $jourMaximum; |
||
54 | $jourMaximum = $jourMinimum; |
||
55 | $jourMinimum = $tmp; |
||
56 | unset($tmp); |
||
57 | } |
||
58 | $this->nombreDeJourATraiter = abs((int)$this->input->getOption("nombreDeJours")); |
||
|
|||
59 | if (empty($jourMinimum) && empty($jourMaximum)) { |
||
60 | // min = j-1-$this->nombreDeJourATraiter max =j-1 |
||
61 | $jourMinimum = (new \DateTime())->sub(new \DateInterval("P".($this->nombreDeJourATraiter + 1)."D")); |
||
62 | $jourMaximum = (new \DateTime())->sub(new \DateInterval("P1D")); |
||
63 | } else { |
||
64 | if (!empty($jourMinimum) && empty($jourMaximum)) { |
||
65 | // max = jMin+$this->nombreDeJourATraiter |
||
66 | $jourMaximum = (new \DateTime($jourMinimum->format("Y-m-d")))->add(new \DateInterval("P".$this->nombreDeJourATraiter."D")); |
||
67 | } else { |
||
68 | if (empty($jourMinimum) && !empty($jourMaximum)) { |
||
69 | // min = jMax-$this->nombreDeJourATraiter |
||
70 | $jourMinimum = (new \DateTime($jourMaximum->format("Y-m-d")))->sub(new \DateInterval("P".($this->nombreDeJourATraiter + 1)."D")); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | $this->dateMin = $jourMinimum->format("Y-m-d 00:00:00"); |
||
75 | $this->dateMax = $jourMaximum->format("Y-m-d 00:00:00"); |
||
76 | } |
||
77 | $this->output->writeln( |
||
78 | "<info>[INFO]</info> Le script va exporter les données par paquet de <info>".$this->nombreElementsParPaquet."</info> éléments.", |
||
79 | OutputInterface::VERBOSITY_VERBOSE |
||
80 | ); |
||
81 | if (!empty($this->nombreElementsRestant)) { |
||
82 | $this->output->writeln( |
||
83 | "<info>[INFO]</info> Le script s'arrêtera après avoir exporté <info>".$this->nombreElementsRestant."</info> éléments.", |
||
84 | OutputInterface::VERBOSITY_VERBOSE |
||
85 | ); |
||
86 | } |
||
87 | if (in_array($this->modeSelection, ["date", "trigger"])) { |
||
88 | $this->output->writeln( |
||
89 | "<info>[INFO]</info> Traitement des données entre le <info>".$this->dateMin."</info> et le <info>".$this->dateMax."</info>.", |
||
90 | OutputInterface::VERBOSITY_VERBOSE |
||
91 | ); |
||
92 | } |
||
93 | if ($this->input->getOption("force")) { |
||
94 | $this->output->writeln("<info>[INFO]</info> Le script renverra <info>TOUTES LES DONNEES</info>, y compris celles déjà envoyées.", OutputInterface::VERBOSITY_VERBOSE); |
||
95 | } |
||
96 | $listeIdsANePasImporter = $this->recupererLaListeDesIdsANePasImporter(); |
||
97 | $idMinimum = $this->recupererLIdMinimumAImporter(); // Cette valeur est voué à évoluer. |
||
98 | $this->nombreElementsDejaEnvoye = 0; |
||
99 | while (true) { |
||
100 | $limit = $this->nombreElementsParPaquet; |
||
101 | if (!empty($this->nombreElementsRestant)) { |
||
102 | $nombreDeDonneesRestantesAImporter = $this->nombreElementsRestant - $this->nombreElementsDejaEnvoye; |
||
103 | if ($nombreDeDonneesRestantesAImporter < $this->nombreElementsParPaquet) { |
||
104 | $limit = $nombreDeDonneesRestantesAImporter; |
||
105 | } |
||
106 | } |
||
107 | $donnees = $this->selectionDonneesAExporter($idMinimum, $listeIdsANePasImporter, $limit); |
||
108 | if (empty($donnees)) { |
||
109 | $this->output->writeln("<info>Pas ou plus de données à traiter</info>", OutputInterface::VERBOSITY_VERBOSE); |
||
110 | break; |
||
111 | } |
||
112 | $paquetExport = []; |
||
113 | foreach ($donnees as $row) { |
||
114 | $paquetExport[] = $this->formatageDonnees($row); |
||
115 | $idMinimum = $row["idExterne"]; |
||
116 | } |
||
117 | $serviceHttp = $this->getContainer()->get('outils.httpservice.post'); |
||
118 | $serviceHttp->enableHttpBuildQuery(); |
||
119 | $url = $this->getContainer()->getParameter("starkerxp.api.datas"); |
||
120 | $retour = $serviceHttp->envoyer($url, ['batch' => $paquetExport]); |
||
121 | $retourJson = json_decode($retour, true); |
||
122 | if (!empty($retourJson) && !$retourJson['error']) { |
||
123 | $this->output->writeln("<info>Réussit</info>", OutputInterface::VERBOSITY_VERBOSE); |
||
124 | } else { |
||
125 | if (!empty($retourJson) && $retourJson['error']) { |
||
126 | $this->output->writeln("<info>Échec</info>", OutputInterface::VERBOSITY_VERBOSE); |
||
127 | foreach ($retourJson['errors'] as $erreur => $nombre) { |
||
128 | $this->output->writeln("\t<info>".$nombre."</info> ".$erreur, OutputInterface::VERBOSITY_VERBOSE); |
||
129 | } |
||
130 | } else { |
||
131 | $this->output->writeln("<error>Une erreur est survenue</error>"); |
||
132 | $this->output->writeln($retour); |
||
133 | |||
134 | return false; |
||
135 | } |
||
136 | } |
||
137 | $this->nombreElementsDejaEnvoye += count($donnees); |
||
138 | if (!empty($this->nombreElementsRestant) && $this->nombreElementsDejaEnvoye >= $this->nombreElementsRestant) { |
||
139 | $this->output->writeln("<info>[INFO]</info>On n'importe pas plus de données", OutputInterface::VERBOSITY_VERBOSE); |
||
140 | break; |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | |||
229 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.