Test Failed
Pull Request — master (#2)
by Pol
03:30
created

Pi::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

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 23
ccs 0
cts 17
cp 0
rs 9.0856
cc 3
eloc 13
nc 3
nop 1
crap 12
1
<?php
2
3
namespace drupol\Yaroc\Examples;
4
5
use drupol\Yaroc\Plugin\Method\GenerateDecimalFractions;
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...
Coding Style Naming introduced by
The variable $GenerateDecimalFractions is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
25
    {
26
        $parameters = ['n' => $n * 2, 'decimalPlaces' => 6];
27
28
        $GenerateDecimalFractions = (new GenerateDecimalFractions($this->getHttpClient()))
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $GenerateDecimalFractions exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
29
            ->withParameters($parameters);
30
31
        $numbers = $this->getRandomOrgAPI()->getData($GenerateDecimalFractions);
32
33
        $inside = 0;
34
        for ($i = 0; $i < $n; $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/$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...
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return float
50
     */
51
    public function get()
52
    {
53
        return $this->estimation;
54
    }
55
}
56