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/tree/RootUnit.php (5 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\tree;
12
13
use hiqdev\php\units\CalculatorInterface;
14
use hiqdev\php\units\calculators\PhpCalculator;
15
use hiqdev\php\units\exceptions\InvalidArgumentException;
16
use hiqdev\php\units\exceptions\NotConvertibleException;
17
use hiqdev\php\units\UnitInterface;
18
19
/**
20
 * Root unit in tree.
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class RootUnit implements UnitInterface
25
{
26
    /**
27
     * @var TreeConverter
28
     */
29
    protected $tree;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 10
    public function getName()
40
    {
41 10
        return $this->name;
42
    }
43
44
    /** @return UnitInterface */
45 7
    public function getParent()
46
    {
47 7
        return $this;
48
    }
49
50
    /** @return UnitInterface */
51 10
    public function getRoot()
52
    {
53 10
        return $this;
54
    }
55
56 8
    public function getFactor()
57
    {
58 8
        return null;
59
    }
60
61 8
    public function getMeasure()
62
    {
63 8
        return $this->name;
64
    }
65
66
    /**
67
     * @param TreeConverter $tree
68
     * @param string $name
69
     */
70 5
    public function __construct(TreeConverter $tree, $name)
71
    {
72 5
        $this->tree = $tree;
73 5
        $this->name = $name;
74 5
    }
75
76
    protected $calculator;
77
78
    /**
79
     * Returns calculator for this unit.
80
     * Units can have different calculators.
81
     * @return CalculatorInterface
82
     */
83 8
    public function getCalculator()
84
    {
85 8
        if ($this->calculator === null) {
86 3
            $this->calculator = new PhpCalculator();
87
        }
88
89 8
        return $this->calculator;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 9
    public function equals(UnitInterface $other)
96
    {
97 9
        return $this->getName() === $other->getName() || $this->isAlias($other);
98
    }
99
100 9
    public function isAlias(UnitInterface $other)
101
    {
102 9
        $node = $this->getNode($other);
103
104 9
        return $this === $node->getCanon()
105 9
            || $node === $this->getCanon()
106
            || (
107 9
                $this->getRoot() === $this->getNode($other)->getRoot()
108 9
                && $this->getFactor() === $this->getNode($other)->getFactor()
109
            );
110
    }
111
112 8
    public function getCanon()
113
    {
114 8
        return $this;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 7
    public function assertConvertible(UnitInterface $other)
121
    {
122 7
        if (!$this->isConvertible($other)) {
123
            throw new InvalidArgumentException('not convertible unit: from ' . $this->getName() . ' to ' . $other->getName());
124
        }
125 7
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 9
    final public function isConvertible(UnitInterface $other)
131
    {
132 9
        return $this->getMeasure() === $other->getMeasure();
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 7
    public function convert(UnitInterface $other, $quantity)
139
    {
140 7
        $this->assertConvertible($other);
141
142 7
        $other = $this->getNode($other);
143
144 7
        if ($this->equals($other)) {
145 6
            return $quantity;
146
        }
147
148 7
        if ($other->equals($this->getParent())) {
149 7
            return $this->multiplyByFactor($quantity);
150
        }
151
152 7
        if ($this->equals($other->getParent())) {
0 ignored issues
show
It seems like $other->getParent() can be null; however, equals() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
153 7
            return $other->divideByFactor($quantity);
154
        }
155
156 7
        return $this->getRoot()->convert($other, $this->convert($this->getRoot(), $quantity));
0 ignored issues
show
$other is of type object<hiqdev\php\units\tree\NodeUnit>, 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...
157
    }
158
159 7 View Code Duplication
    public function multiplyByFactor($quantity)
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...
160
    {
161 7
        if (empty($this->factor)) {
0 ignored issues
show
The property factor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
162
            throw new NotConvertibleException('method not yet implement: ' . $this->getName());
163
        }
164
165 7
        return $this->getCalculator()->multiply($quantity, $this->factor);
166
    }
167
168 7 View Code Duplication
    public function divideByFactor($quantity)
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...
169
    {
170 7
        if (empty($this->factor)) {
171
            throw new NotConvertibleException('method not yet implement: ' . $this->getName());
172
        }
173
174 7
        return $this->getCalculator()->divide($quantity, $this->factor);
175
    }
176
177 9
    public function getNode(UnitInterface $unit)
178
    {
179 9
        return $this->tree->getNode($unit);
180
    }
181
}
182