Passed
Push — develop ( bf9913...29023c )
by Septianata
01:50 queued 14s
created

Factorial::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
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
    protected int $result = 1;
11
12
    /**
13
     * Create a new instance class.
14
     *
15
     * @return void
16
     */
17 3
    public function __construct(protected int $initialN)
18
    {
19
        //
20 3
    }
21
22
    /**
23
     * Return result of the factorial using loop way.
24
     */
25 1
    public function resultWithLoop(): int
26
    {
27 1
        if ($this->initialN <= 1) {
28 1
            return 1;
29
        }
30
31 1
        $result = 1;
32
33 1
        for ($n = $this->initialN; $n >= 1; $n--) {
34 1
            $result *= $n;
35
        }
36
37 1
        return $result;
38
    }
39
40
    /**
41
     * Return result of the factorial using recursive way.
42
     */
43 1
    public function resultWithRecursive(): int
44
    {
45 1
        if ($this->initialN <= 1) {
46 1
            return 1;
47
        }
48
49 1
        $n = $this->initialN;
50
51 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

51
        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...
52
    }
53
54
    /**
55
     * Return result of the factorial using tail recursive way.
56
     */
57 1
    public function resultWithTailRecursive(): int
58
    {
59 1
        if ($this->initialN <= 0) {
60 1
            return $this->result;
61
        }
62
63 1
        $this->result *= $this->initialN;
64 1
        $this->initialN--;
65
66 1
        return $this->resultWithTailRecursive();
67
    }
68
}
69