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