Issues (15)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Metrics/RangedValue.php (2 issues)

Labels
Severity

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
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Metrics;
6
7
use Arcanedev\LaravelMetrics\Metrics\Concerns\{HasRanges, HasRoundedValue};
8
use Arcanedev\LaravelMetrics\Results\RangedValueResult;
9
10
/**
11
 * Class     RangedValue
12
 *
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class RangedValue extends Metric
16
{
17
    /* -----------------------------------------------------------------
18
     |  Traits
19
     | -----------------------------------------------------------------
20
     */
21
22
    use HasRanges,
23
        HasRoundedValue;
24
25
    /* -----------------------------------------------------------------
26
     |  Getters & Setters
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * Get the metric type.
32
     *
33
     * @return string
34
     */
35 8
    public function type(): string
36
    {
37 8
        return 'ranged-value';
38
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Main Methods
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Calculate the `count` of the metric.
47
     *
48
     * @param  \Illuminate\Database\Eloquent\Model|string  $model
49
     * @param  string|null                                 $column
50
     * @param  string|null                                 $dateColumn
51
     *
52
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
53
     */
54 12
    protected function count($model, $column = null, $dateColumn = null)
55
    {
56 12
        return $this->aggregate('count', $model, $column, $dateColumn);
57
    }
58
59
    /**
60
     * Calculate the `average` of the metric.
61
     *
62
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
63
     * @param  string                                        $column
64
     * @param  string|null                                   $dateColumn
65
     *
66
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
67
     */
68 4
    public function average($model, $column, $dateColumn = null)
69
    {
70 4
        return $this->aggregate('avg', $model, $column, $dateColumn);
71
    }
72
73
    /**
74
     * Calculate the `sum` of the metric.
75
     *
76
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
77
     * @param  string                                        $column
78
     * @param  string|null                                   $dateColumn
79
     *
80
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
81
     */
82 4
    public function sum($model, $column, $dateColumn = null)
83
    {
84 4
        return $this->aggregate('sum', $model, $column, $dateColumn);
85
    }
86
87
    /**
88
     * Calculate the `max` of the metric.
89
     *
90
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
91
     * @param  string                                        $column
92
     * @param  string|null                                   $dateColumn
93
     *
94
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
95
     */
96 4
    public function max($model, $column, $dateColumn = null)
97
    {
98 4
        return $this->aggregate('max', $model, $column, $dateColumn);
99
    }
100
101
    /**
102
     * Calculate the `min` of the metric.
103
     *
104
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
105
     * @param  string                                        $column
106
     * @param  string|null                                   $dateColumn
107
     *
108
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
109
     */
110 4
    public function min($model, $column, $dateColumn = null)
111
    {
112 4
        return $this->aggregate('min', $model, $column, $dateColumn);
113
    }
114
115
    /* -----------------------------------------------------------------
116
     |  Other Methods
117
     | -----------------------------------------------------------------
118
     */
119
120
    /**
121
     * Handle the aggregate calculation of the metric.
122
     *
123
     * @param  string                                      $method
124
     * @param  \Illuminate\Database\Eloquent\Model|string  $model
125
     * @param  string|null                                 $column
126
     * @param  string|null                                 $dateColumn
127
     *
128
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
129
     */
130 28
    private function aggregate(string $method, $model, $column = null, $dateColumn = null)
131
    {
132 28
        $query      = static::getQuery($model);
133 28
        $column     = $column ?? $query->getModel()->getQualifiedKeyName();
0 ignored issues
show
The method getQualifiedKeyName does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
134 28
        $dateColumn = $dateColumn ?? $query->getModel()->getCreatedAtColumn();
0 ignored issues
show
The method getCreatedAtColumn does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
135 28
        $range      = (int) $this->request->input('range', 1);
136 28
        $timezone   = $this->getCurrentTimezone($this->request);
137
138 28
        $current  = with(clone $query)->whereBetween($dateColumn, $this->currentRange($range, $timezone))->{$method}($column);
139 28
        $previous = with(clone $query)->whereBetween($dateColumn, $this->previousRange($range, $timezone))->{$method}($column);
140
141 28
        return $this->result($method === 'count' ? $current : $this->roundValue($current))
142 28
                    ->previous($method === 'count' ? $previous : $this->roundValue($previous));
143
    }
144
145
    /**
146
     * Prepare the metric for JSON serialization.
147
     *
148
     * @return array
149
     */
150 4
    public function toArray(): array
151
    {
152 4
        return array_merge(
153 4
            parent::toArray(),
154 4
            ['ranges' => $this->rangesToArray()]
155
        );
156
    }
157
158
    /**
159
     * Make a new result instance.
160
     *
161
     * @param  mixed|null  $value
162
     *
163
     * @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed
164
     */
165 28
    protected function result($value = null)
166
    {
167 28
        return new RangedValueResult($value);
168
    }
169
}
170