Completed
Push — master ( eb5562...b637ab )
by Pol
02:27
created

src/Examples/Pi.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace drupol\Yaroc\Examples;
4
5
/**
6
 * Class Pi.
7
 *
8
 * @package drupol\Yaroc\Examples
9
 */
10
class Pi extends BaseExample {
11
12
  /**
13
   * @var float
14
   */
15
  protected $estimation;
16
17
  /**
18
   * @return self
19
   */
20 1
  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...
21 1
    $numbers = $this->randomOrgAPI->call('generateDecimalFractions', ['n' => $n * 2, 'decimalPlaces' => 6])
22 1
      ->getData();
23
24 1
    $inside = 0;
25 1
    for ($i = 0; $i < $n; $i++) {
26 1
      $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...
27 1
      $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...
28
29 1
      if (sqrt($x*$x + $y*$y) <= 1) {
30 1
        $inside++;
31 1
      }
32 1
    }
33
34 1
    $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...
35
36 1
    return $this;
37
  }
38
39 1
  public function get() {
40 1
    return $this->estimation;
41
  }
42
43
}
44