Issues (19)

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/Traits/CouponTrait.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
namespace LukePOLO\LaraCart\Traits;
4
5
use Carbon\Carbon;
6
use LukePOLO\LaraCart\CartItem;
7
use LukePOLO\LaraCart\Exceptions\CouponException;
8
use LukePOLO\LaraCart\Exceptions\InvalidPrice;
9
use LukePOLO\LaraCart\LaraCart;
10
11
/**
12
 * Class CouponTrait.
13
 */
14
trait CouponTrait
15
{
16
    /**
17
     * @var bool
18
     */
19
    public $appliedToCart = true;
20
21
    use CartOptionsMagicMethodsTrait;
22
23
    /**
24
     * Sets all the options for the coupon.
25
     *
26
     * @param $options
27
     */
28
    public function setOptions($options)
29
    {
30
        foreach ($options as $key => $value) {
31
            $this->$key = $value;
32
        }
33
    }
34
35
    /**
36
     * Checks to see if we can apply the coupon.
37
     *
38
     * @return bool
39
     */
40
    public function canApply()
41
    {
42
        try {
43
            $this->discount(true);
0 ignored issues
show
The method discount() does not exist on LukePOLO\LaraCart\Traits\CouponTrait. Did you maybe mean maxDiscount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
45
            return true;
46
        } catch (CouponException $e) {
47
            return false;
48
        }
49
    }
50
51
    /**
52
     * Get the reason why a coupon has failed to apply.
53
     *
54
     * @deprecated 1.3
55
     *
56
     * @return string
57
     */
58
    public function getMessage()
59
    {
60
        try {
61
            $this->discount(true);
0 ignored issues
show
The method discount() does not exist on LukePOLO\LaraCart\Traits\CouponTrait. Did you maybe mean maxDiscount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
63
            return config('laracart.coupon_applied_message', 'Coupon Applied');
64
        } catch (CouponException $e) {
65
            return $e->getMessage();
66
        }
67
    }
68
69
    /**
70
     * Gets the failed message for a coupon.
71
     *
72
     * @return null|string
73
     */
74
    public function getFailedMessage()
75
    {
76
        try {
77
            $this->discount(true);
0 ignored issues
show
The method discount() does not exist on LukePOLO\LaraCart\Traits\CouponTrait. Did you maybe mean maxDiscount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
        } catch (CouponException $e) {
79
            return $e->getMessage();
80
        }
81
    }
82
83
    /**
84
     * Checks the minimum subtotal needed to apply the coupon.
85
     *
86
     * @param $minAmount
87
     * @param $throwErrors
88
     *
89
     * @throws CouponException
90
     *
91
     * @return bool
92
     */
93
    public function checkMinAmount($minAmount, $throwErrors = true)
94
    {
95
        $laraCart = \App::make(LaraCart::SERVICE);
96
97
        if ($laraCart->subTotal(false, false, false) >= $minAmount) {
98
            return true;
99
        } else {
100
            if ($throwErrors) {
101
                throw new CouponException('You must have at least a total of '.$laraCart->formatMoney($minAmount));
102
            } else {
103
                return false;
104
            }
105
        }
106
    }
107
108
    /**
109
     * Returns either the max discount or the discount applied based on what is passed through.
110
     *
111
     * @param $maxDiscount
112
     * @param $discount
113
     * @param $throwErrors
114
     *
115
     * @throws CouponException
116
     *
117
     * @return mixed
118
     */
119
    public function maxDiscount($maxDiscount, $discount, $throwErrors = true)
120
    {
121
        if ($maxDiscount == 0 || $maxDiscount > $discount) {
122
            return $discount;
123
        } else {
124
            if ($throwErrors) {
125
                throw new CouponException('This has a max discount of '.\App::make(\LukePOLO\Laracart\LaraCart::SERVICE)->formatMoney($maxDiscount));
126
            } else {
127
                return $maxDiscount;
128
            }
129
        }
130
    }
131
132
    /**
133
     * Checks to see if the times are valid for the coupon.
134
     *
135
     * @param Carbon $startDate
136
     * @param Carbon $endDate
137
     * @param $throwErrors
138
     *
139
     * @throws CouponException
140
     *
141
     * @return bool
142
     */
143
    public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors = true)
144
    {
145
        if (Carbon::now()->between($startDate, $endDate)) {
146
            return true;
147
        } else {
148
            if ($throwErrors) {
149
                throw new CouponException('This coupon has expired');
150
            } else {
151
                return false;
152
            }
153
        }
154
    }
155
156
    /**
157
     * Sets a discount to an item with what code was used and the discount amount.
158
     *
159
     * @param CartItem $item
160
     * @param $discountAmount
161
     *
162
     * @throws InvalidPrice
163
     */
164
    public function setDiscountOnItem(CartItem $item, $discountAmount)
165
    {
166
        if (!is_numeric($discountAmount)) {
167
            throw new InvalidPrice('You must use a discount amount.');
168
        }
169
        $this->appliedToCart = false;
170
        $item->code = $this->code;
0 ignored issues
show
The property code does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
The property code does not exist on object<LukePOLO\LaraCart\Traits\CouponTrait>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
171
        $item->discount = $discountAmount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $discountAmount can also be of type double or string. However, the property $discount is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
172
        $item->couponInfo = $this->options;
173
    }
174
}
175