Issues (23)

Security Analysis    no request data  

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/AbstractQuantity.php (8 issues)

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
 * PHP Units of Measure Library
4
 *
5
 * @link      https://github.com/hiqdev/php-units
6
 * @package   php-units
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units;
12
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Quantity with Unit.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
abstract class AbstractQuantity implements QuantityInterface, \JsonSerializable
21
{
22
    /**
23
     * @var UnitInterface
24
     */
25
    private $unit;
26
27
    /**
28
     * @var float|int|string
29
     */
30
    private $quantity;
31
32
    /**
33
     * @var UnitInterface
34
     * @var float|int|string $quantity
35
     */
36 11
    private function __construct(UnitInterface $unit, $quantity)
37
    {
38 11
        $this->unit = $unit;
39 11
        $this->quantity = $quantity;
40 11
    }
41
42
    /**
43
     * Creates quantity with same unit.
44
     * Optimized to return this if same quantity.
45
     * @var float|int|string
46
     */
47 3
    final protected function repeat($quantity)
48
    {
49 3
        return $this->quantity === $quantity ? $this : static::create($this->unit, $quantity);
0 ignored issues
show
$this->unit is of type object<hiqdev\php\units\UnitInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 5
    private function getCalculator()
56
    {
57 5
        return $this->unit->getCalculator();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 12
    final public function getQuantity()
64
    {
65 12
        return $this->quantity;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 7
    final public function getUnit()
72
    {
73 7
        return $this->unit;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    final public function compare(QuantityInterface $other)
80
    {
81 2
        $arg = $other->convert($this->unit)->getQuantity();
82
83 2
        return $this->getCalculator()->compare($this->quantity, $arg);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    final public function equals(QuantityInterface $other)
90
    {
91 1
        return $this->compare($other) === 0;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    final public function isPositive()
98
    {
99 1
        return $this->quantity > 0;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    final public function isNegative()
106
    {
107 1
        return $this->quantity < 0;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    final public function isConvertible(UnitInterface $unit)
114
    {
115 1
        return $this->unit->isConvertible($unit);
0 ignored issues
show
$unit is of type object<hiqdev\php\units\UnitInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Best Practice introduced by
The return type of return $this->unit->isConvertible($unit); (boolean) is incompatible with the return type declared by the interface hiqdev\php\units\QuantityInterface::isConvertible of type hiqdev\php\units\QuantityInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 5
    final public function convert(UnitInterface $unit)
122
    {
123 5
        $res = $this->unit->convert($unit, $this->quantity);
0 ignored issues
show
$unit is of type object<hiqdev\php\units\UnitInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
125 5
        return static::create($unit, $res);
0 ignored issues
show
$unit is of type object<hiqdev\php\units\UnitInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 1 View Code Duplication
    final public function add(QuantityInterface $addend)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133 1
        $arg = $addend->convert($this->unit)->getQuantity();
134 1
        $res = $this->getCalculator()->add($this->quantity, $arg);
135
136 1
        return $this->repeat($res);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1 View Code Duplication
    final public function subtract(QuantityInterface $subtrahend)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144 1
        $arg = $subtrahend->convert($this->unit)->getQuantity();
145 1
        $res = $this->getCalculator()->subtract($this->quantity, $arg);
146
147 1
        return $this->repeat($res);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 1
    final public function multiply($multiplier)
154
    {
155 1
        $res = $this->getCalculator()->multiply($this->quantity, $multiplier);
156
157 1
        return $this->repeat($res);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    final public function divide($divisor)
164
    {
165
        $res = $this->getCalculator()->divide($this->quantity, $divisor);
166
167
        return $this->repeat($res);
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    final public function ceil()
174
    {
175
        $res = $this->getCalculator()->ceil($this->quantity);
176
177
        return $this->repeat($res);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    final public function floor()
184
    {
185
        $res = $this->getCalculator()->floor($this->quantity);
186
187
        return $this->repeat($res);
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    final public function round($roundingMode)
194
    {
195
        $res = $this->getCalculator()->round($this->quantity, $roundingMode);
196
197
        return $this->repeat($res);
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    final public function absolute()
204
    {
205
        $res = $this->getCalculator()->absolute($this->quantity);
206
207
        return $this->repeat($res);
208
    }
209
210 11
    final public static function __callStatic($unit, $args)
211
    {
212 11
        return static::create($unit, $args[0]);
213
    }
214
215
    /**
216
     * @param string $unit
217
     * @var float|int|string $quantity
218
     * @return static
219
     */
220 11
    public static function create($unit, $quantity)
221
    {
222 11
        return new static(static::findUnit($unit), $quantity);
223
    }
224
225
    /**
226
     * Returns unit for given unit name.
227
     * The only function to change in child classes.
228
     * XXX Should be defined as abstract but `abstract static` is not supported in PHP5.
229
     * @param string $name
0 ignored issues
show
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
230
     * @return UnitInterface
231
     */
232
    protected static function findUnit($unit)
233
    {
234
        throw new InvalidConfigException('getUnit method must be redefined');
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     * @return array
240
     */
241
    public function jsonSerialize()
242
    {
243
        return [
244
            'unit' => $this->unit->getName(),
245
            'quantity' => $this->quantity,
246
        ];
247
    }
248
}
249