Completed
Branch master (5a7b19)
by Evgenii
02:31
created

__invoke()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 17
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 5
nop 2
crap 30
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Illuminate\Support\Collection;
6
use Laratrade\Indicators\Contracts\Indicator;
7
use Laratrade\Indicators\Exceptions\NotEnoughDataException;
8
use Throwable;
9
10
class HilbertTransformTrendVersusCycleModeIndicator implements Indicator
11
{
12
    /**
13
     * Invoke the indicator.
14
     *
15
     * @param Collection $ohlcv
16
     * @param bool       $numPeriods
17
     *
18
     * @return int
19
     *
20
     * @throws Throwable
21
     */
22
    public function __invoke(Collection $ohlcv, bool $numPeriods = false): int
23
    {
24
        $htm = trader_ht_trendmode(
25
            $ohlcv->get('open'),
26
            $ohlcv->get('close')
27
        );
28
29
        throw_unless($htm, NotEnoughDataException::class);
30
31
        $htmValue = array_pop($htm);
32
33
        /**
34
         *  We can return the number of periods we have been
35
         *  in either a trend or a cycle by calling this again with
36
         *  $numperiods == true
37
         */
38
        if ($numPeriods) {
39
            $nump = 1;
40
41
            for ($b = 0; $b < count($htm); $b++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
42
                $test = array_pop($htm);
43
                if ($test == $htmValue) {
44
                    $nump++;
45
                } else {
46
                    break;
47
                }
48
            }
49
50
            return $nump;
51
        } elseif ($htmValue == 1) {
52
            return static::BUY;
53
        }
54
55
        return static::HOLD;
56
    }
57
}
58