Issues (7)

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/Number.php (6 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
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\Math;
14
15
/**
16
 * Class Number
17
 * @package Lovenunu\Math
18
 * @author Benjamin Perche <[email protected]>
19
 */
20
class Number implements \Serializable, \JsonSerializable
21
{
22
    // Regex to find correct numbers
23
    const NUMBER_PATTERN = "#^\d+(\.\d+)?$#";
24
25
    // Internal variables
26
    protected $dividend;
27
28
    protected $divisor;
29
30
    // --- Object construction ------------
31
    public function __construct($number = null)
32
    {
33
        list($this->dividend, $this->divisor) = $this->retrieve($number);
34
    }
35
36
    protected function initialize($dividend, $divisor)
37
    {
38
        $this->dividend = $dividend;
39
40
        $this->divisor = $divisor;
41
    }
42
43
    // --- Number processor ------------
44
45
    protected function retrieve($number)
46
    {
47
        $dividend = 0;
48
        $divisor = 1;
49
50
        if ($number !== null) {
51
            if (is_scalar($number) && preg_match(static::NUMBER_PATTERN, $number)) {
52
                $dividend = (int) str_replace(".", "", $number);
53
54
                if (false !== $pos = strpos($number, ".")) {
55
                    $divisor = pow(10, strlen($number) - $pos - 1);
56
                }
57
            } elseif ($number instanceof $this) {
58
                $dividend = $number->getDividend();
59
                $divisor = $number->getDivisor();
60
            } else {
61
                throw new \InvalidArgumentException(
62
                    "The given price is not valid"
63
                );
64
            }
65
        }
66
67
        return array($dividend, $divisor);
68
    }
69
70
    protected function computeDividend($askedDividend, $askedDivisor, Number $number)
71
    {
72
        if ($number->divisor !== $askedDivisor) {
73
            $number->dividend *= $askedDivisor;
74
            $askedDividend *= $number->divisor;
75
76
            $number->divisor *= $askedDivisor;
77
        }
78
79
        return $askedDividend;
80
    }
81
82
    protected function simplify()
83
    {
84
        if (1 < $gcd = GCD::getGCD($this->dividend, $this->divisor)) {
85
            $this->dividend /= $gcd;
86
            $this->divisor /= $gcd;
87
        }
88
    }
89
90
    // --- Operations ------------
91
92 View Code Duplication
    public function add($number)
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...
93
    {
94
        list($askedDividend, $askedDivisor) = $this->retrieve($number);
95
        $returnNumber = clone $this;
96
97
        $askedDividend = $this->computeDividend($askedDividend, $askedDivisor, $returnNumber);
98
99
        /**
100
         * If divisors are different, just multiply them
101
         */
102
103
        $returnNumber->dividend += $askedDividend;
104
        $returnNumber->simplify();
105
106
        return $returnNumber;
107
    }
108
109 View Code Duplication
    public function sub($number)
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...
110
    {
111
        list($askedDividend, $askedDivisor) = $this->retrieve($number);
112
        $returnNumber = clone $this;
113
114
        $askedDividend = $this->computeDividend($askedDividend, $askedDivisor, $returnNumber);
115
116
        /**
117
         * If divisors are different, just multiply them
118
         */
119
120
        $returnNumber->dividend -= $askedDividend;
121
        $returnNumber->simplify();
122
123
        return $returnNumber;
124
    }
125
126 View Code Duplication
    public function multiply($number)
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...
127
    {
128
        list($askedDividend, $askedDivisor) = $this->retrieve($number);
129
        $returnNumber = clone $this;
130
131
        $returnNumber->dividend *= $askedDividend;
132
        $returnNumber->divisor *= $askedDivisor;
133
        $returnNumber->simplify();
134
135
        return $returnNumber;
136
    }
137
138 View Code Duplication
    public function divide($number)
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...
139
    {
140
        list($askedDividend, $askedDivisor) = $this->retrieve($number);
141
        $returnNumber = clone $this;
142
143
        $returnNumber->dividend *= $askedDivisor;
144
        $returnNumber->divisor *= $askedDividend;
145
        $returnNumber->simplify();
146
147
        return $returnNumber;
148
    }
149
150
    // --- Results ------------
151
152
    public function getNumber($roundPrecision = 2, $roundMode = PHP_ROUND_HALF_UP)
153
    {
154
        $value = $this->dividend / $this->divisor;
155
156
        if ($roundPrecision >= 0) {
157
            $value = round($value, $roundPrecision, $roundMode);
158
        }
159
160
        return $value;
161
    }
162
163
    // --- Accessor ------------
164
165
    /**
166
     * @return mixed
167
     */
168
    public function getDividend()
169
    {
170
        return $this->dividend;
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176
    public function getDivisor()
177
    {
178
        return $this->divisor;
179
    }
180
181
    // --- Interfaces implementation ------------
182
    /**
183
     * (PHP 5 &gt;= 5.1.0)<br/>
184
     * String representation of object
185
     * @link http://php.net/manual/en/serializable.serialize.php
186
     * @return string the string representation of the object or null
187
     */
188
    public function serialize()
189
    {
190
        return json_encode(array($this->dividend, $this->divisor));
191
    }
192
193
    /**
194
     * (PHP 5 &gt;= 5.1.0)<br/>
195
     * Constructs the object
196
     * @link http://php.net/manual/en/serializable.unserialize.php
197
     * @param string $serialized <p>
198
     * The string representation of the object.
199
     * </p>
200
     * @return void
201
     */
202
    public function unserialize($serialized)
203
    {
204
        list($this->dividend, $this->divisor) = json_decode($serialized, true);
205
    }
206
207
    /**
208
     * (PHP 5 &gt;= 5.4.0)<br/>
209
     * Specify data which should be serialized to JSON
210
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
211
     * @return mixed data which can be serialized by <b>json_encode</b>,
212
     * which is a value of any type other than a resource.
213
     */
214
    function jsonSerialize()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
215
    {
216
        return $this->serialize();
217
    }
218
219
}
220