Passed
Push — feature/code-analysis ( a2ce2d...28b704 )
by Jonathan
04:31
created
app/Module/Sosa/Services/SosaStatisticsService.php 1 patch
Spacing   +58 added lines, -59 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
      */
75 75
     public function maxGeneration(): int
76 76
     {
77
-        return (int) DB::table('maj_sosa')
77
+        return (int)DB::table('maj_sosa')
78 78
             ->where('majs_gedcom_id', '=', $this->tree->id())
79 79
             ->where('majs_user_id', '=', $this->user->id())
80 80
             ->max('majs_gen');
@@ -151,8 +151,7 @@  discard block
 block discarded – undo
151 151
             ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2')
152 152
             ->get()->first();
153 153
 
154
-        return $row->n == 0 ? 0 :
155
-            -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2));
154
+        return $row->n == 0 ? 0 : -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2));
156 155
     }
157 156
 
158 157
     /**
@@ -176,10 +175,10 @@  discard block
 block discarded – undo
176 175
 
177 176
         $statistics_by_gen = [];
178 177
         foreach ($stats_by_gen as $gen => $stats_gen) {
179
-            $statistics_by_gen[(int) $stats_gen->gen] = array(
180
-                'sosaCount'             =>  (int) $stats_gen->total_sosa,
181
-                'sosaTotalCount'        =>  (int) $cumul_stats_by_gen[$gen]->total_cumul,
182
-                'diffSosaTotalCount'    =>  (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul,
178
+            $statistics_by_gen[(int)$stats_gen->gen] = array(
179
+                'sosaCount'             =>  (int)$stats_gen->total_sosa,
180
+                'sosaTotalCount'        =>  (int)$cumul_stats_by_gen[$gen]->total_cumul,
181
+                'diffSosaTotalCount'    =>  (int)$cumul_stats_by_gen[$gen]->total_distinct_cumul,
183 182
                 'firstBirth'            =>  $stats_gen->first_year,
184 183
                 'firstEstimatedBirth'   =>  $stats_gen->first_est_year,
185 184
                 'lastBirth'             =>  $stats_gen->last_year,
@@ -223,7 +222,7 @@  discard block
 block discarded – undo
223 222
             ->where('majs_user_id', '=', $this->user->id());
224 223
 
225 224
         return DB::table('maj_sosa')
226
-            ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void {
225
+            ->joinSub($list_gen, 'list_gen', function(JoinClause $join): void {
227 226
                 $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen')
228 227
                 ->where('majs_gedcom_id', '=', $this->tree->id())
229 228
                 ->where('majs_user_id', '=', $this->user->id());
@@ -264,37 +263,37 @@  discard block
 block discarded – undo
264 263
             ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id'])
265 264
             ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen'])
266 265
             ->selectRaw(
267
-                '(CASE ' .
268
-                    ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' .
269
-                    ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' .
270
-                    ' ELSE 0 ' .
271
-                ' END)' .
272
-                ' + (CASE ' .
273
-                    ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' .
274
-                    ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' .
275
-                    ' ELSE 0 ' .
266
+                '(CASE '.
267
+                    ' WHEN '.$table_prefix.'sosa_fat.majs_i_id IS NULL'.
268
+                    ' THEN POWER(2, '.$table_prefix.'list_gen.majs_gen - '.$table_prefix.'sosa.majs_gen - 1)'.
269
+                    ' ELSE 0 '.
270
+                ' END)'.
271
+                ' + (CASE '.
272
+                    ' WHEN '.$table_prefix.'sosa_mot.majs_i_id IS NULL'.
273
+                    ' THEN POWER(2, '.$table_prefix.'list_gen.majs_gen - '.$table_prefix.'sosa.majs_gen - 1)'.
274
+                    ' ELSE 0 '.
276 275
                 ' END) contrib'
277 276
             )
278
-            ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void {
277
+            ->joinSub($list_gen, 'list_gen', function(JoinClause $join): void {
279 278
                 $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen')
280 279
                     ->where('majs_gedcom_id', '=', $this->tree->id())
281 280
                     ->where('majs_user_id', '=', $this->user->id());
282 281
             })
283
-            ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void {
282
+            ->leftJoin('maj_sosa AS sosa_fat', function(JoinClause $join) use ($table_prefix): void {
284 283
                 // Link to sosa's father
285
-                $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa')
284
+                $join->whereRaw($table_prefix.'sosa_fat.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa')
286 285
                     ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id())
287 286
                     ->where('sosa_fat.majs_user_id', '=', $this->user->id());
288 287
             })
289
-            ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void {
288
+            ->leftJoin('maj_sosa AS sosa_mot', function(JoinClause $join) use ($table_prefix): void {
290 289
                 // Link to sosa's mother
291
-                $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1')
290
+                $join->whereRaw($table_prefix.'sosa_mot.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa + 1')
292 291
                     ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id())
293 292
                     ->where('sosa_mot.majs_user_id', '=', $this->user->id());
294 293
             })
295 294
             ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
296 295
             ->where('sosa.majs_user_id', '=', $this->user->id())
297
-            ->where(function (Builder $query): void {
296
+            ->where(function(Builder $query): void {
298 297
                 $query->whereNull('sosa_fat.majs_i_id')
299 298
                 ->orWhereNull('sosa_mot.majs_i_id');
300 299
             });
@@ -304,24 +303,24 @@  discard block
 block discarded – undo
304 303
          */
305 304
         $non_roots_ancestors = DB::table('maj_sosa AS sosa')
306 305
             ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa'])
307
-            ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' .
306
+            ->selectRaw('MAX('.$table_prefix.'sosa_anc.majs_sosa) - MIN('.$table_prefix.'sosa_anc.majs_sosa)'.
308 307
                 ' AS full_ancestors')
309
-            ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void {
308
+            ->join('maj_sosa AS sosa_anc', function(JoinClause $join) use ($table_prefix): void {
310 309
                 $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen')
311
-                    ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' .
312
-                        $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' .
313
-                        $table_prefix . 'sosa.majs_sosa')
310
+                    ->whereRaw('FLOOR('.$table_prefix.'sosa_anc.majs_sosa / POWER(2, '.
311
+                        $table_prefix.'sosa_anc.majs_gen - '.$table_prefix.'sosa.majs_gen)) = '.
312
+                        $table_prefix.'sosa.majs_sosa')
314 313
                     ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id())
315 314
                     ->where('sosa_anc.majs_user_id', '=', $this->user->id());
316 315
             })
317 316
             ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
318 317
             ->where('sosa.majs_user_id', '=', $this->user->id())
319
-            ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void {
318
+            ->whereIn('sosa_anc.majs_i_id', function(Builder $query) use ($table_prefix): void {
320 319
                 $query->from('maj_sosa AS sosa_gen')
321 320
                 ->select('sosa_gen.majs_i_id')->distinct()
322 321
                 ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id())
323 322
                 ->where('sosa_gen.majs_user_id', '=', $this->user->id())
324
-                ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen');
323
+                ->whereRaw($table_prefix.'sosa_gen.majs_gen = '.$table_prefix.'sosa.majs_gen');
325 324
             })
326 325
             ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id',
327 326
                 'sosa.majs_sosa', 'sosa.majs_i_id']);
@@ -334,7 +333,7 @@  discard block
 block discarded – undo
334 333
             ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id'])
335 334
             ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen'])
336 335
             ->selectRaw('1 AS contrib')
337
-            ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void {
336
+            ->leftJoinSub($non_roots_ancestors, 'nonroot', function(JoinClause $join): void {
338 337
                 $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen')
339 338
                     ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa')
340 339
                     ->where('nonroot.full_ancestors', '>', 0)
@@ -352,9 +351,9 @@  discard block
 block discarded – undo
352 351
             ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs')
353 352
             ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id'])
354 353
             ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib'])
355
-            ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' .
356
-                $table_prefix . 'sosa_contribs.contrib AS totalContrib')
357
-            ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void {
354
+            ->selectRaw('COUNT('.$table_prefix.'sosa_contribs.majs_i_id) * '.
355
+                $table_prefix.'sosa_contribs.contrib AS totalContrib')
356
+            ->leftJoin('maj_sosa AS sosa_low', function(JoinClause $join): void {
358 357
                 $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen')
359 358
                     ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id')
360 359
                     ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id())
@@ -375,9 +374,9 @@  discard block
 block discarded – undo
375 374
 
376 375
         $pedi_collapse_by_gen = [];
377 376
         foreach ($pedi_collapse_coll as $collapse_gen) {
378
-            $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array(
379
-                'pedi_collapse_roots'   =>  (float) $collapse_gen->pedi_collapse_roots,
380
-                'pedi_collapse_xgen'   =>  (float) $collapse_gen->pedi_collapse_xgen
377
+            $pedi_collapse_by_gen[(int)$collapse_gen->gen] = array(
378
+                'pedi_collapse_roots'   =>  (float)$collapse_gen->pedi_collapse_roots,
379
+                'pedi_collapse_xgen'   =>  (float)$collapse_gen->pedi_collapse_xgen
381 380
             );
382 381
         }
383 382
         return $pedi_collapse_by_gen;
@@ -409,26 +408,26 @@  discard block
 block discarded – undo
409 408
 
410 409
         $table_prefix = DB::connection()->getTablePrefix();
411 410
         $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa')
412
-            ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen])
413
-            ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen])   //@phpcs:ignore Generic.Files.LineLength.TooLong
414
-            ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count')  //@phpcs:ignore Generic.Files.LineLength.TooLong
415
-            ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count')  //@phpcs:ignore Generic.Files.LineLength.TooLong
416
-            ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void {
411
+            ->selectRaw($table_prefix.'sosa.majs_gen - ? AS majs_gen_norm', [$gen])
412
+            ->selectRaw('FLOOR((('.$table_prefix.'sosa.majs_sosa / POW(2, '.$table_prefix.'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen])   //@phpcs:ignore Generic.Files.LineLength.TooLong
413
+            ->selectRaw('SUM(CASE WHEN '.$table_prefix.'sosa_fat.majs_i_id IS NULL AND '.$table_prefix.'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count')  //@phpcs:ignore Generic.Files.LineLength.TooLong
414
+            ->selectRaw('SUM(CASE WHEN '.$table_prefix.'sosa_fat.majs_i_id IS NULL AND '.$table_prefix.'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count')  //@phpcs:ignore Generic.Files.LineLength.TooLong
415
+            ->leftJoin('maj_sosa AS sosa_fat', function(JoinClause $join) use ($table_prefix): void {
417 416
                 // Link to sosa's father
418
-                $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa')
417
+                $join->whereRaw($table_prefix.'sosa_fat.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa')
419 418
                 ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id())
420 419
                 ->where('sosa_fat.majs_user_id', '=', $this->user->id());
421 420
             })
422
-            ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void {
421
+            ->leftJoin('maj_sosa AS sosa_mot', function(JoinClause $join) use ($table_prefix): void {
423 422
                 // Link to sosa's mother
424
-                $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1')
423
+                $join->whereRaw($table_prefix.'sosa_mot.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa + 1')
425 424
                 ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id())
426 425
                 ->where('sosa_mot.majs_user_id', '=', $this->user->id());
427 426
             })
428 427
             ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
429 428
             ->where('sosa.majs_user_id', '=', $this->user->id())
430 429
             ->where('sosa.majs_gen', '>=', $gen)
431
-            ->where(function (Builder $query): void {
430
+            ->where(function(Builder $query): void {
432 431
                 $query->whereNull('sosa_fat.majs_i_id')
433 432
                     ->orWhereNull('sosa_mot.majs_i_id');
434 433
             })
@@ -437,11 +436,11 @@  discard block
 block discarded – undo
437 436
         return DB::table('maj_sosa AS sosa_list')
438 437
             ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id'])
439 438
             ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth')  //@phpcs:ignore Generic.Files.LineLength.TooLong
440
-            ->selectRaw(' SQRT(' .
441
-                '   SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm)))' .     //@phpcs:ignore Generic.Files.LineLength.TooLong
442
-                '   - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm))), 2)' .       //@phpcs:ignore Generic.Files.LineLength.TooLong
439
+            ->selectRaw(' SQRT('.
440
+                '   SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm)))'.//@phpcs:ignore Generic.Files.LineLength.TooLong
441
+                '   - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm))), 2)'.//@phpcs:ignore Generic.Files.LineLength.TooLong
443 442
                 ' ) AS stddev_gen_depth')
444
-            ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void {
443
+            ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function(JoinClause $join): void {
445 444
                 $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor')
446 445
                     ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id())
447 446
                     ->where('sosa_list.majs_user_id', '=', $this->user->id());
@@ -470,16 +469,16 @@  discard block
 block discarded – undo
470 469
         $table_prefix = DB::connection()->getTablePrefix();
471 470
         $multiple_ancestors = DB::table('maj_sosa AS sosa')
472 471
             ->select('sosa.majs_i_id AS sosa_i_id')
473
-            ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count')
474
-            ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void {
472
+            ->selectRaw('COUNT('.$table_prefix.'sosa.majs_sosa) AS sosa_count')
473
+            ->leftJoin('maj_sosa AS sosa_fat', function(JoinClause $join) use ($table_prefix): void {
475 474
                 // Link to sosa's father
476
-                $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa')
475
+                $join->whereRaw($table_prefix.'sosa_fat.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa')
477 476
                     ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id())
478 477
                     ->where('sosa_fat.majs_user_id', '=', $this->user->id());
479 478
             })
480
-            ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void {
479
+            ->leftJoin('maj_sosa AS sosa_mot', function(JoinClause $join) use ($table_prefix): void {
481 480
                 // Link to sosa's mother
482
-                $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1')
481
+                $join->whereRaw($table_prefix.'sosa_mot.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa + 1')
483 482
                 ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id())
484 483
                 ->where('sosa_mot.majs_user_id', '=', $this->user->id());
485 484
             })
@@ -488,15 +487,15 @@  discard block
 block discarded – undo
488 487
             ->whereNull('sosa_fat.majs_sosa')   // We keep only root individuals, i.e. those with no father or mother
489 488
             ->whereNull('sosa_mot.majs_sosa')
490 489
             ->groupBy('sosa.majs_i_id')
491
-            ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1')    // Limit to the duplicate sosas.
492
-            ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC')   //@phpcs:ignore Generic.Files.LineLength.TooLong
490
+            ->havingRaw('COUNT('.$table_prefix.'sosa.majs_sosa) > 1')    // Limit to the duplicate sosas.
491
+            ->orderByRaw('COUNT('.$table_prefix.'sosa.majs_sosa) DESC, MIN('.$table_prefix.'sosa.majs_sosa) ASC')   //@phpcs:ignore Generic.Files.LineLength.TooLong
493 492
             ->limit($limit + 1)     // We want to select one more than required, for ties
494 493
             ->get();
495 494
 
496 495
         if ($multiple_ancestors->count() > $limit) {
497 496
             $last_count = $multiple_ancestors->last()->sosa_count;
498 497
             $multiple_ancestors = $multiple_ancestors->reject(
499
-                fn (stdClass $element): bool => $element->sosa_count ===  $last_count
498
+                fn (stdClass $element): bool => $element->sosa_count === $last_count
500 499
             );
501 500
         }
502 501
         return $multiple_ancestors;
@@ -551,7 +550,7 @@  discard block
 block discarded – undo
551 550
             ->rightJoinSub(
552 551
                 $consolidated_ancestors_branches,
553 552
                 'indi_branch_consolidated',
554
-                function (JoinClause $join) use ($gen): void {
553
+                function(JoinClause $join) use ($gen): void {
555 554
                     $join->where('maj_sosa.majs_gedcom_id', '=', $this->tree->id())
556 555
                         ->where('maj_sosa.majs_user_id', '=', $this->user->id())
557 556
                         ->where('branches', '>', 0)
Please login to merge, or discard this patch.