Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Forensics
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 benfordAnalysis
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 expectedBenfordDistribution
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Business\Finance
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Business\Finance;
16
17/**
18 * Forensics class.
19 *
20 * @package phpOMS\Business\Finance
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25final class Forensics
26{
27    /**
28     * Constructor
29     *
30     * @since 1.0.0
31     * @codeCoverageIgnore
32     */
33    private function __construct()
34    {
35    }
36
37    /**
38     * Perform the Benford analysis
39     *
40     * @param array $data Data to analyze
41     *
42     * @return array
43     *
44     * @since 1.0.0
45     */
46    public static function benfordAnalysis(array $data) : array
47    {
48        $digits = \array_fill(1, 9, 0);
49        $size   = \count($data);
50
51        foreach ($data as $number) {
52            $digit = \substr((string) $number, 0, 1);
53            ++$digits[(int) $digit];
54        }
55
56        $results = [];
57        foreach ($digits as $digit => $count) {
58            $results[$digit] = $count / $size;
59        }
60
61        return $results;
62    }
63
64    /**
65     * Calculate the general Benford distribution
66     *
67     * @return array
68     *
69     * @since 1.0.0
70     */
71    public static function expectedBenfordDistribution() : array
72    {
73        $expected = [];
74        for ($i = 1; $i <= 9; ++$i) {
75            $expected[$i] = \log10(1 + 1 / $i);
76        }
77
78        return $expected;
79    }
80}