Issues (38)

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/services/traits/ModelDelete.php (3 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
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use Craft;
12
use flipbox\spark\helpers\RecordHelper;
13
use flipbox\spark\models\Model;
0 ignored issues
show
This use statement conflicts with another class in this namespace, flipbox\spark\services\traits\Model.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use flipbox\spark\records\Record;
15
use yii\base\ModelEvent;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait ModelDelete
22
{
23
24
    /*******************************************
25
     * ABSTRACTS
26
     *******************************************/
27
28
    /**
29
     * @param Model $model
30
     * @return Record
31
     */
32
    abstract public function getRecordByModel(Model $model): Record;
33
34
35
    /*******************************************
36
     * DELETE
37
     *******************************************/
38
39
    /**
40
     * @param Model $model
41
     * @return bool
42
     * @throws \Exception
43
     */
44
    public function delete(Model $model): bool
45
    {
46
47
        // a 'beforeSave' event
48
        if (!$this->beforeDelete($model)) {
49
            return false;
50
        }
51
52
        // The event to trigger
53
        $event = new ModelEvent();
54
55
        // Db transaction
56
        $transaction = RecordHelper::beginTransaction();
57
58
        try {
59
            // The 'before' event
60
            if (!$model->beforeDelete($event)) {
61
                $transaction->rollBack();
62
63
                return false;
64
            }
65
66
            // Get record
67
            /** @var Record $record */
68
            $record = $this->getRecordByModel($model);
69
70
            // Insert record
71
            if (!$record->delete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record->delete() of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
72
                // Transfer errors to model
73
                $model->addErrors($record->getErrors());
74
75
                // Roll back db transaction
76
                $transaction->rollBack();
77
78
                return false;
79
            }
80
81
            // The 'after' event
82
            if (!$model->afterDelete($event)) {
83
                // Roll back db transaction
84
                $transaction->rollBack();
85
86
                return false;
87
            }
88
        } catch (\Exception $e) {
89
            // Roll back all db actions (fail)
90
            $transaction->rollback();
91
92
            throw $e;
93
        }
94
95
        $transaction->commit();
96
97
        // an 'afterDelete' event
98
        $this->afterDelete($model);
99
100
        return true;
101
    }
102
103
    /**
104
     * @param Model $model
105
     * @return bool
106
     */
107
    protected function beforeDelete(Model $model): bool
0 ignored issues
show
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        return true;
110
    }
111
112
    /**
113
     * @param Model $model
114
     */
115
    protected function afterDelete(Model $model)
116
    {
117
118
        Craft::info(sprintf(
119
            "Model '%s' was deleted successfully.",
120
            (string)get_class($model)
121
        ), __METHOD__);
122
    }
123
}
124