Completed
Pull Request — master (#2)
by Pol
07:20 queued 05:24
created

Pi::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 9.3142
cc 3
eloc 12
nc 3
nop 1
crap 12
1
<?php
2
3
namespace drupol\Yaroc\Examples;
4
5
use drupol\Yaroc\Plugin\Provider;
6
7
/**
8
 * Class Pi.
9
 */
10
class Pi extends BaseExample
11
{
12
    /**
13
     * @var float
14
     */
15
    protected $estimation;
16
17
    /**
18
     * @param int $throws
19
     *
20
     * @throws \Http\Client\Exception
21
     *
22
     * @return $this
23
     *
24
     * @SuppressWarnings(PHPMD.StaticAccess)
25
     */
26
    public function run($throws = 1000)
27
    {
28
        $provider = Provider::withResource('generateDecimalFractions')
29
            ->withParameters(['n' => $throws * 2, 'decimalPlaces' => 6]);
30
31
        $numbers = $this->getRandomOrgAPI()->getData($provider);
32
33
        $inside = 0;
34
        for ($i = 0; $i < $throws; $i++) {
35
            $x = $numbers[$i];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $x. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
36
            $y = $numbers[$i+1];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $y. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
37
38
            if (sqrt($x*$x + $y*$y) <= 1) {
39
                $inside++;
40
            }
41
        }
42
43
        $this->estimation = 4 * $inside/$throws;
0 ignored issues
show
Documentation Bug introduced by
The property $estimation was declared of type double, but 4 * $inside / $throws is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return float
50
     */
51
    public function get()
52
    {
53
        return $this->estimation;
54
    }
55
}
56