Completed
Pull Request — master (#3)
by
unknown
01:16
created

HilbertTransformTrendVersusCycleModeIndicator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 49
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 44 7
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Illuminate\Support\Collection;
6
use Laratrade\Indicators\Contracts\Indicator;
7
use Laratrade\Indicators\Exceptions\NotEnoughDataPointsException;
8
9
10
/**
11
 * Hilbert Transform - Trend vs Cycle Mode
12
 *
13
 *
14
 * Simply tell us if the market is
15
 * either trending or cycling, with an additional parameter the method returns
16
 * the number of days we have been in a trend or a cycle.
17
 */
18
class HilbertTransformTrendVersusCycleModeIndicator implements Indicator
19
{
20
21
    public function __invoke(Collection $ohlcv, bool $numperiods = false): int
22
    {
23
24
        $a_htm = trader_ht_trendmode($ohlcv->get('close'));
25
26
        if (false === $a_htm) {
27
            throw new NotEnoughDataPointsException('Not enough data points');
28
        }
29
30
31
        if (!$a_htm) {
32
            throw new \RuntimeException('Not enough data points. Maybe clear cache and start over.');
33
        }
34
35
        $htm = array_pop($a_htm);
36
37
        /**
38
         *  We can return the number of periods we have been
39
         *  in either a trend or a cycle by calling this again with
40
         *  $numperiods == true
41
         */
42
        if ($numperiods) {
43
            $nump = 1;
44
            $test = $htm;
0 ignored issues
show
Unused Code introduced by
$test is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
            for ($b = 0; $b < count($a_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...
46
                $test = array_pop($a_htm);
47
                if ($test == $htm) {
48
                    $nump++;
49
                } else {
50
                    break;
51
                }
52
            }
53
            return $nump;
54
        }
55
56
        /**
57
         *  Otherwise we just return if we are in a trend or not.
58
         */
59
        if ($htm == 1) {
60
            return 1; // we are in a trending mode
61
        }
62
63
        return 0; // we are cycling.
64
    }
65
66
}
67