Issues (41)

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/actions/AddToCartAction.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
/*
4
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart\actions;
13
14
use hiqdev\yii2\cart\NotPurchasableException;
15
use hiqdev\hiart\Collection;
16
use hiqdev\yii2\cart\CartPositionInterface;
17
use hiqdev\yii2\cart\Module as CartModule;
18
use Yii;
19
20
class AddToCartAction extends \yii\base\Action
21
{
22
    /**
23
     * @var CartPositionInterface The class for new product
24
     */
25
    public $productClass;
26
27
    /**
28
     * @var boolean whether the action expects bulk models load using `selection`
29
     */
30
    public $bulkLoad = false;
31
32
    /**
33
     * @var bool whether client should be redirected to the cart in case of success item adding
34
     */
35
    public $redirectToCart = false;
36
37
    /**
38
     * @var bool whether any errors occurred during save
39
     */
40
    protected $hasErrors = false;
41
42
    /**
43
     * Returns the cart module.
44
     * @return CartModule
45
     */
46
    public function getCartModule()
47
    {
48
        return CartModule::getInstance();
49
    }
50
51
    public function run()
52
    {
53
        $data = null;
0 ignored issues
show
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
        $request = Yii::$app->request;
55
        /** @var CartPositionInterface $model */
56
        $model = Yii::createObject($this->productClass);
0 ignored issues
show
$this->productClass is of type object<hiqdev\yii2\cart\CartPositionInterface>, but the function expects a callable.

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...
57
        $collection = new Collection(); // TODO: drop dependency
58
        $collection->setModel($model);
59
60
        if (!$this->bulkLoad) {
61
            $data = [$request->post() ?: $request->get()];
62
            $collection->load($data);
63
        } else {
64
            $collection->load();
65
        }
66
67
        $positions = [];
68
        foreach ($collection->models as $position) {
69
            /** @var CartPositionInterface $position */
70
            if (!$position->validate()) {
0 ignored issues
show
The method validate() does not seem to exist on object<hiqdev\yii2\cart\CartPositionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
                $this->hasErrors = true;
72
                $error = $collection->getFirstError();
73
                if (empty($error)) {
74
                    $error = Yii::t('cart', 'Failed to add item to the cart');
75
                }
76
                Yii::$app->session->addFlash('warning', $error);
77
                Yii::warning('Failed to add item to cart', 'cart');
78
79
                continue;
80
            }
81
82
            $positions[] = $position;
83
        }
84
85
        try {
86
            $this->putPositionsToCart($positions);
87
        } catch (NotPurchasableException $exception) {
88
            $this->hasErrors = true;
89
            Yii::$app->getSession()->setFlash('error', $exception->getMessage());
0 ignored issues
show
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

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...
90
            $exception->resolve();
0 ignored issues
show
The call to the method hiqdev\yii2\cart\NotPurc...bleException::resolve() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
91
        }
92
    }
93
94
    protected function putPositionsToCart($positions)
95
    {
96
        $cart = $this->getCartModule()->getCart();
97
        $cart->putPositions($positions);
98
    }
99
100
    protected function afterRun()
101
    {
102
        $request = Yii::$app->request;
103
104
        if ($request->isAjax) {
105
            Yii::$app->end();
106
        }
107
108
        if ($this->redirectToCart && !$this->hasErrors) {
109
            return $this->controller->redirect('@cart');
0 ignored issues
show
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

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...
110
        }
111
112
        if (isset($request->referrer)) {
113
            if ($this->hasErrors) {
114
                return $this->controller->redirect('@cart');
115
            }
116
117
            Yii::$app->getSession()->setFlash('success', Yii::t('cart', 'Item has been added to cart'));
0 ignored issues
show
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

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...
118
            return $this->controller->redirect($request->referrer);
119
        }
120
121
        return $this->controller->goHome();
0 ignored issues
show
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

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...
122
    }
123
}
124