Passed
Push — master ( 64a9e4...211325 )
by Septianata
03:21 queued 01:46
created

Factorial::resultWithRecursive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Ianriizky\CodingInterview\Factorial;
4
5
class Factorial
6
{
7
    /**
8
     * Temporary value for the recursive value.
9
     *
10
     * @var int
11
     */
12
    protected int $result = 1;
13
14
    /**
15
     * Create a new instance class.
16
     *
17
     * @param  int  $initialN
18
     * @return void
19
     */
20 3
    public function __construct(protected int $initialN)
21
    {
22
        //
23 3
    }
24
25
    /**
26
     * Return result of the factorial using loop way.
27
     *
28
     * @return int
29
     */
30 1
    public function resultWithLoop(): int
31
    {
32 1
        if ($this->initialN <= 1) {
33 1
            return 1;
34
        }
35
36 1
        $result = 1;
37
38 1
        for ($n = $this->initialN; $n >= 1; $n--) {
39 1
            $result *= $n;
40
        }
41
42 1
        return $result;
43
    }
44
45
    /**
46
     * Return result of the factorial using recursive way.
47
     *
48
     * @return int
49
     */
50 1
    public function resultWithRecursive(): int
51
    {
52 1
        if ($this->initialN <= 1) {
53 1
            return 1;
54
        }
55
56 1
        $n = $this->initialN;
57
58 1
        return $n * $this->resultWithRecursive($this->initialN = $n - 1);
0 ignored issues
show
Unused Code introduced by
The call to Ianriizky\CodingIntervie...::resultWithRecursive() has too many arguments starting with $this->initialN = $n - 1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        return $n * $this->/** @scrutinizer ignore-call */ resultWithRecursive($this->initialN = $n - 1);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
    }
60
61
    /**
62
     * Return result of the factorial using tail recursive way.
63
     *
64
     * @return int
65
     */
66 1
    public function resultWithTailRecursive(): int
67
    {
68 1
        if ($this->initialN <= 0) {
69 1
            return $this->result;
70
        }
71
72 1
        $this->result *= $this->initialN;
73 1
        $this->initialN--;
74
75 1
        return $this->resultWithTailRecursive();
76
    }
77
}
78