Completed
Pull Request — master (#2)
by Pol
02:04
created

Pi   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
cbo 4
dl 0
loc 44
ccs 0
cts 20
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 21 3
A get() 0 4 1
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 $n
19
     *
20
     * @throws \Http\Client\Exception
21
     *
22
     * @return $this
23
     */
24
    public function run($n = 1000)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. 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...
25
    {
26
        $provider = Provider::withResource('generateDecimalFractions')
27
            ->withParameters(['n' => $n * 2, 'decimalPlaces' => 6]);
28
29
        $numbers = $this->getRandomOrgAPI()->getData($provider);
30
31
        $inside = 0;
32
        for ($i = 0; $i < $n; $i++) {
33
            $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...
34
            $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...
35
36
            if (sqrt($x*$x + $y*$y) <= 1) {
37
                $inside++;
38
            }
39
        }
40
41
        $this->estimation = 4 * $inside/$n;
0 ignored issues
show
Documentation Bug introduced by
The property $estimation was declared of type double, but 4 * $inside / $n 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...
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return float
48
     */
49
    public function get()
50
    {
51
        return $this->estimation;
52
    }
53
}
54